#!/bin/bash
# File              : dmenu_boottester
# License           : MIT License
# Author            : M. Nabil Adani <macroid48@gmail.com>
# Date              : Minggu, 27/10/2019 09:28 WIB
# Last Modified Date: Sabtu, 09/11/2019 10:25 WIB
# Last Modified By  : M. Nabil Adani <macroid48@gmail.com>


# add this custom action to policykit configurations
# path : /usr/share/polkit-1/actions/org.freedesktop.policykit.qemu-system-x86_64.policy

# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD polkit Policy Configuration 1.0//EN"
# "http://www.freedesktop.org/software/polkit/policyconfig-1.dtd">
# <policyconfig>

#   <vendor>QEMU</vendor>
#   <vendor_url>https://github.com/qemu/QEMU</vendor_url>

#   <action id="org.freedesktop.policykit.pkexec.qemu-system-x86_64">
#     <description>Run QEMU as root</description>
#     <message>Authentication is required to run the QEMU as root</message>
#     <defaults>
#       <allow_any>no</allow_any>
#       <allow_inactive>auth_admin</allow_inactive>
#       <allow_active>auth_admin</allow_active>
#     </defaults>
#     <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/qemu-system-x86_64</annotate>
#     <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
#   </action>

# </policyconfig>


DMENU='dmenu -x 300 -y 220 -w 800 -fn Iosevka-11 -l 5 -i'
choice=$(echo -e "File Iso\nFlashDrive" | $DMENU -p Bootable)

function file_iso(){
  file=$(find /mnt/Data/Sistem\ Operasi -iname '*.iso'|$DMENU -p "File Iso")
  if [[ -f $file ]] ; then
    ram=$(echo -e "64\n128\n512\n1024\n2024" | $DMENU -p Ram\ Size)
    if [[ ! -z $ram ]]; then
      qemu-system-x86_64 -enable-kvm -boot d -m $ram -cdrom "$file"
    fi
  fi
}
function flash_drive(){
  fd=$(udiskie-info -C -2 -a -o '{ui_label}' | $DMENU | cut -d ":" -f 1)
  if [[ ! -z $fd ]]; then
    ram=$(echo -e "64\n128\n512\n1024\n2024" | $DMENU -p Ram\ Size)
    if [[ ! -z $ram ]]; then
      pkexec qemu-system-x86_64 -enable-kvm -m $ram -vga std -hda "$fd"
    fi
  fi
}
case "$choice" in
  "File Iso") file_iso;;
  "FlashDrive") flash_drive;;
esac
