#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_PATH/common/functions"
source "$(dirname $0)/functions"

case "$1" in
  ps)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    APP="$2"; verify_app_name "$APP"; DOKKU_APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids $APP)
    ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0

    for CID in $DOKKU_APP_RUNNING_CONTAINER_IDS; do
      has_tty && DOKKU_RUN_OPTS="-i -t"
      dokku_log_info1_quiet "running processes in container: $CID"
      docker exec $DOKKU_RUN_OPTS $CID /bin/sh -c "ps auxwww"
    done
  ;;

  ps:start)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    APP="$2"; IMAGE_TAG=$(get_running_image_tag $APP);
    verify_app_name "$APP"

    ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0

    if ! (is_app_running $APP); then
      release_and_deploy "$APP" "$IMAGE_TAG"
    else
      echo "App $APP already running"
    fi
  ;;

  ps:stop)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    APP="$2"; DOKKU_APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids $APP)
    verify_app_name "$APP"

    ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0

    if [[ -n "$DOKKU_APP_RUNNING_CONTAINER_IDS" ]]; then
      echo "Stopping $APP ..."
      docker stop $DOKKU_APP_RUNNING_CONTAINER_IDS > /dev/null || true
    else
      echo "App $APP already stopped"
    fi
  ;;

  ps:rebuild)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    APP="$2"; verify_app_name "$APP"

    pluginhook -p receive-app $APP
  ;;

  ps:rebuildall)
    shopt -s nullglob
    for app in $DOKKU_ROOT/*; do
      [[ ! -d $app ]] && continue
      APP=$(basename $app)
      is_deployed $APP && dokku ps:rebuild $APP
    done
    shopt -u nullglob
  ;;

  ps:restart)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    APP="$2"; IMAGE_TAG=$(get_running_image_tag $APP)
    verify_app_name "$APP"

    ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0

    release_and_deploy "$APP" "$IMAGE_TAG"
  ;;

  ps:restartall)
    shopt -s nullglob
    for app in $DOKKU_ROOT/*; do
      [[ ! -d $app ]] && continue
      APP=$(basename $app)
      dokku ps:restart $APP
    done
    shopt -u nullglob
  ;;

  ps:scale)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    APP="$2"; IMAGE_TAG=$(get_running_image_tag $APP)
    verify_app_name "$APP"

    DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
    shift 2

    generate_scale_file "$APP" "$IMAGE_TAG"
    if [[ -z "$@" ]];then
      dokku_log_info1 "Scaling for $APP"
      dokku_log_info2 "$(< $DOKKU_SCALE_FILE)"
    else
      set_scale "$APP" "$@"
      release_and_deploy "$APP" "$IMAGE_TAG"
    fi
  ;;

  help | ps:help)
    cat && cat<<EOF
    ps <app>, List processes running in app container(s)
    ps:scale <app> <proc>=<count> [<proc>=<count>], Set how many instances of a given process to run
    ps:start <app>, Start app container(s)
    ps:stop <app>, Stop app container(s)
    ps:rebuild <app>, Rebuild an app
    ps:rebuildall, Rebuild all apps
    ps:restart <app>, Restart app container(s)
    ps:restartall, Restart all deployed app containers
EOF
    ;;

  *)
    exit $DOKKU_NOT_IMPLEMENTED_EXIT
    ;;

esac
