#!/bin/bash
# ____  _   _ 
#/ ___|| |_| |__   ___  _ __   ___
#\___ \| __| '_ \ / _ \| '_ \ / _ \
# ___) | |_| | | | (_) | |_) |  __/
#|____/ \__|_| |_|\___/| .__/ \___|
#                      |_|
#

# Associative array to hold the URLs for different distributions
declare -A distros=(
    ["1"]="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
    ["2"]="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
)

# Associative array to hold the names of different distributions
declare -A distro_names=(
    ["1"]="Debian 12 (bookworm)"
    ["2"]="Ubuntu Server 22.04 (jammy)"
)

# Function to download the distribution
function download_distro() {
    local distro_url="$1"
    local filename="proxmox_cloudinit"

    file=$(basename "$distro_url")
    if wget -q "$distro_url" -O "$filename.${file##*.}"; then
        echo "Download completed: $filename.${file##*.}"
    else
        echo "Download failed. Please check the distribution URL and try again."
        exit 1
    fi
    file="$filename.${file##*.}"
}

# Display usage/help message
function display_usage() {
    echo "Usage: proxmox_vms [OPTION]"
    echo "Options:"
    echo "  --distro NUM   Download the specified distribution automatically"
    echo "  -h, --help     Display this help message"
    echo
    echo "Available distributions:"
    for distro_num in "${!distros[@]}"; do
        echo "  $distro_num: ${distro_names[$distro_num]}"
    done
}

# Check if the wget command is available
if ! command -v wget &> /dev/null; then
    echo "Error: wget command not found. Please install wget to use this script."
    exit 1
fi

# Parse command-line arguments using getopts
while getopts ":h-:" opt; do
    case "${opt}" in
        h)
            display_usage
            exit 0
            ;;
        -)
            case "${OPTARG}" in
                distro)
                    # Long option --distro for specifying the distribution number
                    distro_number="${!OPTIND}"
                    ((OPTIND++))
                    ;;
                help)
                    display_usage
                    exit 0
                    ;;
                *)  # Invalid option
                    echo "Invalid option: --${OPTARG}"
                    display_usage
                    exit 1
                    ;;
            esac
            ;;
        *)
            # Invalid option
            echo "Invalid option: -$OPTARG"
            display_usage
            exit 1
            ;;
    esac
done

# If a distro number is provided, download the specified distribution
if [[ -n "$distro_number" ]]; then
    distro_url="${distros[$distro_number]}"
    if [[ -z "$distro_url" ]]; then
        echo "Invalid distribution number. Please choose one of the available options."
        exit 1
    fi
    download_distro "$distro_url"
else
    # If no distro number provided, ask the user interactively
    echo "Available distributions:"
    for distro_num in "${!distros[@]}"; do
        echo "$distro_num: ${distro_names[$distro_num]}"
    done

    read -p "Enter the number corresponding to the distribution you want to install: " choice

    if [[ -z "${distros[$choice]}" ]]; then
        echo "Invalid choice. Please choose one of the available options."
        exit 1
    fi

    download_distro "${distros[$choice]}"
fi

function show_usage() {
  echo "Usage: $0 [--config_file <file.json>]"
  exit 1
}

function create_vm() {
  local vm_id=666
  local vm_name="template"

  qm create $vm_id --name $vm_name --net0 virtio,bridge=vmbr0
  qm importdisk $vm_id $file local-lvm
  qm set $vm_id --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-$vm_id-disk-0
  qm set $vm_id --ide2 local-lvm:cloudinit
  qm set $vm_id --boot c --bootdisk scsi0
  qm set $vm_id --memory 2048 
  qm set $vm_id --balloon 0
  qm set $vm_id --onboot 0 
  qm set $vm_id --cores 2 
  qm set $vm_id --tablet 0 
  qm set $vm_id --agent enabled=1 
  qm set $vm_id --serial0 socket --vga serial0
  qm set $vm_id --ipconfig0 ip=dhcp,ip6=dhcp
  qm set $vm_id --ciuser test --cipassword test
}

create_vm

function make_template() {
    qm template $vm_id
}