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

cmd-storage-mount() {
  declare desc="Add bind-mount, redeploy app if running"
  declare cmd="storage:mount"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" MOUNT_PATH="$2"
  local passed_phases=(deploy run)

  verify_app_name "$APP"
  verify_paths "$MOUNT_PATH"
  check_if_path_exists "$MOUNT_PATH" "$(get_phase_file_path "${passed_phases[@]}")" && dokku_log_fail "Mount path already exists."
  add_passed_docker_option passed_phases[@] "-v $MOUNT_PATH"
}

cmd-storage-mount "$@"
