#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/07-export/
# OR
#   /home/*/.gnome2/nautilus-sctipts/07-export/ (deprecated)
#
# FILE:
#   02-PALADIN-with-2nd-hdd
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Ask for an destination and boot PALADIN EDGE for imaging directly into
#     a VMDK.
#
# REQUIREMENTS:
#   bash, zenity, gksu, parted, grep, awk, coreutils, gnome-terminal, sudo
#     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:
#   29.11.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:
#   ---
#
# 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 "${PKEXEC_BIN}" "policykit-1 (Debian) or polkit (Arch)"
check_dep "${PARTED_BIN}" "parted"
check_dep "${GREP_BIN}" "grep"
check_dep "${AWK_BIN}" "awk"
check_dep "${TR_BIN}" "coreutils"
check_dep "${GTERMINAL_BIN}" "gnome-terminal"
check_dep "${SUDO_BIN}" "sudo"
check_dep "${QEMUSYS64_BIN}" "qemu-system-x86"

check_ext "${SOURCE}" "dd|DD|raw|RAW|img|IMG"

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

#-------------------------------------------------------------------------------
# If qemu-img has problems with a forensic image of a defective disk,
#   PALADIN (EDGE 64) often helps:
#
#   https://sumuri.com/product/paladin-edge-64-bit/
#-------------------------------------------------------------------------------
readonly PALADIN_ISO="${HOME}/Downloads/paladin_edge_64.iso"
if [ ! -f "${PALADIN_ISO}" ] ; then
  error_exit "please download https://sumuri.com/product/paladin-edge-64-bit/"
fi

#-------------------------------------------------------------------------------
# Search for unused (external) hdds.
#-------------------------------------------------------------------------------
UNUSED_HDDS=""
for HDD in $( ${PKEXEC_BIN} ${PARTED_BIN} --list --script --machine \
| ${GREP_BIN} --extended-regexp "/dev/sd.:" \
| tr " " "_" ) ; do
  if ! ${GREP_BIN} "${HDD%%:*}" /proc/mounts > /dev/null 2>&1 ; then
    if [ -z "${UNUSED_HDDS}" ] ; then
      UNUSED_HDDS="${HDD}"
    else
      UNUSED_HDDS="${UNUSED_HDDS}"$'\n'"${HDD}"
    fi
  fi
done

if [ -z "${UNUSED_HDDS}" ] ; then
  error_exit "no unused (external) hdd found"
fi

#-------------------------------------------------------------------------------
# Ask user for destination.
#-------------------------------------------------------------------------------
readonly UNUSED_HDDS="$( echo "${UNUSED_HDDS}" \
                         | ${AWK_BIN} 'NR == 1 { print "TRUE", $0; }; NR > 1 { print "FALSE", $0; }' \
                         | ${TR_BIN} "\n" " " )"

CHOICE="$( ${ZENITY_BIN} --list \
                         --text "Please select an unused (external) hdd!" \
                         --radiolist \
                         --column "Pick" \
                         --column "HDDs" \
                         ${UNUSED_HDDS} \
                         --width="400" \
                         --height="400" )"

if [ -z "${CHOICE}" ] ; then
  error_exit "no hdd selected"
fi

CHOICE="${CHOICE%%:*}"

#-------------------------------------------------------------------------------
# Boot up the virtual machine:
# - Enable KVM full virtualization support.
# - KVM processor with all supported host features.
# - Simulate an SMP system with 2 CPUs and 2 cores .
# - 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 1st harddisk.
# - Use unused (externel) hdd as 2nd harddisk.
# - Use PALADIN ISO image as CD Rom drive.
#-------------------------------------------------------------------------------
${GTERMINAL_BIN} -- ${PKEXEC_BIN} \
  ${QEMUSYS64_BIN} \
    -enable-kvm \
    -cpu host \
    -smp 2,cores=2 \
    -m 3072 \
    -usb -device usb-tablet \
    -vga std \
    -net none \
    -boot d \
    -monitor stdio \
    -drive file="${SOURCE}",index=0,media=disk,format=raw \
    -drive file="${CHOICE}",index=1,media=disk,format=raw \
    -cdrom "${PALADIN_ISO}"

exit 0