#!/usr/bin/env bash
set -euo pipefail

project_root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
ssh_root=${SSH_ROOT:-"$HOME/.ssh"}
local_bin_root=${LOCAL_BIN_ROOT:-"$HOME/.local/bin"}

install -d -m 700 "$ssh_root"
install -d -m 700 "$local_bin_root"

"$project_root/tools/generate-configs.py"

install -m 600 "$project_root/generated/client.conf" "$ssh_root/config"

rm -f "$local_bin_root/ssh" "$local_bin_root/scp" "$local_bin_root/sftp"
install -m 700 "$project_root/scripts/ssh-wrapper.sh" "$local_bin_root/ssh"
install -m 755 "$project_root/scripts/scp-wrapper.sh" "$local_bin_root/scp"
install -m 755 "$project_root/scripts/sftp-wrapper.sh" "$local_bin_root/sftp"

rm -f "$ssh_root/scripts/ssh-wrapper.sh" "$ssh_root/scripts/scp-wrapper.sh" "$ssh_root/scripts/sftp-wrapper.sh"
rmdir --ignore-fail-on-non-empty "$ssh_root/scripts" 2>/dev/null || true

printf 'deployed SSH config to %s and wrappers to %s\n' "$ssh_root" "$local_bin_root"
