set -eu
cd "$(dirname "${BASH_SOURCE[0]}")"
if [[ "$HOME/Code/swap-watchdog" != "$(pwd)" ]]; then
echo "Warning: this repo is at $(pwd), but the unit files' ExecStart=" >&2
echo "expects %h/Code/swap-watchdog (i.e. $HOME/Code/swap-watchdog)." >&2
echo "Edit systemd/*.service before continuing, or the services won't" >&2
echo "find the binaries." >&2
fi
if ! command -v dagger >/dev/null 2>&1; then
echo "Error: dagger CLI not found. Install it (e.g. 'brew install dagger/tap/dagger')" >&2
echo "and a container runtime (podman or docker) before running this script." >&2
exit 1
fi
echo "Building swap-watchdog, force-quit-gui, and kdotool via Dagger+Nix..."
dagger call build-linux --src=. --go-toolchain="$(go env GOROOT)" export --path=./bin
chmod +x bin/swap-watchdog bin/force-quit-gui bin/kdotool
echo "Installing kdotool to ~/.local/bin (so the systemd service can find it)..."
mkdir -p "$HOME/.local/bin"
cp bin/kdotool "$HOME/.local/bin/kdotool"
echo "Installing unit files..."
mkdir -p "$HOME/.config/systemd/user"
cp systemd/*.service "$HOME/.config/systemd/user/"
systemctl --user daemon-reload
echo "Enabling and starting services..."
systemctl --user enable --now force-quit-gui.service swap-watchdog.service
echo "Done. Logs at ~/.local/state/swap-watchdog/watchdog.log"