#!/usr/bin/env bash
# One-off migration of a rig installed under the working title "junk-rig" to "tersler" (2026-11-10).
#   sudo ~/tersler/install/rename-from-junk-rig.sh      (run from the new repo copy; ~/junk-rig can be deleted afterwards)
# Moves /opt/junk-rig to /opt/tersler (a symlink keeps the old path working for the Strata venv, cmake caches and
# hand-written scripts), /etc/junk-rig to /etc/tersler, renames the services, commands and drop-in files, sets the
# hostname to tersler, and starts the serving engine again (SERVE_ENGINE). Models, builds and state are kept.
set -euo pipefail
[[ $EUID -eq 0 ]] || { echo "run with sudo" >&2; exit 1; }
REPO=$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)
OLD=junk-rig NEW=tersler
[[ -f $REPO/install/$NEW.conf ]] || { echo "not a renamed repo: $REPO" >&2; exit 1; }
[[ -d /opt/$OLD && ! -L /opt/$OLD ]] || { echo "/opt/$OLD is not a directory (already migrated?)" >&2; exit 1; }
log() { printf '\033[1;34m[rename]\033[0m %s\n' "$*"; }

log "stopping and removing the $OLD-* services"
for u in strata llama fan gpu; do
    systemctl disable --now "$OLD-$u.service" 2>/dev/null || true
    rm -f "/etc/systemd/system/$OLD-$u.service"
done
systemctl daemon-reload

log "/opt/$OLD -> /opt/$NEW (+ compatibility symlink)"
mv "/opt/$OLD" "/opt/$NEW"
ln -s "/opt/$NEW" "/opt/$OLD"
for l in current previous; do
    t=$(readlink "/opt/$NEW/$l" || true)
    [[ -n $t ]] && ln -sfn "${t/\/opt\/$OLD\//\/opt\/$NEW\/}" "/opt/$NEW/$l"
done
rm -f "/opt/$NEW/bin/$OLD-"*

log "drop-in files"
mvif() { [[ -e $1 ]] && mv "$1" "$2" && echo "  $1 -> $2" || true; }
mvif /etc/apt/preferences.d/$OLD-no-nvidia-repo-drivers /etc/apt/preferences.d/$NEW-no-nvidia-repo-drivers
mvif /etc/cloud/cloud.cfg.d/99-$OLD.cfg /etc/cloud/cloud.cfg.d/99-$NEW.cfg
mvif /etc/default/grub.d/60-$OLD-console.cfg /etc/default/grub.d/60-$NEW-console.cfg
mvif /etc/default/grub.d/99-$OLD.cfg /etc/default/grub.d/99-$NEW.cfg
mvif /etc/modprobe.d/$OLD-blacklist-nouveau.conf /etc/modprobe.d/$NEW-blacklist-nouveau.conf
mvif /etc/netplan/01-$OLD.yaml /etc/netplan/01-$NEW.yaml
mvif /etc/profile.d/$OLD-cuda.sh /etc/profile.d/$NEW-cuda.sh
mvif /etc/ssh/sshd_config.d/10-$OLD.conf /etc/ssh/sshd_config.d/10-$NEW.conf
rm -f /etc/profile.d/$OLD.sh
sed -i "s/$OLD/$NEW/g" /etc/default/grub.d/*-$NEW*.cfg
# the image's cloud-init has no hostname source left, but make sure it never resets the new name
grep -q '^preserve_hostname:' /etc/cloud/cloud.cfg.d/99-$NEW.cfg 2>/dev/null ||
    echo 'preserve_hostname: true' >> /etc/cloud/cloud.cfg.d/99-$NEW.cfg
update-grub 2>/dev/null

log "hostname $NEW"
hostnamectl set-hostname "$NEW"
sed -i "/^127\.0\.1\.1 /d" /etc/hosts
echo "127.0.1.1 $NEW" >> /etc/hosts
systemctl restart avahi-daemon 2>/dev/null || true

log "config, commands and services under the new name"
"$REPO/install/install.sh" config gpu services
install -m 0644 "$REPO/install/strata/flash-next-p100.json" "/opt/$NEW/strata/flash-next-p100.json"
rm -rf "/etc/$OLD"
# shellcheck source=tersler.conf
source "/etc/$NEW/$NEW.conf"; [[ -f /etc/$NEW/$NEW.local.conf ]] && source "/etc/$NEW/$NEW.local.conf"
[[ ${FAN_CONTROL:-0} == 1 ]] && systemctl start "$NEW-fan.service"
engine=llama; [[ ${SERVE_ENGINE:-llama} == strata ]] && engine=strata
systemctl start "$NEW-$engine.service"
log "done: $NEW-$engine starting (journalctl -u $NEW-$engine -f). Reach the rig as $NEW.local from now on."
log "the old repo copy ~${SUDO_USER:-}/$OLD is no longer used; delete it when you are happy"
