#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/06-Qemu/
# OR
#   /home/*/.gnome2/nautilus-sctipts/06-Qemu/ (deprecated)
#
# FILE:
#   02-bios
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Boot up a virtual machine in BIOS mode.
#
# REQUIREMENTS:
#   bash, zenity, gnome-terminal, file, grep, qemu-system-x86, coreutils
#     and vinagre
#
# 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:
#   20.01.2020
#
# COPYRIGHT (C):
#   2015-2020 - 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:
#   - Support for S-ATA, SCSI and SAS in addition to IDE
#   - Mount VirtualBox Guest Additions ISO image
#       and VMware Tools ISO for more flexibility after hand over the vmdk
#
# 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_dep "${SLEEP_BIN}" "coreutils"
check_dep "${VINAGRE_BIN}" "vinagre"

check_ext "${SOURCE}" "dd"

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

#-------------------------------------------------------------------------------
# Check, if source is not mounted.
#-------------------------------------------------------------------------------
check_if_source_is_not_mounted

#-------------------------------------------------------------------------------
# Ideas for some content for the drivers-and-tools.iso...
#   Drivers for Windows guests:
#   - https://www.spice-space.org/download/windows/qxl/qxl-0.1-21/qxl_xp_x86.zip
#   - https://www.spice-space.org/download/windows/qxl/qxl-0.1-24/qxl_w7_x86.zip
#   - https://www.spice-space.org/download/windows/qxl/qxl-0.1-24/qxl_w7_x64.zip
#   - https://www.spice-space.org/download/windows/qxl/qxl-0.1-24/qxl_8k2R2_x64.zip
#   - https://people.redhat.com/~vrozenfe/qxlwddm/qxlwddm-0.12.zip (Windows 8)
#   - https://www.spice-space.org/download/windows/qxl-wddm-dod/qxl-wddm-dod-0.19/spice-qxl-wddm-dod-0.19.zip (Windows 10)
#   Windows tools:
#   - https://technet.microsoft.com/de-de/sysinternals/bb842062.aspx
#   - http://nirsoft.net/utils/index.html
#   GNU/Linux tools (i386, amd64, arm and aarch64):
#   - https://busybox.net
#   - https://matt.ucc.asn.au/dropbear/dropbear.html
#   - https://rsync.samba.org/
#   macOS tools:
#   - ?
#-------------------------------------------------------------------------------
DRIVERSANDTOOLS="${HOME}/Additions/drivers-and-tools.iso"
if [ ! -f "${DRIVERSANDTOOLS}" ] ; then
  error_exit "ISO with drivers and tools not found"
fi

#-------------------------------------------------------------------------------
# Collect additional options.
#-------------------------------------------------------------------------------
readonly ASK_SHAREDFOLDER="Additional device with drivers"
readonly ASK_CONNECT="Connect to the Network"
readonly ASK_VNC="Start QEMU VNC service"
readonly SELECTS="$( ${ZENITY_BIN} --list \
                                   --text "Include some additional options:" \
                                   --checklist \
                                   --column "Pick" \
                                   --column "Option" TRUE "${ASK_SHAREDFOLDER}" \
                                                     FALSE "${ASK_CONNECT}" \
                                                     FALSE "${ASK_VNC}" )"

if echo "${SELECTS}" | ${GREP_BIN} --fixed-strings "${ASK_SHAREDFOLDER}" > /dev/null 2>&1 ; then
  DRIVERS="-cdrom ${DRIVERSANDTOOLS}"
else
  DRIVERS=""
fi

if echo "${SELECTS}" | ${GREP_BIN} --fixed-strings "${ASK_CONNECT}" > /dev/null 2>&1 ; then
  RESTRICT="off"
else
  RESTRICT="on"
fi

if echo "${SELECTS}" | ${GREP_BIN} --fixed-strings "${ASK_VNC}" > /dev/null 2>&1 ; then
  VNC="-k de -vnc :0,tls,password"
else
  VNC=""
fi

case "${OR_ID}${OR_VERSION_ID}" in
  debian8) readonly PXE_ROM="/usr/lib/ipxe/qemu/pxe-rtl8139.rom"
           ;;
  arch)    readonly PXE_ROM="/usr/share/qemu/pxe-rtl8139.rom"
           ;;
  *)       error_exit "sorry, ${OR_PRETTY_NAME} is not supported"
esac

#-------------------------------------------------------------------------------
# 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.
# - QXL paravirtual VGA compatible (VESA 2.0 VBE) graphic card.
# - With an "old" and wide supported network card.
# - Connect to the network if selected.
# - Windowed or VNC mode.
# - Redirect the "monitor" to standart input/output.
# - Use the RAW image as harddisk. (Newer Qemu wants "format=raw"!)
#   e. g.: -drive file="${SOURCE}",index=0,media=disk,format=raw
# - Use shared folder as additional drive. (Newer Qemu wants "format=raw"!)
#-------------------------------------------------------------------------------
${GTERMINAL_BIN} --execute \
  ${QEMUSYS64_BIN} \
    -enable-kvm \
    -cpu host \
    -smp 1,cores=1 \
    -m 3072 \
    -usb -device usb-tablet \
    -vga qxl \
    -device rtl8139,romfile="${PXE_ROM}",netdev=net0 \
    -netdev user,restrict=${RESTRICT},id=net0 \
    ${VNC} \
    -monitor stdio \
    -drive file="${SOURCE}",index=0,media=disk,format=raw \
    ${DRIVERS}

if [ -n "${VNC}" ] ; then
  # give qemu-system some time to bind process to port!
  ${SLEEP_BIN} 1
  ${VINAGRE_BIN} ::5900
fi

exit 0