#!/usr/bin/env bash

set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x

# shellcheck disable=SC1090
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"

dokku-ansible-deploy-validate-dependencies() {
  # shellcheck disable=SC2034
  declare desc="check that ansible dependencies are available"

  if ! command -v "ansible-playbook" &>/dev/null; then
    dokku_col_log_info1_quiet "Missing ansible dependency, run dokku plugin:install-dependencies"
    exit 1
  fi
}

dokku-ansible-deploy-vault-pass-cmd() {
  # shellcheck disable=SC2034
  declare desc="add new app vault password for decryption of passwords"

  declare APP="$2"
  declare vault_file="$DOKKU_LIB_ROOT/data/deploy.d/$APP/.vault-password.sh"

  if [[ ! -n "$APP" ]]; then
    dokku_col_log_info1_quiet "missing app name, try 'dokku ansible-deploy:vault-pass myappname'"
    exit 1
  fi

  if [[ -f $vault_file ]]; then
    dokku_col_log_info1_quiet "Vault password already setup for $APP"
    exit 0
  fi

  # shellcheck disable=SC2162 disable=SC2116 disable=SC2006
  read -p "Please enter your vault password for $APP: `echo $'\n> '`" vault_password

  if [[ ! -d "$DOKKU_LIB_ROOT/data/deploy.d/$APP" ]]; then
    mkdir -p "$DOKKU_LIB_ROOT/data/deploy.d/$APP"
  fi
  dokku_col_log_info1_quiet "Created $DOKKU_LIB_ROOT/data/deploy.d/$APP"

  { echo "#!/bin/bash";
    echo "";
    echo "set -eu -o pipefail";
    echo "";
    echo "echo \"$vault_password\""; } > "$vault_file"

  chmod +x "$vault_file"

  dokku_col_log_info1_quiet "Generated $vault_file for $APP"
}

dokku-ansible-deploy-dependencies() {
  # shellcheck disable=SC2034
  declare desc="install plugin dependencies"

  declare DEPENDENCIES="ansible python3 python3-dev python3-ruamel.yaml"

  dokku_col_log_info1_quiet "Ensuring the following packages are installed: $DEPENDENCIES"

  export DEBIAN_FRONTEND=noninteractive
  apt install -qq -y "$DEPENDENCIES"
}

dokku-ansible-deploy-post-extract() {
  # shellcheck disable=SC2034
  declare desc="run the post-extract hook to setup the plugin"

  declare APP="$1" TMPDIR="$2"

  if [[ -d "$TMPDIR/deploy.d" ]] && [[ "$(ls -A "$TMPDIR/deploy.d")" ]]; then
    mkdir -p "$DOKKU_LIB_ROOT/data/deploy.d/$APP"
    cp -r "$TMPDIR/deploy.d/." "$DOKKU_LIB_ROOT/data/deploy.d/$APP"
    dokku_col_log_info1_quiet "Copied deploy.d files into place"

    if [[ -d "$TMPDIR/deploy.d/vault" ]]; then
      if [[ ! -f "$DOKKU_LIB_ROOT/data/deploy.d/$APP/.vault-password.sh" ]]; then
        dokku_col_log_info1_quiet "Vault directory discovered but missing vault password"
        dokku_col_log_info1_quiet "Please run dokku ansible-deploy:vault-password $APP"
        exit 1
      fi
    fi
  fi
}

dokku-ansible-deploy-pre-deploy() {
  # shellcheck disable=SC2034
  declare desc="run the pre-deploy hook to setup an app"

  declare APP="$1"
  declare PREDEPLOY="$PLUGIN_CORE_AVAILABLE_PATH/ansible-deploy/scripts/predeploy.py"

  dokku_col_log_info1_quiet "Running pre-deployment steps"
  /usr/bin/python3 "$PREDEPLOY"

}

dokku-ansible-deploy-post-deploy() {
  # shellcheck disable=SC2034
  declare desc="run the post-deploy hook to finish an app setup"

  declare APP="$1"
  declare POSTDEPLOY="$PLUGIN_CORE_AVAILABLE_PATH/ansible-deploy/scripts/postdeploy.py"

  dokku_col_log_info1_quiet "Running post-deploy steps"
  /usr/bin/python3 "$POSTDEPLOY" "$APP"
}

dokku-ansible-deploy-post-delete() {
  # shellcheck disable=SC2034
  declare desc="run the post-delete hook to remove an app"

  declare APP="$1"
  declare POSTDELETE="$PLUGIN_CORE_AVAILABLE_PATH/ansible-deploy/scripts/postdelete.py"

  dokku_col_log_info1_quiet "Running post-delete steps"
  /usr/bin/python3 "$POSTDELETE" "$APP"
}