#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

: "${JOBS:="1"}"

TARGET=deck@172.16.16.250

rsync -av --progress --delete --exclude=.git "$DIR/../../" "${TARGET}:~/.config/mynix"
rsync -av --progress --delete --exclude=.git "/persist/manual/manual_add_to_store" "${TARGET}:~/.persist/manual/"

ssh "${TARGET}" 'source /home/deck/.nix-profile/etc/profile.d/nix.sh && for f in ~/.persist/manual/manual_add_to_store/*; do nix-store --add-fixed sha256 "$f"; done'

ssh "${TARGET}" "source /home/deck/.nix-profile/etc/profile.d/nix.sh && nix run /home/deck/.config/mynix  -- switch -b \$(date +%s).hmbackup --flake /home/deck/.config/mynix --max-jobs $JOBS"

ssh "${TARGET}" 'cat .config/mynix/flake.lock' > "$DIR/../../flake.lock"
