#!/usr/bin/env bash
[[ " ps ps:start ps:stop ps:rebuild ps:rebuildall ps:restart ps:restartall ps:restore ps:scale help ps:help " == *" $1 "* ]] || exit $DOKKU_NOT_IMPLEMENTED_EXIT
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"

case "$1" in
  ps)
    [[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
    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 ]] && dokku_log_fail "Please specify an app to run the command on"
    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 ]] && dokku_log_fail "Please specify an app to run the command on"
    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
      plugn trigger post-stop $APP
    else
      echo "App $APP already stopped"
    fi
    ;;

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

    plugn trigger receive-app $APP
    ;;

  ps:rebuildall)
    shopt -s nullglob
    for app in $(dokku_apps); do
      is_deployed $app && dokku ps:rebuild $app
    done
    shopt -u nullglob
    ;;

  ps:restart)
    [[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
    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_apps); do
      dokku ps:restart $app
    done
    shopt -u nullglob
    ;;

  ps:restore)
    shopt -s nullglob
    for app in $(dokku_apps); do
      DOKKU_APP_RESTORE=$(dokku config:get $app DOKKU_APP_RESTORE || true)
      if [[ $DOKKU_APP_RESTORE != 0 ]]; then
        echo "Restoring app $app ..."
        dokku ps:start $app
      fi
    done
    shopt -u nullglob
  ;;

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

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

    [[ ! -e $DOKKU_SCALE_FILE ]] && generate_scale_file "$APP" "$IMAGE_TAG"
    if [[ -z "$@" ]];then
      dokku_log_info1_quiet "Scaling for $APP"
      dokku_col_log_info1_quiet "proctype" "qty"
      dokku_col_log_info1_quiet "--------" "---"
      while read -r line || [[ -n "$line" ]]; do
        [[ -z "$line" ]] && continue
        PROC_NAME=${line%%=*}
        PROC_COUNT=${line#*=}
        dokku_col_log_info1 "$PROC_NAME" "$PROC_COUNT"
      done < "$DOKKU_SCALE_FILE"
    else
      set_scale "$APP" "$@"
      release_and_deploy "$APP" "$IMAGE_TAG"
    fi
    ;;

  help | ps:help)
    cat<<EOF
    ps <app>, List processes running in app container(s)
    ps:scale [<app> <proc>=<count> [<proc>=<count>]], Get/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
    ps:restore, Start previously running apps e.g. after reboot
EOF
    ;;

  *)
    exit $DOKKU_NOT_IMPLEMENTED_EXIT
    ;;

esac
