#!/usr/bin/env bash

# Get vars
VERSION=$(pveversion | cut -d'/' -f2)

for filename in /etc/proxmox_helper/*.config; do
    # Load Image variables
    # shellcheck source=/dev/null
    . "$filename"
    # Test if variables are set
    if ! [[  $IMAGE_NAME && ${IMAGE_NAME-x} ]]; then
        echo "Please set a IMAGE_NAME in ${filename}"
        exit 1
    fi
    if ! [[  $PROXMOX_ID && ${PROXMOX_ID-x} ]]; then
        echo "Please set a PROXMOX_ID in ${filename}"
        exit 1
    fi
    if ! [[  $TEMPLATE_NAME && ${TEMPLATE_NAME-x} ]]; then
        echo "Please set a TEMPLATE_NAME in ${filename}"
        exit 1
    fi

    # Load proper images
    case ${VERSION} in
        *7.*)
            if ! /usr/bin/wget https://hermes.cps.io/images/"${IMAGE_NAME}".raw.gz -O /tmp/"${IMAGE_NAME}".raw.gz &>/dev/null; then
                echo "[ERROR] Cannot download image"
                exit 1
            fi
            # Unpack the image
            if ! /bin/gunzip /tmp/"${IMAGE_NAME}".raw.gz -f &>/dev/null; then
                echo "[ERROR] Cannot unzip ${IMAGE_NAME}"
                exit 1
            fi
            ;;
        *8.*)
            if ! /usr/bin/wget https://hermes.cps.io/images/"${IMAGE_NAME}".raw.gz -O /tmp/"${IMAGE_NAME}".raw.gz &>/dev/null; then
                echo "[ERROR] Cannot download image"
                exit 1
            fi
            # Unpack the image
            if ! /bin/gunzip /tmp/"${IMAGE_NAME}".raw.gz -f &>/dev/null; then
                echo "[ERROR] Cannot unzip ${IMAGE_NAME}"
                exit 1
            fi
            ;;
        *9.*)
            if ! /usr/bin/wget https://hermes.cps.io/images/"${IMAGE_NAME}".raw.gz -O /tmp/"${IMAGE_NAME}".raw.gz &>/dev/null; then
                echo "[ERROR] Cannot download image"
                exit 1
            fi
            # Unpack the image
            if ! /bin/gunzip /tmp/"${IMAGE_NAME}".raw.gz -f &>/dev/null; then
                echo "[ERROR] Cannot unzip ${IMAGE_NAME}"
                exit 1
            fi
            ;;
        *)
            if ! /usr/bin/wget https://hermes.cps.io/images/"${IMAGE_NAME}".qcow2.gz -O /tmp/"${IMAGE_NAME}".qcow2.gz &>/dev/null; then
                echo "[ERROR] Cannot download image"
                exit 1
            fi
            # Unpack the image
            if ! /bin/gunzip /tmp/"${IMAGE_NAME}".qcow2.gz -f &>/dev/null; then
                echo "[ERROR] Cannot unzip ${IMAGE_NAME}"
                exit 1
            fi
            ;;
    esac

    # Delete template before recreating it and don't care if it isn't there
    /usr/sbin/qm destroy "${PROXMOX_ID}" &>/dev/null || true
    # Create virtual machine here
    if ! /usr/sbin/qm create "${PROXMOX_ID}" --net0 \
        virtio,bridge=vmbr21 \
        --name "${TEMPLATE_NAME}" \
        --scsihw virtio-scsi-pci --ostype l26 --sockets 1 --template 1 \
        --onboot 1 --bootdisk virtio0 &>/dev/null; then
        echo "[ERROR] Template creation failed"
        exit 1
    fi

    case ${VERSION} in
        *5.*)
            # This imports the disk and saves it to the storage of vm as unmanaged
            if ! /usr/sbin/qm importdisk "${PROXMOX_ID}" /tmp/"${IMAGE_NAME}".qcow2 local-lvm &>/dev/null; then
                echo "[ERROR] importdisk failed"
                exit 1
            fi
            ;;
        *7.*)
            # This imports the disk and saves it to the storage of vm as unmanaged
            if ! /usr/sbin/qm importdisk "${PROXMOX_ID}" /tmp/"${IMAGE_NAME}".raw local-zfs &>/dev/null; then
                echo "[ERROR] importdisk failed"
                exit 1
            fi
            ;;
        *8.*)
            # This imports the disk and saves it to the storage of vm as unmanaged
            if ! /usr/sbin/qm importdisk "${PROXMOX_ID}" /tmp/"${IMAGE_NAME}".raw local-zfs &>/dev/null; then
                echo "[ERROR] importdisk failed"
                exit 1
            fi
            ;;
        *9.*)
            # This imports the disk and saves it to the storage of vm as unmanaged
            if ! /usr/sbin/qm importdisk "${PROXMOX_ID}" /tmp/"${IMAGE_NAME}".raw local-zfs &>/dev/null; then
                echo "[ERROR] importdisk failed"
                exit 1
            fi
            ;;
    esac

    case ${VERSION} in
        *7.*)
            # This set's it as a managed device
            if ! /usr/sbin/qm set "${PROXMOX_ID}" --virtio0 local-zfs:vm-"${PROXMOX_ID}"-disk-0 &>/dev/null; then
                echo "[ERROR] Setting the bootdevice failed"
                exit 1
            fi
            ;;
        *8.*)
            # This set's it as a managed device
            if ! /usr/sbin/qm set "${PROXMOX_ID}" --virtio0 local-zfs:vm-"${PROXMOX_ID}"-disk-0 &>/dev/null; then
                echo "[ERROR] Setting the bootdevice failed"
                exit 1
            fi
            ;;
        *9.*)
            # This set's it as a managed device
            if ! /usr/sbin/qm set "${PROXMOX_ID}" --virtio0 local-zfs:vm-"${PROXMOX_ID}"-disk-0 &>/dev/null; then
                echo "[ERROR] Setting the bootdevice failed"
                exit 1
            fi
            ;;
        *)
            # This set's it as managed device
            if ! /usr/sbin/qm set "${PROXMOX_ID}" --virtio0 local-lvm:vm-"${PROXMOX_ID}"-disk-0 &>/dev/null; then
                echo "[ERROR] Setting the bootdevice failed"
                exit 1
            fi
            ;;
    esac


    unset IMAGE_NAME
    unset PROXMOX_ID
    unset TEMPLATE_NAME
done
