#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/06-Qemu/
# OR
#   /home/*/.gnome2/nautilus-sctipts/06-Qemu/ (deprecated)
#
# FILE:
#   04a-osx-cats
#
# 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 and a OS X compatible boot loader.
#
# REQUIREMENTS:
#   bash, zenity, openssl, 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:
#   16.02.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 different port of VNC server
#   - Support for inserting Kon-Boot iso image
#   - Mount 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 "${OPENSSL_BIN}" "openssl"
check_dep "${CUT_BIN}" "coreutils"
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

#-------------------------------------------------------------------------------
# Check if a valid bootloader exists.
#
#-------------------------------------------------------------------------------
# Download newest OS X booloader from:
#   - Enoch (http://www.insanelymac.com/forum/files/file/71-enoch/)
#       supports up to El Capitan (10.11) in virtual machines
#   or
#   - Chimera (http://www.tonymacx86.com/downloads.php?do=file&id=258)
#       supports up to Yosemite (10.10), but only on real hardware
#   or
#   - Chameleon (http://chameleon.osx86.hu/)
#       supports up to Mavericks (10.9) in virutal machines
#-------------------------------------------------------------------------------
BOOTLOADER="${HOME}/Hackintosh/Enoch-rev.2922_bootloader"
if [ ! -f "${BOOTLOADER}" ] ; then
  error_exit "Boot loader not found"
fi

#-------------------------------------------------------------------------------
# Collect additional options.
#-------------------------------------------------------------------------------
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" FALSE "${ASK_CONNECT}" \
                                                     FALSE "${ASK_VNC}" )"

if echo "${SELECTS}" | ${GREP_BIN} --fixed-strings "${ASK_CONNECT}" > /dev/null 2>&1 ; then
  # OS X needs a special network configuration AND a connection to the internet:
  NETWORKING="-netdev user,id=hub0port0 \
              -device e1000-82545em,netdev=hub0port0,id=mac_vnet0"
else
  # ... or no NIC:
  NETWORKING="-net none"
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

#-------------------------------------------------------------------------------
# Boot up the virtual machine:
# - Enable KVM full virtualization support.
# - Intel Core 2 Duo processor.
# - Simulate an SMP system with 2 CPUs and 2 cores (needed for first boot!).
# - Set virtual RAM size to 3072 megabytes (3 GB).
# - Standard PC (Q35 + ICH9, 2009).
# - USB keyboard and mouse.
# - Apple SMC with OSKs (see above!).
# - With bootloader Chameleon or Chimera (see above!).
# - Define Base Board Information.
# - Standard VGA card with Bochs VBE (VESA 2.0) extensions.
# - With or without network.
# - Windowed or VNC mode.
# - Redirect the "monitor" to standart input/output.
# - Use the RAW image as S-ATA harddisk.
#   Alternative IDE support:
#     -device ide-drive,bus=ide.0,drive=Macintosh_HD
#     -drive id=Macintosh_HD,if=none,format=raw,file="${SOURCE}"
#
# - for OS X >10.9 needed:
#   $ sudo su - -c "echo 1 > /sys/module/kvm/parameters/ignore_msrs"
#-------------------------------------------------------------------------------
${GTERMINAL_BIN} --execute \
  ${QEMUSYS64_BIN} \
    -enable-kvm \
    -cpu core2duo \
    -smp 2,cores=2 \
    -m 3072 \
    -machine q35 \
    -usb -device usb-kbd -device usb-mouse \
    -device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
    -kernel "${BOOTLOADER}" \
    -smbios type=2 \
    ${NETWORKING} \
    -vga std \
    ${VNC} \
    -monitor stdio \
    -device ahci,id=ahci0 \
    -drive id=Macintosh_HD,if=none,format=raw,file="${SOURCE}" \
    -device ide-hd,bus=ahci0.0,drive=Macintosh_HD,id=sata-disk0

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

exit 0