#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/07-export/
# OR
#   /home/*/.gnome2/nautilus-sctipts/07-export/ (deprecated)
#
# FILE:
#   01-qemu-img-convert
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Convert a RAW image to a virtual disk image (qcow2, vmdk, vdi, ...).
#
# REQUIREMENTS:
#   bash, zenity, qemu-utils, coreutils and gnome-terminal
#
# 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 VERSIONS:
#   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!).
#-------------------------------------------------------------------------------
# --geometry is no longer supported by gnome-terminal!
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 "${BASENAME_BIN}" "coreutils"
check_dep "${QEMUIMG_BIN}" "qemu-utils"
check_dep "${GTERMINAL_BIN}" "gnome-terminal"
check_dep "${SLEEP_BIN}" "coreutils"
check_dep "${PGREP_BIN}" "procps"

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

#-------------------------------------------------------------------------------
# ... configuration...
#-------------------------------------------------------------------------------
FORMAT="$( ${ZENITY_BIN} --list \
                         --text "Please select the output image format!" \
                         --radiolist \
                         --column "" \
                         --column "Output image formats (-O):" TRUE qcow2 FALSE vdi FALSE vmdk FALSE vpc \
                         --width="320" \
                         --height="320" )"

if [ "${FORMAT}" = "qcow2" ] ; then
  readonly COMPRESSION=" -c"
else
  readonly COMPRESSION=""
fi

TARGET="$( ${BASENAME_BIN} "${SOURCE}" )"
readonly TARGET="${DIRNAME}/${TARGET%%.dd}.${FORMAT}"
readonly COMMAND="${QEMUIMG_BIN} convert -p -f raw -O ${FORMAT}${COMPRESSION} ${SOURCE} ${TARGET}"

#-------------------------------------------------------------------------------
# ... converting...
#-------------------------------------------------------------------------------
${GTERMINAL_BIN} --hide-menubar \
                 --geometry=40x2 \
                 --execute ${BASH_BIN} -c "${COMMAND}"

${SLEEP_BIN} 3

while ${PGREP_BIN} --full "${COMMAND}" > /dev/null 2>&1; do
  ${SLEEP_BIN} 1
done

#-------------------------------------------------------------------------------
# ... give the user some feedback.
#-------------------------------------------------------------------------------
if [ -f "${TARGET}" ] ; then
  success "converting done"
else
  error_exit "converting failed"
fi

exit 0