#!/bin/bash

export _AO_OPT="$@"

function deploy_basename {
  local cmd="$(basename "$1")"; shift
  echo "${cmd#aoh-}"
} 

# define command line options:
#   var name, default, description, short option
DEFINE_string "gateway" "" "deploy gateway host" "G"
DEFINE_boolean "proxy" "$FLAGS_FALSE" "use a gateway to proxy deploys" "p"
DEFINE_boolean "direct" "$FLAGS_FALSE" "override proxy" "d"
DEFINE_string "chef" "" "location to chef repo with opsdb" "c"
DEFINE_string "tag" "HEAD" "tag to deploy in production" "t"
DEFINE_string "user" "" "app user override" "u"
DEFINE_string "group" "" "app group override" "g"
DEFINE_string "ssh_login" "" "ssh login override" "l"
DEFINE_boolean "migrations" "$FLAGS_FALSE" "run development migrations" "M"

# entry point
function main {
  export _AO_ARGS="$@"

  if [[ "$#" = 0 ]]; then
    logger_fatal "must specify a pod name or integration environment when deploying"
    exit 1
  fi

  local nm_pod="$1" # will shift later since this could be an integration environment
  if [[ "$#" = 1 ]]; then
    local deploy_base="$(ryaml $_AO_HOME/config/deploy.yml deploy_base)"
    if [[ -n "$deploy_base" ]]; then
      local pod_shortcut="$(ryaml $HOME/.getting-started/config/pods.yml pods ${deploy_base}${nm_pod} pod)"
      if [[ -n "$pod_shortcut" ]]; then
        nm_pod="${deploy_base}${nm_pod}"
        set -- "$nm_pod"
      fi
      unset pod_shortcut
    fi
  fi

  if [[ -r "$HOME/.getting-started/config/pods.yml" ]]; then
    local pod_shortcut="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod pod)"
    if [[ -n "$pod_shortcut" ]]; then
      nm_pod="$pod_shortcut"
    else
      shift # treat as a pod name
    fi

    local pod_alias="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod alias)"
    if [[ -n "$pod_alias" ]]; then
      nm_pod="$pod_alias"
    fi

    local is_local="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod local)"
    local hst_gateway="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod gateway)"
    if [[ -z "$is_local" ]]; then
      if [[ -z "$hst_gateway" ]]; then
        logger_fatal "$nm_pod is not a pod name"
        exit 1
      fi
      FLAGS_proxy="$FLAGS_TRUE"
    else
      if [[ -z "$FLAGS_chef" ]]; then
        FLAGS_chef="$HOME/.getting-started"
      fi
      export _AO_ENV="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod ao_env)"
      export RAILS_ENV="$_AO_ENV"
      if [[ -z "$_AO_ENV" ]]; then
        _AO_ENV="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod env)"
      fi
      export _AO_SSH="$HOME/.getting-started/ssh/config"
    fi

    if [[ -z "$FLAGS_gateway" ]]; then
      FLAGS_gateway="$hst_gateway"
    fi
  else
    shift # not proxied, so expect full names here
  fi

  if [[ -n "$FLAGS_user" ]]; then
    export _AO_USER="$FLAGS_user"
  fi

  : ${_AO_DEPLOYER:=$LOGNAME}
  export _AO_DEPLOYER

  if [[ -n "$FLAGS_group" ]]; then
    export _AO_GROUP="$FLAGS_group"
  fi

  if [[ -n "$FLAGS_ssh_login" ]]; then
    export _AO_SSH_LOGIN="$FLAGS_ssh_login"
  fi

  if [[ -n "$FLAGS_chef" ]]; then
    export _AO_CHEF="$FLAGS_chef"
  fi

  if [[ -n "$FLAGS_gateway" ]]; then
    export GATEWAY="$FLAGS_gateway"
  fi

  if [[ "$FLAGS_proxy" = "$FLAGS_FALSE" || "$FLAGS_direct" = "$FLAGS_TRUE" || -z "$FLAGS_gateway" ]]; then
    export _AO_DEPLOY=1

    local _AO_THIS_HOST="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod this_host)"
    if [[ -n "$_AO_THIS_HOST" ]]; then
      export _AO_THIS_HOST
    fi

    local _AO_THIS_POD="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_pod this_pod)"
    if [[ -n "$_AO_THIS_POD" ]]; then
      export _AO_THIS_POD
    fi

    bundle check 2>&1 >/dev/null || { bundle install --quiet --local --path vendor/bundle || bundle check > /dev/null; }

    if [[ "$nm_pod" = "pod1" ]]; then
      nm_pod="sys"
    fi

    if [[ "$nm_pod" = "sys" && "$#" > 0 ]]; then
      if [[ "${1%.*}" = "production" ]]; then
        set -- "admin" "$@"
      elif [[ "${1%.*}" = "master" ]]; then
        set -- "master00" "$@"
      elif [[ "${1%.*}" = "qa" ]]; then
        set -- "qa00" "$@"
      elif [[ "${1%.*}" = "staging" ]]; then
        set -- "staging00" "$@"
      fi
    fi

    if [[ "$FLAGS_migrations" = "$FLAGS_TRUE" ]]; then
      export FLAGS_migrations
    fi
    aomain "$nm_pod" "$@"
  else 
    if [[ "$#" > 0 ]]; then
      case "$1" in
        all|world)
          logger_fatal "cannot use all or world, these are dangerous"
          exit 1
          ;;
        production)
          if [[ -z "$FLAGS_tag" || "$FLAGS_tag" = "HEAD" ]]; then
            case "$(deploy_basename $0)" in
              hosts|debug|dna)
                true
                ;;
              *)
                logger_fatal "must specify a version using --tag|-t"
                exit 1
                ;;
            esac
          fi
          ;;
      esac
    fi

    local gateway="$FLAGS_gateway"
    if [[ -n "$FLAGS_ssh_login" ]]; then
      gateway="${FLAGS_ssh_login}@${gateway}"
    fi

    local remote_chef=""
    if [[ -n "$FLAGS_chef" ]]; then
      remote_chef="-c $FLAGS_chef "
    fi

    $shome/sbin/proxy "$gateway" "$FLAGS_tag" $(deploy_basename $0) ${remote_chef}"$nm_pod" "$@"
  fi
}