#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/docker-options/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

cmd-ps-inspect() {
  declare desc="displays a sanitized version of docker inspect for an app"
  declare cmd="ps:inspect"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1"

  verify_app_name "$APP"
  local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
  plugn trigger scheduler-inspect "$DOKKU_SCHEDULER" "$APP"
}

cmd-ps-report() {
  declare desc="displays a ps report for one or more apps"
  declare cmd="ps:report"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" INFO_FLAG="$2"
  local INSTALLED_APPS=$(dokku_apps)

  if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then
    INFO_FLAG="$APP"
    APP=""
  fi

  if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then
    INFO_FLAG="true"
  fi

  if [[ -z "$APP" ]]; then
    for app in $INSTALLED_APPS; do
      cmd-ps-report-single "$app" "$INFO_FLAG" | tee || true
    done
  else
    cmd-ps-report-single "$APP" "$INFO_FLAG"
  fi
}

cmd-ps-report-single() {
  declare APP="$1" INFO_FLAG="$2"
  local APP_DIR="$DOKKU_ROOT/$APP"
  [[ "$INFO_FLAG" == "true" ]] && INFO_FLAG=""

  local APP_RUNNING="$(fn-ps-app-status "$APP")"
  local PROCS=$(echo "$APP_RUNNING" | cut -d ' ' -f 1)
  local RUNNING=$(echo "$APP_RUNNING" | cut -d ' ' -f 2)

  if (is_deployed "$APP"); then DEPLOYED="true"; else DEPLOYED="false"; fi
  local passed_phases="deploy"
  local RESTARTPOLICY=$(get_restart_policies "$(get_phase_file_path "$passed_phases")" || true)
  local CAN_SCALE="$(fn-ps-can-scale "$APP")"

  local CONTAINER_FILES="$(find "$DOKKU_ROOT/$APP" -maxdepth 1 -name "CONTAINER.*" -printf "%f\n" 2>/dev/null | sort -t . -k 2 -n | xargs)"
  local CONTAINER_FILE
  local DOKKU_APP_RESTORE=$(config_get "$APP" DOKKU_APP_RESTORE || true)
  local STATUSES=()
  if [[ "$DOKKU_APP_RESTORE" != 0 ]]; then RESTORE="true"; else RESTORE="false"; fi
  local APP_CONTAINER_STATUS
  for CONTAINER_FILE in $CONTAINER_FILES; do
    CID=$(<"$DOKKU_ROOT/$APP/$CONTAINER_FILE")
    APP_CONTAINER_STATUS=$("$DOCKER_BIN" container inspect --format '{{.State.Status}}' "$CID" 2>/dev/null || true)
    [[ -z "$APP_CONTAINER_STATUS" ]] && APP_CONTAINER_STATUS="missing"
    STATUSES+=("${CONTAINER_FILE#*.}:$APP_CONTAINER_STATUS#${CID:0:12}")
  done
  local flag_map=(
    "--processes: $PROCS"
    "--deployed: $DEPLOYED"
    "--running: $RUNNING"
    "--restore: $RESTORE"
    "--restart-policy: $RESTARTPOLICY"
    "--ps-can-scale: $CAN_SCALE"
  )

  if [[ -z "$INFO_FLAG" ]]; then
    dokku_log_info2_quiet "$APP ps information"
    for flag in "${flag_map[@]}"; do
      key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
      dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
    done
    for STATUS in "${STATUSES[@]}"; do
      name=${STATUS%:*}
      value=${STATUS#*:}
      status=${value%#*}
      cid=${value#*#}
      dokku_log_verbose "$(printf "%-30s %-10s (CID: %s)" "Status $name:" "$status" "$cid")"
    done
  else
    local match=false
    local value_exists=false
    for STATUS in "${STATUSES[@]}"; do
      name=${STATUS%:*}
      value=${STATUS#*:}
      status=${value%#*}
      cid=${value#*#}
      status_flag="--status-$name"
      flag_map+=("$status_flag: $status")
    done
    for flag in "${flag_map[@]}"; do
      valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
      if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
        value=${flag#*: }
        size="${#value}"
        if [[ "$size" -ne 0 ]]; then
          echo "$value" && match=true && value_exists=true
        else
          match=true
        fi
      fi
    done
    if [[ "$match" == "true" ]]; then
      [[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
    else
      dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
    fi
  fi
}

fn-ps-parallel-cmd() {
  declare desc="Run a ps command against all apps"
  declare CMD="$1"
  local GNU_PARALLEL has_apps=false

  for APP in $(dokku_apps); do
    has_apps=true
    break
  done

  if [[ "$has_apps" == "false" ]]; then
    dokku_log_info1 "No apps detected"
    return
  fi

  if which parallel >/dev/null 2>&1; then
    dokku_log_info1 "Running 'dokku ps:${CMD}' in parallel"
    GNU_PARALLEL=$(parallel -V 2>&1 | grep GNU || true)
    if [[ -z "$GNU_PARALLEL" ]]; then
      # shellcheck disable=SC2046
      parallel $DOKKU_PARALLEL_ARGUMENTS -i bash -c "dokku ps:${CMD} {}" -- $(dokku_apps)
    else
      # shellcheck disable=SC2046
      dokku_apps | parallel $DOKKU_PARALLEL_ARGUMENTS "dokku ps:${CMD} {}"
    fi
    return
  fi

  dokku_log_info1 "Running 'dokku ps:${CMD}' in serially"
  for APP in $(dokku_apps); do
    if ! (is_deployed "$APP"); then
      dokku_log_warn "App $APP has not been deployed"
      continue
    fi

    if [[ "$CMD" == "restore" ]]; then
      local DOKKU_APP_RESTORE=$(config_get "$APP" DOKKU_APP_RESTORE || true)
      if [[ "$DOKKU_APP_RESTORE" == 0 ]]; then
        continue
      fi
    fi

    dokku_log_verbose "Running 'dokku ps:${CMD} $APP' ..."
    if ps_${CMD} "$APP"; then
      continue
    fi
    dokku_log_warn "dokku ps:${CMD} $APP failed"
  done
}
