#!/bin/bash

name=$(basename -s .sh $0)

numberofcores=$(grep -c ^processor /proc/cpuinfo)

virt_cpu=$(( $numberofcores/2 ))
smp=$(( $virt_cpu / 2 ))
sockets=$(( $virt_cpu / 2 ))

mem=$(expr $(cat /proc/meminfo | grep "MemAvailable" | awk '{print $2}') / 1024 - $(( 2 * 1024 )) )

if [[ $mem -lt 4096 ]]; then
    mem=4096
fi

if [[ $mem -gt 8192 ]]; then
    mem=8192
fi

fd="$name-OVMF_VARS.fd"

if [ ! -f "./fd/$fd" ]; then
    cp -av -- '/usr/share/edk2/x64/OVMF_VARS.4m.fd' "./fd/$fd"
fi

iso=$(find ../sf -name "*.iso" | grep -i "melawy" | grep -i "netinstall" | tail -n 1)

qcow2="$name.qcow2"

if [ ! -f "./qcow2/$qcow2" ]; then
    qemu-img create -f qcow2 "./qcow2/$qcow2" 200G
fi

echo "CPU: $virt_cpu; MEM: $mem; ISO: $iso; DISK: $qcow2; NVRAM: $fd"

random_num=$((1 + $RANDOM % 100))

ssh_port=$(shuf -i 60000-61000 -n 1)


sys="spice"

if [ $sys == "gtk" ]; then
    display="gtk,full-screen=off,grab-on-hover=on,show-cursor=on,window-close=on"
    audio="pipewire"
elif [ $sys == "sdl" ]; then
    display="sdl,gl=core,show-cursor=on,window-close=on"
    audio="pipewire"
elif [ $sys == "spice" ]; then
    display="spice-app,show-cursor=on"
    audio="spice"
fi

vdaagent="spice"

if [ $vdaagent == "qemu" ]; then
    vda="-device virtio-serial-pci \
    -device virtio-serial \
    -chardev qemu-vdagent,id=vdagent,name=vdagent,clipboard=on \
    -device virtserialport,chardev=vdagent,id=vdagent,name=com.redhat.spice.0
    -chardev qemu-vdagent,name=usbredir,id=usbredirchardev1 \
    -device usb-redir,chardev=usbredirchardev1,id=usbredirdev1,debug=3 \
    -chardev qemu-vdagent,name=usbredir,id=usbredirchardev2 \
    -device usb-redir,chardev=usbredirchardev2,id=usbredirdev2,debug=3 \
    -chardev qemu-vdagent,name=usbredir,id=usbredirchardev3 \
    -device usb-redir,chardev=usbredirchardev3,id=usbredirdev3,debug=3"
elif [ $vdaagent == "spice" ]; then
    vda="-device virtio-serial-pci \
    -device virtio-serial \
    -chardev spicevmc,id=vdagent,name=vdagent \
    -device virtserialport,chardev=vdagent,id=vdagent,name=com.redhat.spice.0 \
    -chardev spicevmc,name=usbredir,id=usbredirchardev1 \
    -device usb-redir,chardev=usbredirchardev1,id=usbredirdev1,debug=3 \
    -chardev spicevmc,name=usbredir,id=usbredirchardev2 \
    -device usb-redir,chardev=usbredirchardev2,id=usbredirdev2,debug=3 \
    -chardev spicevmc,name=usbredir,id=usbredirchardev3 \
    -device usb-redir,chardev=usbredirchardev3,id=usbredirdev3,debug=3"
fi

title="${name}_${iso}_${random_num}_ssh_port_${ssh_port}"


qemu-system-x86_64 \
-name ${title},process=${title},guest=${title},debug-threads=on \
-drive "if=pflash,format=raw,unit=0,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd,read-only=on" \
-drive "if=pflash,format=raw,unit=1,file=./fd/$fd" \
-global "driver=cfi.pflash01,property=secure,value=off" \
-global ICH9-LPC.disable_s3=1 \
-cdrom ${iso} \
-drive file="./qcow2/$qcow2,index=0,media=disk,format=qcow2" \
-boot order=d,menu=on,reboot-timeout=5000 \
-smbios type=0,uefi=on \
-smp $smp,sockets=$sockets,cores=1,threads=1 \
-rtc base=utc,driftfix=slew \
-cpu host,migratable=on \
-m "size=$mem,slots=0,maxmem=$(($mem*1024*1024))" \
-usb \
-usbdevice tablet \
-k en-us \
$vda \
-vga virtio \
-display $display \
-audiodev $audio,id=snd0 \
-device ich9-intel-hda \
-device hda-output,audiodev=snd0 \
-device virtio-net-pci,romfile=,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::${ssh_port}-:22 \
-machine type=pc-q35-8.1,smm=on,accel=kvm,usb=on,vmport=auto,dump-guest-core=off,hpet=off,acpi=on,pcspk-audiodev=snd0 \
-enable-kvm \
-msg timestamp=on

# -netdev user,id=net0 \
# -drive file=shared.img,index=1,media=disk,format=raw \
# -netdev user,id=net0,hostfwd=tcp::60022-:22 \
# -serial stdio
# -m 8192 \
# -display sdl,gl=on,show-cursor=on,window-close=on \
# -full-screen \
# -audiodev pa,id=snd0 \
# -net nic,vlan=0 -net user,vlan=0
# -net nic,vlan=0 -net user,vlan=0 -netdev user,id=user.0 -device e1000,netdev=user.0