#!/bin/bash
#
# Copyright (C) 2019 PCLinuxOS
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#
Check="/usr/bin/VBoxSDL"
if [ ! -f "$Check" ]; then
yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --title="ERROR" --text "ERROR ... failed to find a valid VirtualBox installation.\n" --center --buttons-layout=center --button=Quit:0
exit
fi

Vname=$(cat /sys/class/dmi/id/product_name)
if [ "$Vname" == "VirtualBox" ]; then
yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --title="ERROR" --text "ERROR ... cannot run inside VirtualBox.\n" --center --buttons-layout=center --button=Quit:0
exit
fi

testiso() {
ISO=""
Ram=""
Con=$(ifconfig | grep "eth\|wlan" | cut -d " " -f1)
Nic=$(echo $Con | sed 's| |\||g')
Cpu="$(nproc)"
Mode=""
Result=$(yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --width=500 --center --title="Boot your ISO file in VirtualBox" --text="          Set up the options to boot your ISO:   " \
--image="/usr/share/icons/mini/xblack-bull.png" \
--form \
--item-separator=" " \
--item-separator="\|" \
--field="Select number of Cores to use":NUM \
--field="Select boot mode - UEFI or BIOS":CB \
--field="Select the amount of RAM":CB \
--field="Select wired or wireless connection":CB \
--field="Select the ISO type here":CB \
--field="Select your ISO file here":FL \
 "1\|1..$Cpu" "efi64|bios" "2048|1024|4096" "$Nic" "Linux_64|Linux_32|Windows_64|Windows_32|other" "")
if [ "$?" = "1" ]; then
launch 
fi
Cpu=$(echo $Result | cut -d "|" -f1)
Mode=$(echo $Result | cut -d "|" -f2)
Ram=$(echo $Result | cut -d "|" -f3)
Nic=$(echo $Result | cut -d "|" -f4)
OStype=$(echo $Result | cut -d "|" -f5)
ISO=$(echo $Result | cut -d "|" -f6)
#ISO=$(echo $Result | cut -d "|" -f6 | sed 's/^.......//')
# The Virtual Box commands
VBoxManage createvm --name TestISO --ostype "$OStype" --register
VBoxManage modifyvm TestISO --memory $Ram --vram 128
VBoxManage modifyvm TestISO --nic1 bridged --bridgeadapter1 $Nic
VBoxManage modifyvm TestISO --cpus $Cpu
VBoxManage modifyvm TestISO --graphicscontroller VMSVGA
VBoxManage modifyvm TestISO --firmware $Mode
VBoxManage modifyvm TestISO --mouse usbtablet --keyboard usb --audiocontroller ac97 --audio pulse --audioout on --acpi on --usbehci on
VBoxManage storagectl TestISO  --name "IDE Controller" --add ide
VBoxManage storageattach TestISO --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium $ISO
VBoxSDL --startvm TestISO
VBoxManage unregistervm TestISO --delete
launch
}

testusb() {
cpu=""
Cpus=$(nproc)
Con=$(ifconfig | grep "eth\|wlan" | cut -d " " -f1); Conn=$(echo $Con | sed 's| |\||g')
Avail=$(lsblk -lo name,tran,rm | grep "usb     1" | cut -d " " -f1 | sed 's|sd|/dev/sd|g' | sed 's|/dev| \| /dev|g')
if [ "$Avail" =  "" ]; then
yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --text "ERROR#1 ... failed to find a valid USB device to boot.\n" --center --buttons-layout=center --button=yad-cancel:0
launch 
fi
Avail2=$(echo $Avail | cut -d " " -f2,4,6,8,10)
Details=$(fdisk -l $Avail2 > $HOME/tmp/UsB; cat $HOME/tmp/UsB | cut -d "," -f1 | grep -B1 model | sed 's|Disk model||g' | sed 's|--|\||g' | sed 's|Mass-Storage|SD-Card|g' | cut -d " " -f2)
rm -f $HOME/tmp/UsB
Result=$(yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --width=500 --center --title="Boot your LiveUSB in VirtualBox" --text="                      Set up the options to boot your liveUSB:" \
--image="/usr/share/icons/mini/xblack-bull.png" \
--form \
--item-separator=" " \
--item-separator="\|" \
--field="Select number of Cores to use":NUM \
--field="Select boot mode - UEFI or BIOS":CB \
--field="Select the amount of RAM":CB \
--field="Select wired or wireless connection":CBE \
--field="Select the USB drive to boot":CB \
 "1\|1..$Cpus" "efi64|bios" "2048|1024|4096" "$Conn" "`echo $Details`")
if [ "$?" = "1" ]; then
launch 
fi
cpu=$(echo $Result | cut -d "|" -f1)
Mode=$(echo $Result | cut -d "|" -f2)
Ram=$(echo $Result | cut -d "|" -f3)
Conn=$(echo $Result | cut -d "|" -f4)
stick=$(echo $Result | cut -d "|" -f5 | cut -d ":" -f1 | sed 's|Disk ||'| sed 's|/dev/sd||' | sed -e 's/^[ \t]*//')
if [ "$stick" = "(null)" ]; then
yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --text "ERROR ... failure to find a valid USB device to boot.\n" --center --buttons-layout=center --button=yad-cancel:0
launch 
fi
# The Virtual Box commands
VBoxManage internalcommands createrawvmdk -filename $HOME/usb$stick.vmdk -rawdisk /dev/sd$stick
VBoxManage createvm --name usb$stick --ostype "Other_64" --register
VBoxManage modifyvm usb$stick --memory $Ram --vram 128
VBoxManage modifyvm usb$stick --nic1 bridged --bridgeadapter1 $Conn
VBoxManage modifyvm usb$stick --cpus $cpu
VBoxManage modifyvm usb$stick --graphicscontroller VMSVGA
VBoxManage modifyvm usb$stick --firmware $Mode
VBoxManage modifyvm usb$stick --mouse usbtablet --keyboard usb --audiocontroller ac97 --audio pulse --audioout on --acpi on --usbehci on
VBoxManage storagectl usb$stick  --name "IDE Controller" --add ide
VBoxManage storageattach usb$stick  --storagectl "IDE Controller"  --port 0 --device 0 --type hdd --medium $HOME/usb$stick.vmdk
VBoxSDL --startvm usb$stick
VBoxManage unregistervm usb$stick --delete
rm -f $HOME/usb$stick.vmdk
launch
}
launch() {
yad --window-icon=/opt/VirtualBox/icons/scalable/virtualbox.svg --image="/usr/share/icons/mini/xblack-bull.png" --title="VirtualBox    LiveBoot" --form --on-top --center --text-align=center --text=" Use Vbox to boot an ISO File or a USB Device   " --button="ISO File":0 --button="Close":1 --button="USB Device":2 --buttons-layout=center
Ret="$?"

if [[ $Ret -eq 0 ]]; then testiso
elif [[ $Ret -eq 2 ]]; then testusb
else exit
fi
}

launch
exit
