#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

fn-letsencrypt-migrate-properties() {
  declare desc="migrates deprecated config variables to property counterpart"
  local value

  value=$(plugn trigger config-get-global "DOKKU_LETSENCRYPT_EMAIL" || true)
  if [[ -n "$value" ]]; then
    dokku_log_info1 "Migrating deprecated global DOKKU_LETSENCRYPT_EMAIL to letsencrypt email property."
    fn-plugin-property-write "letsencrypt" "--global" "email" "$value"
    DOKKU_QUIET_OUTPUT=1 config_unset --global DOKKU_LETSENCRYPT_EMAIL || true
  fi

  value=$(plugn trigger config-get-global "DOKKU_LETSENCRYPT_GRACEPERIOD" || true)
  if [[ -n "$value" ]]; then
    dokku_log_info1 "Migrating deprecated global DOKKU_LETSENCRYPT_GRACEPERIOD to letsencrypt graceperiod property."
    fn-plugin-property-write "letsencrypt" "--global" "graceperiod" "$value"
    DOKKU_QUIET_OUTPUT=1 config_unset --global DOKKU_LETSENCRYPT_GRACEPERIOD || true
  fi

  value=$(plugn trigger config-get-global "DOKKU_LETSENCRYPT_ARGS" || true)
  if [[ -n "$value" ]]; then
    dokku_log_info1 "Migrating deprecated global DOKKU_LETSENCRYPT_ARGS to letsencrypt lego-args property."
    fn-plugin-property-write "letsencrypt" "--global" "lego-args" "$value"
    DOKKU_QUIET_OUTPUT=1 config_unset --global DOKKU_LETSENCRYPT_ARGS || true
  fi

  value="$(fn-plugin-property-get-default "letsencrypt" "--global" "lego-docker-args" "")"
  if [[ -n "$value" ]]; then
    dokku_log_info1 "Migrating deprecated global lego-docker-args property to lego-args."
    fn-plugin-property-write "letsencrypt" "--global" "lego-args" "$value"
    fn-plugin-property-delete "letsencrypt" "--global" "lego-docker-args"
  fi

  value=$(plugn trigger config-get-global "DOKKU_LETSENCRYPT_SERVER" || true)
  if [[ -n "$value" ]]; then
    dokku_log_info1 "Migrating deprecated global DOKKU_LETSENCRYPT_SERVER to letsencrypt server property."
    fn-plugin-property-write "letsencrypt" "--global" "server" "$value"
    DOKKU_QUIET_OUTPUT=1 config_unset --global DOKKU_LETSENCRYPT_SERVER || true
  fi

  for app in $(dokku_apps "false"); do
    value="$(plugn trigger config-get "$app" DOKKU_LETSENCRYPT_EMAIL || true)"
    if [[ -n "$value" ]]; then
      dokku_log_info1 "Migrating deprecated DOKKU_LETSENCRYPT_EMAIL to letsencrypt email property for $app."
      fn-plugin-property-write "letsencrypt" "$app" "email" "$value"
      DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$app" "DOKKU_LETSENCRYPT_EMAIL" || true
    fi

    value="$(plugn trigger config-get "$app" DOKKU_LETSENCRYPT_GRACEPERIOD || true)"
    if [[ -n "$value" ]]; then
      dokku_log_info1 "Migrating deprecated DOKKU_LETSENCRYPT_GRACEPERIOD to letsencrypt graceperiod property for $app."
      fn-plugin-property-write "letsencrypt" "$app" "graceperiod" "$value"
      DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$app" "DOKKU_LETSENCRYPT_GRACEPERIOD" || true
    fi

    value="$(plugn trigger config-get "$app" DOKKU_LETSENCRYPT_ARGS || true)"
    if [[ -n "$value" ]]; then
      dokku_log_info1 "Migrating deprecated DOKKU_LETSENCRYPT_ARGS to letsencrypt lego-args property for $app."
      fn-plugin-property-write "letsencrypt" "$app" "lego-args" "$value"
      DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$app" "DOKKU_LETSENCRYPT_ARGS" || true
    fi

    value="$(fn-plugin-property-get-default "letsencrypt" "$app" "lego-docker-args" "")"
    if [[ -n "$value" ]]; then
      dokku_log_info1 "Migrating deprecated lego-docker-args property to lego-args for $app."
      fn-plugin-property-write "letsencrypt" "$app" "lego-args" "$value"
      fn-plugin-property-delete "letsencrypt" "$app" "lego-docker-args"
    fi

    value="$(plugn trigger config-get "$app" DOKKU_LETSENCRYPT_SERVER || true)"
    if [[ -n "$value" ]]; then
      dokku_log_info1 "Migrating deprecated DOKKU_LETSENCRYPT_SERVER to letsencrypt server property for $app."
      fn-plugin-property-write "letsencrypt" "$app" "server" "$value"
      DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$app" "DOKKU_LETSENCRYPT_SERVER" || true
    fi
  done
}

plugin-install() {
  pull-docker-image() {
    declare IMAGE="$1"
    if [[ "$PLUGIN_DISABLE_PULL" == "true" ]]; then
      echo " !     ${PLUGIN_DISABLE_PULL_VARIABLE} environment variable detected. Not running pull command." 1>&2
      echo " !        docker pull ${IMAGE}" 1>&2
      return
    fi
    if [[ "$(docker images -q "${IMAGE}" 2>/dev/null)" == "" ]]; then
      docker pull "${IMAGE}"
    fi
  }

  pull-docker-image "${PLUGIN_IMAGE}:${PLUGIN_IMAGE_VERSION}"

  mkdir -p "${DOKKU_LIB_ROOT}/data/letsencrypt"
  chmod 0755 "${DOKKU_LIB_ROOT}/data/letsencrypt"
  # Migrate the legacy autorenew marker into the --global container introduced
  # to keep plugin-owned entries from colliding with same-named apps. Drop the
  # legacy copy unconditionally once the new one exists.
  mkdir -p "${DOKKU_LIB_ROOT}/data/letsencrypt/--global"
  if [[ -f "${DOKKU_LIB_ROOT}/data/letsencrypt/autorenew" && ! -e "${DOKKU_LIB_ROOT}/data/letsencrypt/--global/autorenew" ]]; then
    touch "${DOKKU_LIB_ROOT}/data/letsencrypt/--global/autorenew"
  fi
  rm -f "${DOKKU_LIB_ROOT}/data/letsencrypt/autorenew"
  # Heal per-app ACME webroots whose mode was previously left at the invoker's
  # umask (commonly 0700 under sudo). nginx must be able to read these to
  # serve HTTP-01 challenges; the files are public by design. Skip the
  # `--global` container, whose `accounts/` leaf is locked down to 0700 just
  # below.
  find "${DOKKU_LIB_ROOT}/data/letsencrypt" -mindepth 1 -maxdepth 1 -type d \
    ! -name '--global' -exec chmod 0755 {} +
  mkdir -p "${DOKKU_LIB_ROOT}/data/letsencrypt/--global/accounts"
  chmod 0700 "${DOKKU_LIB_ROOT}/data/letsencrypt/--global/accounts"
  chown -R "${DOKKU_SYSTEM_USER}:${DOKKU_SYSTEM_GROUP}" "${DOKKU_LIB_ROOT}/data/letsencrypt"
  fn-plugin-property-setup "letsencrypt"
  fn-letsencrypt-migrate-properties
}

plugin-install "$@"
