#!/usr/bin/env bash
set -euo pipefail

SRC_DIR="shine-UI"
REMOTE_HOST="${REMOTE_HOST:-player@shineup.me}"
REMOTE_BASE_DIR="${REMOTE_BASE_DIR:-/home/player/SHiNE}"
BUILD_VERSION="$(date -u +%Y%m%d%H%M%S)"
VERSION_FILE="VERSION.properties"
export BUILD_VERSION
TMP_DIR="$(mktemp -d)"
TARGET="${1:-prod}"

if [[ ! -f "$VERSION_FILE" ]]; then
  echo "ERROR: version file not found: $VERSION_FILE" >&2
  exit 1
fi

CLIENT_VERSION="$(sed -n "s/^client\\.version[[:space:]]*=[[:space:]]*//p" "$VERSION_FILE" | head -n 1 | tr -d '\r' | xargs)"
if [[ -z "$CLIENT_VERSION" ]]; then
  echo "ERROR: client.version is empty in $VERSION_FILE" >&2
  exit 1
fi
export CLIENT_VERSION

TARGET_DIR="shine-UI"
TARGET_URL="https://shineup.me"
case "$TARGET" in
  prod|production|main|shineup|shineup.me|shine-UI)
    TARGET_DIR="shine-UI"
    TARGET_URL="https://shineup.me"
    ;;
  ui_1|ui-1|1|shine-UI_1)
    TARGET_DIR="test-UI/shine-UI_1"
    TARGET_URL="https://ui-1.shineup.me"
    ;;
  ui_2|ui-2|2|shine-UI_2)
    TARGET_DIR="test-UI/shine-UI_2"
    TARGET_URL="https://ui-2.shineup.me"
    ;;
  ui_3|ui-3|3|shine-UI_3)
    TARGET_DIR="test-UI/shine-UI_3"
    TARGET_URL="https://ui-3.shineup.me"
    ;;
  ui_drygmira|ui-drygmira|drygmira|shine-UI_drygmira)
    TARGET_DIR="test-UI/shine-UI_drygmira"
    TARGET_URL="https://ui-drygmira.shineup.me"
    ;;
  ui_milana|ui-milana|milana|shine-UI_milana)
    TARGET_DIR="test-UI/shine-UI_milana"
    TARGET_URL="https://ui-milana.shineup.me"
    ;;
  ui_aidar|ui-aidar|aidar|shine-UI_aidar)
    TARGET_DIR="test-UI/shine-UI_aidar"
    TARGET_URL="https://ui-aidar.shineup.me"
    ;;
  *)
    echo "ERROR: unknown target '$TARGET'" >&2
    echo "Available targets: prod, ui_1, ui_2, ui_3, ui_drygmira, ui_milana, ui_aidar" >&2
    exit 1
    ;;
esac
REMOTE_DIR="${REMOTE_BASE_DIR}/${TARGET_DIR}"

cleanup() {
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

if [[ ! -d "$SRC_DIR" ]]; then
  echo "ERROR: source directory not found: $SRC_DIR" >&2
  exit 1
fi

echo "==> Preparing staged UI copy with build version: $BUILD_VERSION"
echo "==> Client version from $VERSION_FILE: $CLIENT_VERSION"
echo "==> Deploy target: $TARGET_URL ($TARGET_DIR)"
rsync -a "$SRC_DIR"/ "$TMP_DIR"/

INDEX_FILE="$TMP_DIR/index.html"
if [[ ! -f "$INDEX_FILE" ]]; then
  echo "ERROR: index.html not found in staged UI: $INDEX_FILE" >&2
  exit 1
fi

perl -0pi -e 's/window\.__SHINE_BUILD_HASH__\s*=\s*'\''[^'\'']*'\'';/window.__SHINE_BUILD_HASH__ = '\''$ENV{BUILD_VERSION}'\'';/' "$INDEX_FILE"
perl -0pi -e 's/window\.__SHINE_CLIENT_VERSION__\s*=\s*'\''[^'\'']*'\'';/window.__SHINE_CLIENT_VERSION__ = '\''$ENV{CLIENT_VERSION}'\'';/' "$INDEX_FILE"

echo "==> Checking SSH connectivity to $REMOTE_HOST"
ssh -o BatchMode=yes -o ConnectTimeout=10 "$REMOTE_HOST" "echo SSH OK" >/dev/null

echo "==> Preparing remote directory: $REMOTE_DIR"
ssh "$REMOTE_HOST" "mkdir -p '$REMOTE_DIR'"

echo "==> Syncing staged files to $REMOTE_DIR"
rsync -avz --delete "$TMP_DIR"/ "$REMOTE_HOST":"$REMOTE_DIR"/

echo "Всё хорошо: $TARGET_URL"
