#!/usr/bin/env bash
# Deploy the ink-redesigned wellness tracker to the funy.click vhost.
#
# Why this script exists: funy.click serves /var/www/funy.click, a different
# directory from the /var/www/html/wellness-tracker working copy. Only the HTML
# is copied. That vhost's sw.js and manifest.json use ABSOLUTE paths because the
# app is hosted at the domain root, whereas the working copy uses relative paths
# for a subdirectory — overwriting them would break the installed PWA and could
# pull the other apps sharing that root (index, food-picks, overclock) into the
# wellness cache. So those two are patched in place instead.
#
# Safe to re-run.

set -euo pipefail

SRC=/var/www/html/wellness-tracker/wellness-tracker.html
DST_DIR=/var/www/funy.click
TS=$(date -u +%Y%m%dT%H%M%SZ)

[ -f "$SRC" ] || { echo "FAIL: source missing: $SRC" >&2; exit 1; }
[ -d "$DST_DIR" ] || { echo "FAIL: vhost root missing: $DST_DIR" >&2; exit 1; }
grep -q 'ink-paper' "$SRC" || { echo "FAIL: source is not the ink redesign" >&2; exit 1; }

echo "==> backing up (stamp $TS)"
for f in wellness-tracker.html sw.js manifest.json; do
  [ -f "$DST_DIR/$f" ] && cp -p "$DST_DIR/$f" "$DST_DIR/$f.bak-$TS" && echo "    $f"
done

echo "==> copying redesigned HTML"
cp "$SRC" "$DST_DIR/wellness-tracker.html"
chown --reference="$DST_DIR/manifest.json" "$DST_DIR/wellness-tracker.html" 2>/dev/null || true

# Grounding corpus for the Talk tab (built by build-knowledge.py). The vhost sw.js
# keeps its absolute paths, so the asset is spliced into its list and matcher once.
KB="$(dirname "$SRC")/knowledge.json"
if [ -f "$KB" ]; then
  echo "==> copying knowledge.json"
  cp "$KB" "$DST_DIR/knowledge.json"
  grep -q "'/knowledge.json'" "$DST_DIR/sw.js" || sed -i "s#^  '/manifest.json',#&\n  '/knowledge.json',#" "$DST_DIR/sw.js"
  grep -q 'knowledge\\.json' "$DST_DIR/sw.js" || sed -i 's#(manifest\\.json|#(manifest\\.json|knowledge\\.json|#' "$DST_DIR/sw.js"
fi

# Without this, anyone with the PWA installed keeps getting the old build from
# cache forever. The version is read from the working-copy sw.js rather than
# hardcoded: a stale literal here once pointed backwards (v4 while live was v6).
VER=$(grep -oE 'wellness-tracker-v[0-9]+' /var/www/html/wellness-tracker/sw.js | head -1)
[ -n "$VER" ] || { echo "FAIL: cannot read cache version from working-copy sw.js" >&2; exit 1; }
echo "==> bumping service worker cache to $VER"
sed -i "s/wellness-tracker-v[0-9][0-9]*/$VER/" "$DST_DIR/sw.js"

echo "==> repointing manifest colours to the paper ground"
sed -i 's/#0B0D21/#F0EBDE/g' "$DST_DIR/manifest.json"

echo
echo "==> verifying"
fail=0
grep -q 'ink-paper'            "$DST_DIR/wellness-tracker.html" && echo "    OK  html is the ink redesign"       || { echo "    BAD html"; fail=1; }
grep -q 'ink-night'            "$DST_DIR/wellness-tracker.html" && echo "    OK  both skins present"            || { echo "    BAD skins"; fail=1; }
grep -q "$VER"                 "$DST_DIR/sw.js"                 && echo "    OK  sw.js at $VER"                 || { echo "    BAD sw.js"; fail=1; }
grep -q "'/wellness-tracker.html'" "$DST_DIR/sw.js"             && echo "    OK  sw.js absolute paths intact"   || { echo "    BAD sw.js paths"; fail=1; }
if [ -f "$KB" ]; then
  cmp -s "$KB" "$DST_DIR/knowledge.json" && grep -q "'/knowledge.json'" "$DST_DIR/sw.js" && grep -q 'knowledge\\.json' "$DST_DIR/sw.js" \
    && echo "    OK  knowledge.json deployed and precached" || { echo "    BAD knowledge.json"; fail=1; }
fi
grep -q '"scope": "/"'         "$DST_DIR/manifest.json"         && echo "    OK  manifest scope intact"         || { echo "    BAD manifest scope"; fail=1; }
grep -q '#F0EBDE'              "$DST_DIR/manifest.json"         && echo "    OK  manifest colours updated"      || { echo "    BAD manifest colours"; fail=1; }
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 https://funy.click/wellness-tracker.html || echo ERR)
[ "$code" = "200" ] && echo "    OK  https serving 200" || { echo "    BAD https returned $code"; fail=1; }

echo
if [ "$fail" -eq 0 ]; then
  echo "DONE — hard-reload https://funy.click/wellness-tracker.html"
  echo "       (DevTools > Application > Service Workers > Unregister if it looks unchanged)"
else
  echo "PROBLEMS ABOVE — roll back with:"
  echo "  for f in wellness-tracker.html sw.js manifest.json; do cp -p $DST_DIR/\$f.bak-$TS $DST_DIR/\$f; done"
  exit 1
fi
echo
echo "Roll back at any time with:"
echo "  for f in wellness-tracker.html sw.js manifest.json; do cp -p $DST_DIR/\$f.bak-$TS $DST_DIR/\$f; done"
