#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/04-RAW/
# OR
#   /home/*/.gnome2/nautilus-sctipts/04-RAW/ (deprecated)
#
# FILE:
#   02-mount-partition
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Shows available partitions and mount one of them through a loop device.
#
# REQUIREMENTS:
#   bash, zenity, gksu, coreutils, grep and mount
#
# 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:
#   10.03.2016
#
# 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 (Free)BSD (UFS, ZFS,...)
#   - Support for slices in partitions (Solaris)
#
# 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 "${MKDIR_BIN}" "coreutils"
check_dep "${CUT_BIN}" "coreutils"
check_dep "${GREP_BIN}" "grep"
check_dep "${MOUNT_BIN}" "mount"
check_dep "${PKEXEC_BIN}" "polkit"

check_ext "${SOURCE}" "dd"

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

#-------------------------------------------------------------------------------
# Disable automount in gnome.
#-------------------------------------------------------------------------------
disable_gnome_automount

#-------------------------------------------------------------------------------
# Disable tracker (may interfere with later umount).
#-------------------------------------------------------------------------------
kill_all_tracker_processes

#-------------------------------------------------------------------------------
# Ask user for partition and set/configure offset, sizelimit, partition number
#   and mountpoint.
#-------------------------------------------------------------------------------
readonly PARTITION="$( choose_partition "${SOURCE}" parted )"
OFFSET="${PARTITION#*:}"
readonly OFFSET="${OFFSET%%B:*}"
SIZELIMIT="${PARTITION#*:*:*:}"
readonly SIZELIMIT="${SIZELIMIT%%B:*}"
readonly PARTITION_NUMBER="${PARTITION%%:*}"
readonly MOUNTPOINT="${DIRNAME}/Partition_${PARTITION_NUMBER}"

if [ ! -d "${MOUNTPOINT}" ] ; then
  ${MKDIR_BIN} "${MOUNTPOINT}"
fi

#-------------------------------------------------------------------------------
# Add option "force" for unclean unmounted hfs+ partitions.
#-------------------------------------------------------------------------------
if echo "${PARTITION}" \
| ${CUT_BIN} -d ":" -f 5 \
| ${GREP_BIN} --fixed-strings "hfs+" > /dev/null 2>&1 ; then
  readonly EXTRAS=",force"
fi

#-------------------------------------------------------------------------------
# Add option "recover" (= force) for unclean unmounted ntfs partitions.
#-------------------------------------------------------------------------------
if echo "${PARTITION}" \
| ${CUT_BIN} -d ":" -f 5 \
| ${GREP_BIN} --fixed-strings "ntfs" > /dev/null 2>&1 ; then
  readonly EXTRAS=",recover"
fi

#-------------------------------------------------------------------------------
# Mount source on mountpint with options.
#   - recover for unclean unmounted ntfs partitions,
#   - force for unclean unmounted hfs+ partitions,
#       BUT: ext and xfs don't support it!
#   - sizelimit is necessary for hfs+!
#   - ufs (freebsd) need some more individual options! (unsupported!)
#   - solaris makes some add. probs with partitions in slices! (unsupported!)
#-------------------------------------------------------------------------------
if ${PKEXEC_BIN} ${MOUNT_BIN} \
                 --options loop,rw,offset=${OFFSET},sizelimit=${SIZELIMIT}${EXTRAS} \
                 "${SOURCE}" \
                 "${MOUNTPOINT}" \
                 > /dev/null 2>&1
then
  success "partition ${PARTITION_NUMBER} mounted"
else
  error_exit "mounting partition ${PARTITION_NUMBER} failed"
fi

exit 0