#!/bin/bash
# Packs everything needed to switch themes on another CachyOS machine --
# scripts/ (00-config.sh, 06-19, lib/*.py) + generated_icons/ (the
# palette-independent, pre-traced/duotone-tagged icon cache) -- into a
# SINGLE self-extracting installer script, so testing this on a real
# laptop is "copy one file over, run it" instead of juggling two
# directories and a separate rsync/ssh step.
#
# This only needs to run on a dev host with the two source directories
# present (this repo). The installer it produces needs no network, no
# SSH, and no knowledge of this host -- scp/USB stick/anything gets it
# onto the laptop, and running it there does the rest locally (same
# "18 is the only step that needs network, switching itself never does"
# split documented in README.md -- this just replaces 18's rsync with a
# single portable file for a machine that isn't reachable over SSH from
# here).
#
# What the installer does when run ON THE LAPTOP:
#   1. Extracts scripts/ + generated_icons/ to ~/timhyperland/ (or the
#      directory given as its own first argument).
#   2. Runs 19-hook-icon-retint-into-wallpaper-app.sh --local, so ML4W's
#      own wallpaper picker (Super+Ctrl+W) re-tints icons too, not just
#      06-switch-wallpaper.sh.
# It's safe to re-run (extraction overwrites in place, the hook it runs
# is idempotent) -- same installer doubles as the "repair the hook after
# an ML4W reinstall" tool, see 19's header for why that's sometimes needed.
#
# Usage (on this dev host): ./deploy.sh [output-path]
#   Default output-path: ./timhyperland-deploy.sh
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"

OUT="${1:-./timhyperland-deploy.sh}"

for tool in rsync tar base64; do
    if ! command -v "$tool" >/dev/null 2>&1; then
        echo "Missing required tool: $tool" >&2
        exit 1
    fi
done
if [ ! -f generated_icons/manifest.json ]; then
    echo "generated_icons/manifest.json not found -- run the icon generation" >&2
    echo "steps (09-14/16) first; deploy.sh packs that cache, it doesn't build it." >&2
    exit 1
fi

WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

echo ">>> [1/3] Collecting scripts/ + generated_icons/ (excluding VM-provisioning-only"
echo "    01-05, session transcripts, and __pycache__)"
mkdir -p "$WORK/payload/scripts"
rsync -a \
    --exclude '0[1-5]-*.sh' \
    --exclude '[0-9][0-9][0-9][0-9]-*.txt' \
    --exclude '__pycache__' \
    scripts/ "$WORK/payload/scripts/"
rsync -a generated_icons/ "$WORK/payload/generated_icons/"

echo ">>> [2/3] Building the tar.gz payload"
TARBALL="$WORK/payload.tar.gz"
tar czf "$TARBALL" -C "$WORK/payload" scripts generated_icons
PAYLOAD_SIZE=$(du -h "$TARBALL" | cut -f1)

echo ">>> [3/3] Writing self-extracting installer to ${OUT}"
{
    cat <<'INSTALLER_HEADER'
#!/bin/bash
# Self-extracting timhyperland icon-theming installer -- see deploy.sh
# (in the repo this was built from) for how this file was produced.
#
# Usage: ./timhyperland-deploy.sh [dest-dir]
#   dest-dir defaults to $HOME/timhyperland. Extracts scripts/ +
#   generated_icons/ there, then runs
#   scripts/19-hook-icon-retint-into-wallpaper-app.sh --local so ML4W's
#   native wallpaper picker (Super+Ctrl+W) re-tints icons too. Needs no
#   network and no SSH -- everything it needs is embedded in this file.
#   Safe to re-run (e.g. after an ML4W reinstall reverts the hook).
set -euo pipefail

MARKER_LINE=$(grep -n '^__TIMHYPERLAND_PAYLOAD_BELOW__$' "$0" | head -1 | cut -d: -f1)
if [ -z "$MARKER_LINE" ]; then
    echo "Corrupt installer: no payload marker found in $0" >&2
    exit 1
fi

DEST="${1:-$HOME/timhyperland}"
mkdir -p "$DEST"
echo ">>> Extracting scripts/ + generated_icons/ to ${DEST}"
tail -n +$((MARKER_LINE + 1)) "$0" | base64 -d | tar xzf - -C "$DEST"

echo ">>> Running scripts/19-hook-icon-retint-into-wallpaper-app.sh --local"
if [ "$DEST" != "$HOME/timhyperland" ]; then
    echo "NOTE: 19's inserted hook always calls \$HOME/timhyperland/scripts/..." >&2
    echo "      explicitly -- using a non-default dest-dir means that hook" >&2
    echo "      will point at the wrong place. Re-run with no argument (or" >&2
    echo "      symlink \$HOME/timhyperland -> ${DEST}) before switching themes." >&2
fi
"$DEST/scripts/19-hook-icon-retint-into-wallpaper-app.sh" --local

echo ">>> Done. Switch a wallpaper (Super+Ctrl+W, or"
echo "    ${DEST}/scripts/06-switch-wallpaper.sh --local) to test -- icons"
echo "    should re-tint to match, with no network involved from here on."
exit 0
__TIMHYPERLAND_PAYLOAD_BELOW__
INSTALLER_HEADER
    base64 -w0 "$TARBALL"
    echo
} > "$OUT"
chmod +x "$OUT"

echo ">>> Built ${OUT} (payload: ${PAYLOAD_SIZE})"
echo ">>> Copy it to the laptop (scp, USB stick, anything) and run it there:"
echo "      ./$(basename "$OUT")"
