set -ex

_user=alfoldi

_hostname=nokia-l13g4-sisu
_port=22

# _hostname=localhost
# _port=12222

# _ssh=alfoldi@nokia-l13g4-sisu
# _ssh="-e 'ssh -p 12222' alfoldi@localhost"
echo "
Host nokia-sisu
  HostName $_hostname
  User $_user
  Port $_port
" >/tmp/sisu.ssh

function my_rsync() {
  rsync -avzP -e "ssh -F /tmp/sisu.ssh" "$@"
}

ssh -qF /tmp/sisu.ssh nokia-sisu "bash -c '
sudo mkdir -p /persists/sops/age/
sudo chown $USER -R /persists/sops/age/
'"

my_rsync --mkpath ~/.config/sops/age/keys.txt nokia-sisu:/persists/sops/age/
my_rsync ~/.ssh/ nokia-sisu:~/.ssh

ssh -qF /tmp/sisu.ssh nokia-sisu <<'EOF'
mkdir -p ~/workspace/home/nix
cd ~/workspace/home/nix
git clone git@github.com:alfonzso/home-manager.git || true
cd home-manager
sudo bash -c 'echo "trusted-users = root $SUDO_USER" >> /etc/nix/nix.conf'
sudo systemctl restart nix-daemon.service
home-manager --extra-experimental-features 'flakes nix-command' --flake .#nokia switch -b backup
EOF
