#!/usr/bin/env bash
set -e

if [ ! -f ~/.npmrc ]; then
  echo -n '' >> ~/.npmrc
fi


if [ -z "$APP_NAME" ]; then
  APP_NAME=${PWD##*/}
  export APP_NAME=${APP_NAME//-} # (basename of the current directory)
fi

. $(dirname $0)/yak-up -d

export YAKTOR_DOCKER_MACHINE=${YAKTOR_DOCKER_MACHINE-default}

if [ "$(uname)" == "Darwin" ]; then
  if ! ./osx-route.sh 2> osx-route.log ; then
    cat osx-route.log
  fi
fi

if [ "$#" -eq 0 ]; then
  CMD=help
else
  CMD="$@"
fi

if [[ "$CMD" =~ ^help ]]; then
  cat <<EOF

yak: a management script for running a Yaktor stack

Available commands:

  yaktor subcommand                 - execute "yaktor subcommand" in the "app" container
  npm subcommand                    - execute "npm subcommand" in the "app" container
  node subcommand                   - execute "node subcommand" in the "app" container
  grunt subcommand                  - execute "grunt subcommand" in the "app" container
  subcommand                        - execute "grunt subcommand" in the "app" container
  shell [container] [-- subcommand] - start a shell in container ("app" by default) and execute subcommand

EOF
  echo "Available grunt tasks:"
fi

CONTAINER=app
SHELL="bash -c"

APP_CMD_REGEX='^(yaktor|npm|node|grunt).*$'
SHELL_CMD_REGEX='^(bash|shell)( +(([a-zA-Z0-9_]+)?( *\-\- +(.*))?))?$'
if [[ "$CMD" =~ $APP_CMD_REGEX ]]; then
  CMD=". ~/.profile && $CMD"
elif [[ "$CMD" =~ $SHELL_CMD_REGEX ]]; then
  PRECMD=${BASH_REMATCH[1]}
  if [ "$PRECMD" == "bash" ]; then
    echo "WARNING: yak subcommand 'bash' deprecated; use 'shell' from now on" >&2
    PRECMD=shell
  fi
  CONTAINER=${BASH_REMATCH[4]}
  if [ -z "$CONTAINER" ]; then
    CONTAINER=app
  fi
  CMD=${BASH_REMATCH[6]}
  case $CONTAINER in
  app)
    PREFIX=". ~/.profile && bash -l"
    if [ -n "$CMD" ]; then
      FIRST="${CMD::1}"
      if [ "$FIRST" != "'" ] && [ "$FIRST" != '"' ]; then
        CMD="'$CMD'"
      fi
      CMD="$PREFIX -c $CMD"
    else
      CMD="$PREFIX"
    fi
    ;;
  *)
    if [ -z "$CMD" ]; then
      CMD="bash -l"
    fi
    ;;
  # TODO: if we add any container to docker-compose.yml that uses something other than bash, set SHELL in new case clause here for it
  esac
else
  CMD=". ~/.profile && grunt $CMD"
fi

if [ "${INDEX:-1}" -gt "1" ]; then
  docker-compose scale app=${INDEX:-1}
fi

docker-compose exec --index ${INDEX-1} $CONTAINER $SHELL "$CMD"
