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

case "$1" in
  ps)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    verify_app_name "$2"

    APP="$2"; CONTAINER_IDS=$(get_app_container_ids $APP)
    ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0

    for CID in $CONTAINER_IDS; do
      docker exec -ti "$CID" /bin/bash -c "ps auxwww"
    done
  ;;

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

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

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

    APP="$2"; CONTAINER_IDS=$(get_app_container_ids $APP)
    ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0

    DOKKU_APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids $APP)
    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
    verify_app_name "$2"
    APP="$2"

    pluginhook 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
    verify_app_name "$2"

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

    release_and_deploy $APP
  ;;

  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
    verify_app_name "$2"

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

    generate_scale_file "$APP"
    if [[ -z "$@" ]];then
      dokku_log_info1 "Scaling for $APP"
      dokku_log_info2 "$(< $DOKKU_SCALE_FILE)"
    else
      set_scale "$APP" "$@"
      release_and_deploy "$APP"
    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 processes 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
