#!/usr/bin/env bash
###############################################################################
# mirror.sh — Download the original ZyrexTrack website (the content the APK
#             wrapped) so it can be re-hosted on a new domain as a plain site.
#
# Source (recovered from classes.dex):
#     https://YukiDiablo.github.io/yuki_diablo_public
#
# Produces a fully static, browsable copy under ./site/ with links rewritten
# to work locally / on any domain.
#
# Requires: wget  (fallback: httrack if installed)
# Usage:    ./mirror.sh [SOURCE_URL] [OUTPUT_DIR]
###############################################################################
set -euo pipefail

SRC="${1:-https://YukiDiablo.github.io/yuki_diablo_public/}"
OUT="${2:-site}"

# Normalise: ensure trailing slash so wget treats it as a directory root.
case "$SRC" in
  */) ;;
  *) SRC="$SRC/" ;;
esac

HOST="$(printf '%s' "$SRC" | sed -E 's#^https?://##; s#/.*##')"

echo "=============================================================="
echo " ZyrexTrack site mirror"
echo " Source : $SRC"
echo " Host   : $HOST"
echo " Output : $OUT/"
echo "=============================================================="

if command -v wget >/dev/null 2>&1; then
  echo "[*] Using wget…"
  # --mirror            : recursive, timestamping, infinite depth
  # --convert-links     : make links work offline / on a new domain
  # --adjust-extension  : add .html where needed
  # --page-requisites   : grab css/js/img needed to render each page
  # --no-parent         : stay within the project sub-path
  # --restrict-file-names=windows : safe filenames on all OSes
  # -e robots=off       : ignore robots for your OWN content
  wget \
    --mirror \
    --convert-links \
    --adjust-extension \
    --page-requisites \
    --no-parent \
    --restrict-file-names=windows \
    -e robots=off \
    --user-agent="Mozilla/5.0 (migration-mirror)" \
    --directory-prefix="$OUT.raw" \
    "$SRC" || true

  # wget nests output under host/path — flatten into $OUT/
  SRC_DIR="$OUT.raw/$HOST"
  # Detect the project sub-directory (e.g. yuki_diablo_public)
  SUBPATH="$(printf '%s' "$SRC" | sed -E "s#^https?://$HOST##; s#^/##; s#/$##")"
  if [ -n "$SUBPATH" ] && [ -d "$SRC_DIR/$SUBPATH" ]; then
    SRC_DIR="$SRC_DIR/$SUBPATH"
  fi
  rm -rf "$OUT"
  mkdir -p "$OUT"
  cp -a "$SRC_DIR/." "$OUT/" 2>/dev/null || true
  rm -rf "$OUT.raw"

elif command -v httrack >/dev/null 2>&1; then
  echo "[*] wget not found, using httrack…"
  rm -rf "$OUT"; mkdir -p "$OUT"
  httrack "$SRC" -O "$OUT" "+*$HOST*" -r6 -%v || true

else
  echo "!! Neither wget nor httrack is installed."
  echo "   Install one, e.g.:  sudo apt-get install -y wget"
  exit 1
fi

# Ensure an index exists at the root.
if [ ! -f "$OUT/index.html" ]; then
  FOUND="$(find "$OUT" -maxdepth 2 -iname 'index.html' | head -1 || true)"
  if [ -n "$FOUND" ]; then
    echo "[*] Root index.html missing; copying $FOUND -> $OUT/index.html"
    cp "$FOUND" "$OUT/index.html"
  fi
fi

echo
echo "[✓] Mirror complete. Files in: $OUT/"
echo "    Next: python3 tools/migrate.py --site $OUT --domain https://your-new-domain.com"
