#! /bin/bash


if ! command -v stow >/dev/null 2>&1; then
    echo "stow is not installed. Please install it before running this script."
    exit 1
fi


# Collect all sub‑directories into an array
dirs=()
for d in */ ; do
    # Remove trailing slash for clean names
    dir="${d%/}"
    [[ -d "$dir" ]] && dirs+=("$dir")
done

if [ ${#dirs[@]} -eq 0 ]; then
    echo "No modules found."
    exit 1
fi

echo "Available modules:"
for i in "${!dirs[@]}"; do
    printf '  %2d) %s\n' $((i+1)) "${dirs[i]}"
done

# Prompt for a number
while true; do
    read -r -p "Select module to install [1-${#dirs[@]}] (or press Enter to skip): " choice
    # Allow empty input → exit without installing
    if [[ -z "$choice" ]]; then
        echo "No selection made. Bye!"
        exit 0
    fi

    # Check that the choice is an integer within range
    if ! [[ "$choice" =~ ^[1-9][0-9]*$ ]] || (( choice < 1 || choice > ${#dirs[@]} )); then
        echo "Invalid selection. Please enter a number between 1 and ${#dirs[@]}."
        continue
    fi

    selected="${dirs[$((choice-1))]}"
    break
done

echo "Installing $selected"
# Place your actual install logic here, e.g.:
# cd "$selected" && ./install.sh || echo "Failed to install $selected"

exit 0
