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

trigger-scheduler-docker-local-pre-deploy() {
  declare desc="scheduler-docker-local pre-deploy plugin trigger"
  declare trigger="pre-deploy"
  declare APP="$1" IMAGE_TAG="$2"

  local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
  if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
    return
  fi

  scheduler-docker-local-pre-deploy-chown-app "$APP" "$IMAGE_TAG"
  scheduler-docker-local-pre-deploy-precheck "$APP"
}

scheduler-docker-local-pre-deploy-chown-app() {
  declare desc="Runs chown against the /app directory for herokuish images"
  declare APP="$1" IMAGE_TAG="$2"
  local DOCKER_RUN_LABEL_ARGS="--label=com.dokku.app-name=$APP"
  local IMAGE DISABLE_CHOWN DOCKER_ARGS DOKKU_APP_TYPE DOKKU_APP_USER APP_PATHS CONTAINER_PATHS
  declare -a ARG_ARRAY

  IMAGE=$(get_app_image_name "$APP" "$IMAGE_TAG")

  DOKKU_APP_TYPE=$(config_get "$APP" DOKKU_APP_TYPE || true)
  DOKKU_APP_USER=$(config_get "$APP" DOKKU_APP_USER || true)
  DOKKU_APP_USER=${DOKKU_APP_USER:="herokuishuser"}
  APP_PATHS=$(plugn trigger storage-list "$APP" "deploy")

  if [[ -n "$APP_PATHS" ]]; then
    CONTAINER_PATHS=$(echo "$APP_PATHS" | awk -F ':' '{ print $2 }' | xargs)
    DOCKER_ARGS=$(: | plugn trigger docker-args-deploy "$APP" "$IMAGE_TAG")

    filterdArgs=("--cpus" "--gpus" "--memory" "--memory-reservation" "--memory-swap" "--publish" "--publish-all" "-p" "-P" "--restart")
    for filteredArg in "${filterdArgs[@]}"; do
      # shellcheck disable=SC2001
      DOCKER_ARGS=$(sed -e "s/$filteredArg=[[:graph:]]\+[[:blank:]]\?//g" <<<"$DOCKER_ARGS")

      # shellcheck disable=SC2001
      DOCKER_ARGS=$(sed -e "s/$filteredArg\+[[:blank:]][[:graph:]]\+[[:blank:]]\?//g" <<<"$DOCKER_ARGS")
    done

    eval "ARG_ARRAY=($DOCKER_ARGS)"
  fi

  if [[ "$DOKKU_APP_TYPE" != "herokuish" ]] || [[ -z "$CONTAINER_PATHS" ]]; then
    return
  fi

  DISABLE_CHOWN="$(fn-plugin-property-get "scheduler-docker-local" "$APP" "disable-chown" "")"
  if [[ "$DISABLE_CHOWN" == "true" ]]; then
    return
  fi

  "$DOCKER_BIN" container run --rm "${DOCKER_RUN_LABEL_ARGS[@]}" $DOKKU_GLOBAL_RUN_ARGS "${ARG_ARRAY[@]}" $IMAGE /bin/bash -c "find $CONTAINER_PATHS -not -user $DOKKU_APP_USER -print0 | xargs -0 -r chown -R $DOKKU_APP_USER" || true
}

scheduler-docker-local-pre-deploy-precheck() {
  declare desc="Outputs the checks messages if necessary"
  declare APP="$1"

  dokku_log_info2 "Processing deployment checks"

  local TMP_APP_JSON_OUTPUT=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
  trap "rm -rf '$TMP_APP_JSON_OUTPUT' >/dev/null" RETURN INT TERM EXIT
  plugn trigger app-json-get-content "$APP" >"$TMP_APP_JSON_OUTPUT"

  if ! docker-container-healthchecker exists web --app-json "$TMP_APP_JSON_OUTPUT"; then
    local CHECKS_URL="${DOKKU_CHECKS_URL:-https://dokku.com/docs/deployment/zero-downtime-deploys/}"
    dokku_log_verbose "No web healthchecks found in app.json. Simple container checks will be performed."
    dokku_log_verbose "For more efficient zero downtime deployments, add healthchecks to your app.json. See ${CHECKS_URL} for examples"
  fi
}

trigger-scheduler-docker-local-pre-deploy "$@"
