#!/bin/bash
# General-purpose deploy: pulls ONLY the files you name as arguments from
# your Gitea repo, backs up whatever they're replacing, applies them,
# reinstalls deps, rebuilds the frontend, and restarts the service.
#
# Usage examples:
#   ./pull-deploy.sh App.jsx
#   ./pull-deploy.sh App.jsx storage.js
#   ./pull-deploy.sh server.js server-package.json
#
# Recognized filenames (anything else is an error, to catch typos):
#   App.jsx               -> frontend/src/App.jsx
#   storage.js             -> frontend/src/storage.js
#   server.js              -> server/server.js
#   server-package.json    -> server/package.json
#   frontend-package.json  -> frontend/package.json
#
# If the Gitea repo is private, generate a token (Settings -> Applications
# -> Generate New Token, "read:repository" scope) and run it like this:
#   GITEA_TOKEN=your_token_here ./pull-deploy.sh App.jsx
set -e

GITEA_BASE="https://git.stickrouter.com/mike/client/raw/branch/main"
GITEA_TOKEN="${GITEA_TOKEN:-}"
APP_DIR=/opt/customer-crm
BACKUP_DIR=/opt/customer-crm-backups
TS=$(date +%Y%m%d_%H%M%S)

if [ "$#" -eq 0 ]; then
  echo "Usage: ./pull-deploy.sh <file1> [file2] [file3] ..."
  echo "Example: ./pull-deploy.sh App.jsx"
  exit 1
fi

mkdir -p "$BACKUP_DIR"

CURL_AUTH=()
if [ -n "$GITEA_TOKEN" ]; then
  CURL_AUTH=(-H "Authorization: token $GITEA_TOKEN")
fi

dest_for() {
  case "$1" in
    App.jsx) echo "frontend/src/App.jsx" ;;
    storage.js) echo "frontend/src/storage.js" ;;
    server.js) echo "server/server.js" ;;
    server-package.json) echo "server/package.json" ;;
    frontend-package.json) echo "frontend/package.json" ;;
    *) echo "" ;;
  esac
}

echo "=== [1/4] Downloading the requested files ==="
DOWNLOADED=()
for name in "$@"; do
  dest=$(dest_for "$name")
  if [ -z "$dest" ]; then
    echo "[ERROR] Unrecognized filename: $name"
    echo "  Valid names: App.jsx, storage.js, server.js, server-package.json, frontend-package.json"
    exit 1
  fi
  tmp="/tmp/$(basename "$name").new"
  if ! curl -fsSL "${CURL_AUTH[@]}" "$GITEA_BASE/$name" -o "$tmp"; then
    echo "[ERROR] Could not download $name from $GITEA_BASE/$name"
    exit 1
  fi
  echo "  $name: downloaded"
  DOWNLOADED+=("$name:$dest:$tmp")
done

echo ""
echo "=== [2/4] Backing up the files being replaced ==="
for entry in "${DOWNLOADED[@]}"; do
  IFS=: read -r name dest tmp <<< "$entry"
  target="$APP_DIR/$dest"
  if [ -f "$target" ]; then
    backup_name=$(echo "$name" | sed 's/\.[^.]*$//')
    ext="${name##*.}"
    cp "$target" "$BACKUP_DIR/${backup_name}_$TS.$ext"
    echo "  $name -> $BACKUP_DIR/${backup_name}_$TS.$ext"
  fi
done

echo ""
echo "=== [3/4] Applying the new files ==="
for entry in "${DOWNLOADED[@]}"; do
  IFS=: read -r name dest tmp <<< "$entry"
  mkdir -p "$(dirname "$APP_DIR/$dest")"
  mv "$tmp" "$APP_DIR/$dest"
  echo "  $name -> $dest"
done

echo ""
echo "=== [4/4] Rebuilding and restarting ==="
cd "$APP_DIR/server"
npm install --omit=dev
cd "$APP_DIR/frontend"
npm install
npm run build
systemctl restart customer-crm
sleep 1
systemctl --no-pager -l status customer-crm | head -10

echo ""
echo "Done. Open your domain in a browser to check it."
