#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/06-Qemu/
# OR
#   /home/*/.gnome2/nautilus-sctipts/06-Qemu/ (deprecated)
#
# FILE:
#   01-cdrom
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Ask for an ISO image and boot up a virtual machine with it.
#
# REQUIREMENTS:
#   bash, zenity, gnome-terminal, file, grep and qemu-system-x86
#
# BUGS:
#   ---
#
# NOTES:
#   Tested on
#   - Debian 8+
#   - Arch Linux
#
# AUTHOR:
#   Patrick Neumann, patrick@neumannsland.de
#
# COMPANY:
#   (privately)
#
# VERSION:
#   0.9 (beta)
#
# LINK TO THE MOST CURRENT VERSION:
#   https://
#
# CREATED:
#   16.11.2019
#
# COPYRIGHT (C):
#   2015-2010 - Patrick Neumann
#
# LICENSE:
#   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 3 of the License, or
#   (at your option) any later version.
#
# WARRANTY:
#   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.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# TODO:
#   - ask for networking?
#
# HISTORY:
#   0.9 - Patrick Neumann - Initial (public) release
#
#===============================================================================

#-------------------------------------------------------------------------------
# Additional supported Distribution(s) (add before Library!).
#-------------------------------------------------------------------------------
SUPPORTED_OSR="arch"

#-------------------------------------------------------------------------------
# Check for library (casualscripter_nautilus-scripts_functions.sh).
#-------------------------------------------------------------------------------
readonly LIBRARY="${0%/*/*}/.casualscripter_nautilus-scripts_functions.sh"
if [ ! -f "${LIBRARY}" ] ; then
  zenity --error \
         --text \
         "ERROR: casualscripter_nautilus-scripts_functions.sh MISSING!"
  exit 1
fi

source "${LIBRARY}"

#-------------------------------------------------------------------------------
# Checks (see library "casualscripter_nautilus-scripts_functions.sh").
#-------------------------------------------------------------------------------
check_dep "${FILE_BIN}" "file"
check_dep "${GREP_BIN}" "grep"
check_dep "${GTERMINAL_BIN}" "gnome-terminal"
check_dep "${SUDO_BIN}" "sudo"
check_dep "${QEMUSYS64_BIN}" "qemu-system-x86"

check_ext "${SOURCE}" "dd"

#-------------------------------------------------------------------------------
# Avoid unintended changes to the Image.
#-------------------------------------------------------------------------------
#check_if_is_xmounted
check_if_pwd_is_used_as_mountpoint

#-------------------------------------------------------------------------------
# Check, if source is not mounted.
#-------------------------------------------------------------------------------
#check_if_is_mounted "${SOURCE}"
check_if_source_is_not_mounted

#-------------------------------------------------------------------------------
# Ask user for an iso image.
#-------------------------------------------------------------------------------
readonly CHOICE="$( ${ZENITY_BIN} --title="Select an .iso image" \
                                  --file-selection )"

# FIX IT: auch MBR/MS-DOS ist möglich!
#if ! ${FILE_BIN}  "${CHOICE}" | ${GREP_BIN}  --extended-regexp "ISO\ 9660.*\(bootable\)" > /dev/null 2>&1 ; then
#  error_exit "please select a booable ISO 9660 image"
#fi

#-------------------------------------------------------------------------------
# Boot up the virtual machine:
# - Enable KVM full virtualization support.
# - KVM processor with all supported host features.
# - Simulate system without SMP (1 CPU and 1 core)
#     (for max compatibility, e. g. Windows XP)
# - Set virtual RAM size to 3072 megabytes (3 GB).
# - Pointer device that uses absolute coordinates without grab the mouse.
# - Standard VGA card with Bochs VBE (VESA 2.0) extensions.
# - Indicate that no network devices should be configured.
# - Boot from CD-ROM first, switch back to default order after reboot.
# - Redirect the "monitor" to standart input/output.
# - Use the RAW image as harddisk.
# - Use the selected ISO image as CD Rom drive.
#-------------------------------------------------------------------------------
${GTERMINAL_BIN} --execute \
  ${QEMUSYS64_BIN} \
    -enable-kvm \
    -cpu host \
    -smp 1,cores=1 \
    -m 3072 \
    -usbdevice tablet \
    -vga std \
    -net none \
    -boot once=d \
    -monitor stdio \
    -drive file="${SOURCE}",index=0,media=disk,format=raw \
    -cdrom "${CHOICE}"

exit 0