#!/bin/sh

# Script to link the configuration of a host to the current directory
# to the root to make access easier
#
# This does not need to be used when flakes is enabled
# Yes need for nvim integration of nixos

cd $HOME/configuration/nixos

directory="hosts"

# List all folder names in the specified directory
echo "Known hosts:"
folders=()
counter=1
for folder in "$directory"/*/; do
  if [ -d "$folder" ]; then
    folder_name=$(basename "$folder")
    folders+=("$folder_name")
    echo "$counter: $folder_name"
    ((counter++))
  fi
done

echo "Please select a host (number):"
read -p "> " folder_number

# Validate the selection
if ! echo "$folder_number" | grep -Eq '^[1-9][0-9]*$'; then
  echo "Invalid selection: You must enter a positive integer."
  exit 1
fi

folder_index=$((folder_number - 1))
if [ "$folder_index" -lt 0 ] || [ "$folder_index" -ge "${#folders[@]}" ]; then
  echo "Invalid selection: Number out of range."
  exit 1
fi

selected_folder="${folders[$folder_index]}"  # Use the 0-based index
selected_folder_path="$directory/$selected_folder"

ln -sf "$selected_folder_path/configuration.nix" "configuration.nix"
echo "Switched to host configuration in the current directory."

