#!/bin/bash

set -e

LOG() {
    echo $(date "+%Y-%m-%d %H-%M-%S") tomedo.voice Installer "$@"
}

usage() {
    UNKNOWN=""
    if [[ ${#ARGS[@]} -gt 0 ]]; then
        UNKNOWN="""
    unknown argument(s): ${ARGS[@]}
        """
    fi
    cat <<-_EOF_
    $UNKNOWN
    usage: install_tomedo_voice.sh [OPTIONS]

    OPTIONS are:
        -b | --base-folder <folder>     Base folder to store VM data (default: $BASE_FOLDER)
        -d | --iso-download-url <url>   download url for tomedo.voice iso (default: $ISO_DOWNLOAD_URL)
        -f | --force                    force new iso download from server (ignores --iso option)
        -i | --iso <iso-file>           use pre-downloaded iso
        -h | --help                     show this help
        -l | --license-file <licence>   set license file (required)
        -m | --machine-name <name>      set the virtial machine name (default: $MACHINE_NAME)
        -w | --worker <number>          set number of worker (default: $WORKER)

_EOF_
}

####################################
#     configurable Parameters      #
####################################

BASE_FOLDER=$(pwd)
FORCE_DOWNLOAD=
ISO=
ISO_DOWNLOAD_URL=https://apt.zollsoft.net/tomedo.voice.iso
LICENSE_FILE=
MACHINE_NAME=tomedo.voice
WORKER=4

####################################
#          fix parameters          #
####################################

IDE_CONTROLLER_NAME="IDEController"
SATA_CONTROLLER_NAME="SATAController"
UUID=

if [[ $(uname) == "Darwin" ]]; then
    UUID=$(uuidgen)
else
    LOG "this scipt only runs on MacOS"
    exit 1
fi

while [ "$1" != "" ]; do
    case $1 in
    -b | --base-folder)
        shift
        BASE_FOLDER=$1
        ;;
    -d | --iso-download-url)
        shift
        ISO_DOWNLOAD_URL=$1
        ;;
    -f | --force)
        FORCE_DOWNLOAD="true"
        ;;
    -h | --help)
        usage
        exit 0
        ;;
    -i | --iso)
        shift
        ISO=$1
        ;;
    -l | --license-file)
        shift
        LICENSE_FILE=$1
        ;;
    -w | --worker)
        shift
        WORKER=$1
        ;;
    *)
        ARGS+=($1)
        ;;
    esac
    shift
done

if [[ ${#ARGS[@]} -gt 0 ]]; then
    usage
    exit 1
fi

if [[ -z $LICENSE_FILE ]]; then
    cat <<-_EOF_
    
    !!!ATTENTION!!!

    A license file is reqiured to run the tomedo.voice installation.
    Please provide the license by adding the -l or --license parameter!

    For more information on possible parameters please run
    
    $(pwd)/install_tomedo_voice.sh --help

_EOF_

    exit 1
fi

if [[ -z $ISO ]]; then
    ISO=$BASE_FOLDER/tomedo.voice.iso
fi

LOG "installing tomedo.voice with following configuration"

LOG "/-------------------------------------------------------------------------------------\\"
LOG " "
LOG "  BASE_FOLDER            $BASE_FOLDER"
LOG "  FORCE_DOWNLOAD         $FORCE_DOWNLOAD"
LOG "  ISO                    $ISO"
LOG "  LICENSE_FILE           $LICENSE_FILE"
LOG "  MACHINE_NAME           $MACHINE_NAME"
LOG "  WORKER                 $WORKER"
LOG "  IDE_CONTROLLER_NAME    $IDE_CONTROLLER_NAME"
LOG "  SATA_CONTROLLER_NAME   $SATA_CONTROLLER_NAME"
LOG "  UUID                   $UUID"
LOG " "
LOG "\\-------------------------------------------------------------------------------------/"

LOG "checking iso"
if [[ ! -f $ISO || -n $FORCE_DOWNLOAD ]]; then
    if [[ -f $ISO ]]; then
        LOG "removing old iso file"
        rm -f $ISO
    fi

    LOG "downloading new iso"
    curl $ISO_DOWNLOAD_URL --output $ISO
else
    LOG "found local iso"
fi

#Create VM
LOG "creating new VM instance"
VBoxManage createvm --name $MACHINE_NAME --ostype "Debian_64" --register --uuid $UUID --basefolder $BASE_FOLDER

LOG "setting basic VM configurations"
VBoxManage modifyvm $UUID --ioapic on --acpi on --vram 32 --hwvirtex on --graphicscontroller vmsvga
VBoxManage modifyvm $UUID --memory 8192 --cpus 4

LOG "setting network configuration"
VBoxManage modifyvm $UUID --nic1 nat 
LOG "Adding tomedo port forwarding"
VBoxManage modifyvm $UUID --natpf1 "tomedo,tcp,,8080,,8080"
VBoxManage modifyvm $UUID --natpf1 "server-tools,tcp,,8085,,8085"
LOG "Adding tomedo.voice port forwarding"
VBoxManage modifyvm $UUID --natpf1 "tomedo.voice,tcp,,8888,,8888"
LOG "Adding ssh port forwarding"
VBoxManage modifyvm $UUID --natpf1 "ssh,tcp,,2222,,22"

#Create Disk and connect Debian Iso
LOG "Creating hdd"
VBoxManage createmedium disk --filename $BASE_FOLDER/$MACHINE_NAME/disk.vdi --size 50000 --format VDI

LOG "Adding controller and media"
VBoxManage storagectl $UUID --name $SATA_CONTROLLER_NAME --add sata --controller IntelAhci
VBoxManage storageattach $UUID --storagectl $SATA_CONTROLLER_NAME --port 0 --device 0 --type hdd --medium $BASE_FOLDER/$MACHINE_NAME/disk.vdi
VBoxManage storagectl $UUID --name $IDE_CONTROLLER_NAME --add ide --controller PIIX4
VBoxManage storageattach $UUID --storagectl $IDE_CONTROLLER_NAME --port 1 --device 0 --type dvddrive --medium $ISO

LOG "changing boot order"
VBoxManage modifyvm $UUID --boot1 disk --boot2 dvd --boot3 none --boot4 none

#Adding shared folder
if [[ ! -d $BASE_FOLDER/data ]]; then
    LOG "adding shared folder"
    mkdir -p $BASE_FOLDER/data
fi

VBoxManage sharedfolder add $UUID --name "Data" --hostpath $BASE_FOLDER/data --automount --auto-mount-point /media/data

#Enable RDP
# VBoxManage modifyvm $UUID --vrde on
# VBoxManage modifyvm $UUID --vrdemulticon on --vrdeport 10001

#Start the VM
LOG "starting VM"
VBoxHeadless --startvm $UUID &
