#!/bin/bash

set -euo pipefail

# === Default Options ===
DRY_RUN=false
INTERACTIVE=true

# === Help Message ===
show_help() {
  cat <<EOF
Usage: $0 [--dry-run] [--interactive|--no-interactive] <POINT_RELEASE> <RELEASE>

Promotes a Ceph release from prerelease to production.

Arguments:
  <POINT_RELEASE>    e.g., 18.2.7
  <RELEASE>          e.g., reef

Options:
  --dry-run          Show what would be done without making changes
  --interactive      Prompt before each action (default)
  --no-interactive   Do not prompt before actions
  --help             Show this help message

Environment Variables:
  PRERELEASE_BASE    Path to prerelease/ceph (default: /data/download.ceph.com/www/prerelease/ceph)
  PRODUCTION_BASE    Path to production (default: /data/download.ceph.com/www/production)
EOF
  exit 0
}

# === Parse Arguments ===
POSITIONAL=()
while [[ $# -gt 0 ]]; do
  case "$1" in
    --dry-run)
      DRY_RUN=true
      shift
      ;;
    --interactive)
      INTERACTIVE=true
      shift
      ;;
    --no-interactive)
      INTERACTIVE=false
      shift
      ;;
    --help)
      show_help
      ;;
    -*)
      echo "Unknown option: $1"
      show_help
      ;;
    *)
      POSITIONAL+=("$1")
      shift
      ;;
  esac
done

if [[ ${#POSITIONAL[@]} -ne 2 ]]; then
  show_help
fi

POINT_RELEASE="${POSITIONAL[0]}"
RELEASE="${POSITIONAL[1]}"

# === Paths ===
PRERELEASE_BASE="${PRERELEASE_BASE:-/data/download.ceph.com/www/prerelease/ceph}"
PRODUCTION_BASE="${PRODUCTION_BASE:-/data/download.ceph.com/www}"
PRERELEASE_TARBALLS="$PRERELEASE_BASE/tarballs"
PRODUCTION_TARBALLS="$PRODUCTION_BASE/tarballs"

# === Utility Functions ===
confirm_and_run() {
  local CMD="$1"
  echo "> $CMD"
  if [[ "$DRY_RUN" == true ]]; then
    return
  fi
  if [[ "$INTERACTIVE" == true ]]; then
    read -p "Run this command? [y/N] " yn
    case "$yn" in
      [Yy]*) eval "$CMD" ;;
      *) echo "Skipped." ;;
    esac
  else
    eval "$CMD"
  fi
}

check_exists() {
  local path="$1"
  if [[ ! -e "$path" ]]; then
    echo "WARNING: Missing: $path"
    return 1
  fi
  return 0
}

# === Start Promotion ===
echo "=== Promoting Ceph Release: $POINT_RELEASE ($RELEASE) ==="
echo "Dry run      : $DRY_RUN"
echo "Interactive  : $INTERACTIVE"
echo "Prerel base  : $PRERELEASE_BASE"
echo "Prod base    : $PRODUCTION_BASE"
echo

# === Paths to Operate On ===
SYMLINKS_TO_REMOVE=(
  "$PRODUCTION_BASE/debian-$RELEASE"
  "$PRODUCTION_BASE/rpm-$RELEASE"
)

FILES_TO_MOVE=(
  "$PRERELEASE_TARBALLS/ceph-${POINT_RELEASE}.tar.gz:$PRODUCTION_TARBALLS/"
  "$PRERELEASE_TARBALLS/ceph_${POINT_RELEASE}.orig.tar.gz:$PRODUCTION_TARBALLS/"
)

DIRS_TO_MOVE=(
  "$PRERELEASE_BASE/debian-$POINT_RELEASE:$PRODUCTION_BASE/"
  "$PRERELEASE_BASE/rpm-$POINT_RELEASE:$PRODUCTION_BASE/"
  "$PRERELEASE_BASE/debian-$RELEASE:$PRODUCTION_BASE/"
  "$PRERELEASE_BASE/rpm-$RELEASE:$PRODUCTION_BASE/"
)

# === Step 1: Remove old symlinks ===
echo "Step 1: Removing old symlinks"
for path in "${SYMLINKS_TO_REMOVE[@]}"; do
  if check_exists "$path"; then
    confirm_and_run "rm -f '$path'"
  fi
done
echo

# === Step 2: Move tarballs ===
echo "Step 2: Moving tarballs"
for item in "${FILES_TO_MOVE[@]}"; do
  src="${item%%:*}"
  dst="${item##*:}"
  if check_exists "$src"; then
    confirm_and_run "mv '$src' '$dst'"
  fi
done
echo

# === Step 3: Move directories/symlinks ===
echo "Step 3: Moving directories and symlinks"
for item in "${DIRS_TO_MOVE[@]}"; do
  src="${item%%:*}"
  dst="${item##*:}"
  if check_exists "$src"; then
    confirm_and_run "mv '$src' '$dst'"
  fi
done
echo

echo "=== DONE: Ceph release $POINT_RELEASE ($RELEASE) promoted ==="

