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

cmd-storage-ensure-directory() {
  declare desc="creates a persistent storage directory in the recommended storage path"
  declare cmd="storage:ensure-directory"
  [[ "$1" == "$cmd" ]] && shift 1
  declare DIRECTORY CHOWN_FLAG

  CHOWN_FLAG=herokuish
  skip=false
  for arg in "$@"; do
    if [[ "$arg" == "--chown" ]]; then
      skip=true
      continue
    fi

    if [[ "$skip" == "true" ]]; then
      CHOWN_FLAG="$arg"
      skip=false
      continue
    fi

    if [[ -z "$DIRECTORY" ]]; then
      DIRECTORY="$arg"
    fi
  done

  if [[ -z "$DIRECTORY" ]]; then
    dokku_log_fail "Please specify a directory to create"
  fi

  if [[ ! "$DIRECTORY" =~ ^[A-Za-z0-9\\_-]+$ ]]; then
    dokku_log_fail "Directory can only contain the following set of characters: [A-Za-z0-9_-]"
  fi

  if [[ "$CHOWN_FLAG" == "herokuish" ]]; then
    CHOWN_FLAG="32767"
  elif [[ "$CHOWN_FLAG" == "heroku" ]]; then
    CHOWN_FLAG="1000"
  elif [[ "$CHOWN_FLAG" == "packeto" ]]; then
    CHOWN_FLAG="2000"
  elif [[ "$CHOWN_FLAG" == "false" ]]; then
    CHOWN_FLAG="false"
  else
    dokku_log_fail "Unsupported chown permissions"
  fi

  local storage_directory="${DOKKU_LIB_ROOT}/data/storage/$DIRECTORY"
  dokku_log_info1 "Ensuring ${storage_directory} exists"
  mkdir -p "${storage_directory}"
  if [[ "$CHOWN_FLAG" != "false" ]]; then
    dokku_log_verbose_quiet "Setting directory ownership to $CHOWN_FLAG:$CHOWN_FLAG"
    sudo "$PLUGIN_AVAILABLE_PATH/storage/bin/chown-storage-dir" "$DIRECTORY" "$CHOWN_FLAG"
  fi

  dokku_log_verbose_quiet "Directory ready for mounting"
}

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

  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 $(dokku_apps); do
      cmd-storage-report-single "$app" "$INFO_FLAG" | tee || true
    done
  else
    cmd-storage-report-single "$APP" "$INFO_FLAG"
  fi
}

cmd-storage-report-single() {
  declare APP="$1" INFO_FLAG="$2"
  if [[ "$INFO_FLAG" == "true" ]]; then
    INFO_FLAG=""
  fi
  verify_app_name "$APP"
  local flag_map=(
    "--storage-build-mounts: $(fn-storage-bind-mounts "$APP" build)"
    "--storage-deploy-mounts: $(fn-storage-bind-mounts "$APP" deploy)"
    "--storage-run-mounts: $(fn-storage-bind-mounts "$APP" run)"
  )

  if [[ -z "$INFO_FLAG" ]]; then
    dokku_log_info2_quiet "$APP storage information"
    for flag in "${flag_map[@]}"; do
      key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
      dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
    done
  else
    local match=false
    local value_exists=false
    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
    [[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
    [[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
  fi
}

fn-storage-bind-mounts() {
  declare APP="$1" PHASE="$2"
  local PHASE_FILE="$(fn-get-phase-file-path "$APP" "$PHASE")"
  if [[ -r "$PHASE_FILE" ]]; then
    sed -e '/^-v/!d' "$PHASE_FILE" | tr '\n' ' '
  fi
}

cmd-storage-list() {
  declare desc="List all bound mounts"
  declare cmd="storage:list"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" FORMAT_FLAG="$2" FORMAT_FLAG_VALUE="$3"
  local phase="deploy" format="text"

  if [[ "$FORMAT_FLAG" == "--format" ]]; then
    if [[ "$FORMAT_FLAG_VALUE" == "json" ]] || [[ "$FORMAT_FLAG_VALUE" == "text" ]]; then
      format="$FORMAT_FLAG_VALUE"
    else
      dokku_log_fail "Invalid --format value specified"
    fi
  elif [[ -n "$FORMAT_FLAG" ]]; then
    dokku_log_fail "Invalid argument specified"
  fi

  verify_app_name "$APP"
  if [[ "$FORMAT_FLAG_VALUE" == "text" ]]; then
    dokku_log_info1_quiet "$APP volume bind-mounts:"
    plugn trigger storage-list "$APP" "$phase" "$format" | sed "s/^/       /"
  else
    plugn trigger storage-list "$APP" "$phase" "$format"
  fi
}
