#!/bin/bash -e

hst_this=$(hostname -s)
if ! echo "$hst_this" | egrep -q "^(admin|develop)"; then
  # unless the .ao_no_proxy file exists
  if [[ ! -f ".ao_no_proxy" ]]; then
    # ssh into deploy host and run from there
    exec bin/proxy $(basename $0) "$@"
    exit 1
  fi
fi

export LOCAL_ONLY=true

case "$(basename $0)" in
  invoke)
    export COMMAND="$1"; shift

    if [[ -n $1 ]]; then
      set -- $(for a in "$@"; do echo $a.app; done)
    fi

    LOCAL_ONLY=
    tmp_invoke=$(mktemp -t XXXXXXXXX)
    bundle exec cap "$@" invoke 2>&1 | perl -pe 's{.\[\d+m}{}g' | perl -ne 'm{^\s*\*+\s*\[(out|err)\s*::\s*([^]]+)\] ?(.*)} && print "$1 $2 $3\n"' > $tmp_invoke 2>&1

    cat $tmp_invoke | awk '{print $2}' | sort -u | while read -r nm_host; do
      egrep "^(out|err) $nm_host" $tmp_invoke || true
      echo
    done
    rm -f $tmp_invoke
    ;;
  *)
    if [[ -n $1 ]]; then
      set -- $(for a in "$@"; do echo $a; done)
    fi

    case "$(basename $0)" in
      debug)
        if [[ -z $1 ]]; then
          set "world"
        fi

        if [[ -n $1 ]]; then
          cap $(for a in "$@"; do echo $a.echo; done)
        fi
        ;;

      hosts)
        {
          if [[ -n $1 ]]; then
            cap $(for a in "$@"; do echo $a.echo; done)
          fi
        } 2>&1 | grep eea914aaa8dde6fdae29242b1084a2b0415eefaf | awk '{print $NF}' | sort -u
        ;;

      dna)
        {
          echo "eea914aaa8dde6fdae29242b1084a2b0415eefaf ---"
          if [[ -n $1 ]]; then
            cap $(for a in "$@"; do echo $a.yaml; done)
          fi
        } 2>&1 | grep eea914aaa8dde6fdae29242b1084a2b0415eefaf | sed 's#eea914aaa8dde6fdae29242b1084a2b0415eefaf ##'
        ;;

      *)
        LOCAL_ONLY=
        if [[ -n $1 ]]; then
          set -- $(for a in "$@"; do echo $a.app; done)
        fi
        case "$(basename $0)" in
          deploy)
            bundle exec cap "$@" deploy:lock deploy
            ;;

          release)
            bundle exec cap "$@" deploy:lock deploy
            ;;

          stage)
            bundle exec cap "$@" deploy:lock deploy:update_code deploy:symlink_next
            ;;

          restart)
            bundle exec cap "$@" deploy:lock deploy:restart
            ;;

          config)
            bundle exec cap "$@" deploy:lock deploy:config
            ;;

          build)
            bundle exec cap "$@" deploy:lock deploy:build
            ;;

          dist)
            bundle exec cap "$@" deploy:lock deploy:dist
            ;;

          activate)
            bundle exec cap "$@" deploy:lock deploy:symlink deploy:restart
            ;;

          rollback)
            bundle exec cap "$@" deploy:lock deploy:rollback
            ;;

          check)
            bundle exec cap "$@" deploy:lock deploy:check
            ;;

          plan)
            bundle exec cap "$@" deploy:lock deploy:plan
            ;;

          lock)
            bundle exec cap "$@" deploy:dont_unlock deploy:lock
            ;;

          unlock)
            bundle exec cap "$@" deploy:unlock
            ;;

          shell)
            bundle exec cap "$@" shell
            ;;

          compare)
            bundle exec cap "$@" deploy:compare
            ;;

          migrate)
            want_list=
            if [[ $1 = "list" ]]; then
              want_list=1; shift
            fi

            change_group=
            migration=
            case $1 in
              data)
                change_group=$1; shift
                ;;
              pre)
                change_group=$1; shift
                ;;
              during)
                change_group=$1; shift
                ;;
              post)
                change_group=$1; shift
                ;;
              [0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789])
                migration=$1; shift
                ;;
            esac

            if [[ -n $change_group ]]; then
              if [[ -n $want_list ]]; then
                echo "listing change group $change_group"
              else
                echo "running change group $change_group"
              fi
            elif [[ -n $migration ]]; then
              echo "running migration $migratio"
            elif [[ -n $want_list ]]; then
              echo "listing migrations"
            fi

            bundle exec cap "$@" deploy:migrate
            ;;

          *)
            bundle exec cap "$@"
            ;;
        esac
    esac
    ;;
esac