#!/usr/bin/env bash

echo 'Installation steps:'
echo ''
echo '  - Make sure configuration has your SSH key authorized for root so you can change your password, e.g.'
echo '       users.users.root.openssh.authorizedKeys.keys = ['
echo '         "ssh-rsa blahblah"'
echo '       ];'
echo '  - Boot minimal NixOS image on target, e.g. using a USB stick'
echo '  - Do NOT use Ventoy, as it doesnt work on some devices. Use a direct image on a USB stick'
echo '  - On target: Change password with `passwd`'
echo '  - On source: `scp ~/.ssh/authorized_keys nixos@<address>:/home/nixos`'
echo '  - On target: `mkdir -p ~/.ssh; mv ~/authorized_keys ~/.ssh/authorized_keys'
echo '  - Then continue by entering the values below'
echo ''

read -p "Enter IP Address: " ADDRESS

if [[ $ADDRESS =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "IP: ${ADDRESS}"
  echo ""
else
  echo "Invalid IP Address"
  exit
fi

read -p "ARE YOU SURE? This will DESTROY the target (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1

NIX_SSHOPTS=-tt nix run github:nix-community/nixos-anywhere -- --flake ../#homefree nixos@$ADDRESS