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

# Debian 12 (bookworm)
distro_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"

filename="proxmox_cloudinit"

wget -q "$distro_url" -O "$filename.${file##*.}"

file="$filename.${file##*.}"

NEXTID=$(pvesh get /cluster/nextid)
vm_id=$NEXTID

vm_name="debian"
vm_ram=2048
vm_core=2
username="test"
password="changeme"
ip4="dhcp"
ip6="dhcp"
balloon=0
onboot=0
tablet=0
agent_enabled=1
hdd_size=10
start_now=0

function display_usage {
    echo "Usage: bash debian [OPTIONS]                      "
    echo "Options:                                          "
    echo "  --name NAME             Set the Name for the VM"
    echo "  --ram RAM               Set the RAM size for the VM"
    echo "  --core CORE             Set the number of CPU cores for the VM"
    echo "  --username USERNAME     Set the username for the VM"
    echo "  --password PASSWORD     Set the password for the VM"
    echo "  --ip4 IP_ADDRESS        Set the IPv4 address for the VM (use 'dhcp' for DHCP)"
    echo "  --ip6 IP_ADDRESS        Set the IPv6 address for the VM (use 'dhcp' for DHCP)"
    echo "  --balloon BALLOON       Set the balloon value for the VM"
    echo "  --onboot ONBOOT         Set the onboot value for the VM"
    echo "  --tablet TABLET         Set the tablet value for the VM"
    echo "  --agent-enabled AGENT   Set the agent enabled value for the VM"
    echo "  --hdd-size HDD_SIZE     Set the size of the VMs HDD in GB"
}

while [[ "$#" -gt 0 ]]; do
    case $1 in
        --name) vm_name="$2"; shift ;;
        --ram) vm_ram="$2"; shift ;;
        --core) vm_core="$2"; shift ;;
        --username) username="$2"; shift ;;
        --password) password="$2"; shift ;;
        --ip4) ip4="$2"; shift ;;
        --ip6) ip6="$2"; shift ;;
        --balloon) balloon="$2"; shift ;;
        --autoboot) onboot="$2"; shift ;;
        --tablet) tablet="$2"; shift ;;
        --agent-enabled) agent_enabled="$2"; shift ;;
        --hdd-size) hdd_size="$2"; shift ;;
        *) echo "Unknown option: $1"; display_usage; exit 1 ;;
    esac
    shift
done

qm create $vm_id  --name $vm_name --memory $vm_ram --core $vm_core --net0 virtio,bridge=vmbr0 >/dev/null
qm importdisk $vm_id $file local-lvm >/dev/null
qm set $vm_id --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-$vm_id-disk-0 >/dev/null
qm set $vm_id --ide2 local-lvm:cloudinit >/dev/null
qm set $vm_id --boot c --bootdisk scsi0 >/dev/null
qm set $vm_id --serial0 socket --vga serial0 >/dev/null
qm set $vm_id --balloon $balloon >/dev/null
qm set $vm_id --onboot $onboot >/dev/null
qm set $vm_id --tablet $tablet >/dev/null
qm set $vm_id --agent enabled=$agent_enabled >/dev/null
qm set $vm_id --ipconfig0 ip=$ip4,ip6=$ip6 >/dev/null
qm set $vm_id --ciuser "$username" --cipassword "$password" >/dev/null
qm resize $vm_id scsi0 +"$hdd_size"G >/dev/null

if [ $start_now -eq 1 ]; then
    qm start $vm_id
fi