#!/bin/bash
# Copies this repo's scripts/ + generated_icons/ onto the TARGET at
# ~/timhyperland/{scripts,generated_icons}, so that everything needed to
# actually switch a theme -- including the wallpaper-app hook installed
# by 19-hook-icon-retint-into-wallpaper-app.sh -- lives ON the target
# and is found there, with no network involved, the moment the user
# switches a wallpaper. See README.md's "Deploying the icon theme to a
# real CachyOS laptop" for exactly what's in those two directories and
# why generated_icons/ is palette-independent (safe to copy once and
# reuse across every future theme switch).
#
# This is the ONE step in the whole pipeline that needs network access
# TO the target (an SSH/rsync connection for --vm, or just a local copy
# for --local if source and destination differ) -- run it once for
# first-time setup, and again only when generated_icons/ actually
# changes (new app icons traced via 09-14/16). It is NOT part of
# switching a theme and switching never re-triggers it; see
# 19-hook-icon-retint-into-wallpaper-app.sh for the part that runs
# on every switch, fully offline.
#
# Usage: 18-deploy-theme-artifacts.sh [--vm|--local]
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source ./00-config.sh

parse_target_flag "$@"
set -- "${PARSED_ARGS[@]+"${PARSED_ARGS[@]}"}"

REPO_ROOT="$(cd .. && pwd)"

echo ">>> Target: ${TARGET}"

if [ "$TARGET" = "local" ]; then
    DEST="$HOME/timhyperland"
    mkdir -p "$DEST"
    if [ "$(cd "$REPO_ROOT" && pwd)" = "$(cd "$DEST" && pwd)" ]; then
        echo ">>> Already running from \$HOME/timhyperland -- nothing to copy."
        exit 0
    fi
    echo ">>> Copying scripts/ + generated_icons/ to ${DEST}"
    rsync -a --exclude '[0-9][0-9][0-9][0-9]-*.txt' --exclude '__pycache__' \
        "$REPO_ROOT/scripts/" "$DEST/scripts/"
    rsync -a "$REPO_ROOT/generated_icons/" "$DEST/generated_icons/"
else
    echo ">>> Copying scripts/ + generated_icons/ to \$HOME/timhyperland on ${VM_IP}"
    ssh "${SSH_USER}@${VM_IP}" "mkdir -p ~/timhyperland"
    rsync -a --exclude '[0-9][0-9][0-9][0-9]-*.txt' --exclude '__pycache__' \
        "$REPO_ROOT/scripts/" "${SSH_USER}@${VM_IP}:timhyperland/scripts/"
    rsync -a "$REPO_ROOT/generated_icons/" "${SSH_USER}@${VM_IP}:timhyperland/generated_icons/"
fi

echo ">>> Done. \$HOME/timhyperland on the target now has everything it needs to switch"
echo "    themes on its own -- run 19-hook-icon-retint-into-wallpaper-app.sh next (once),"
echo "    and no further network access to this host is needed to switch themes."
