#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/05c-Linux/
# OR
#   /home/*/.gnome2/nautilus-sctipts/05c-Linux/ (deprecated)
#
# FILE:
#   01_RAW-find-installation-and-version
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Shows/stores partition with linux installation inkl. version.
#
# REQUIREMENTS:
#   bash, zenity, coreutils, policykit-1|polkit, mount, grep and parted
#
# 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:
#   06.06.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").
#-------------------------------------------------------------------------------
# Because The Sleuth Kit does not support XFS and some other rare but important
#   filesystems, the only choice is:
check_dep "${CAT_BIN}" "coreutils"
check_dep "${PKEXEC_BIN}" "policykit-1 (Debian) or polkit (Arch)"
check_dep "${MOUNT_BIN}" "mount"
check_dep "${UMOUNT_BIN}" "mount"
check_dep "${GREP_BIN}" "grep"
check_dep "${MKDIR_BIN}" "coreutils"
check_dep "${PARTED_BIN}" "parted"
check_dep "${CUT_BIN}" "coreutils"
check_dep "${RMDIR_BIN}" "coreutils"

check_ext "${SOURCE}" "dd"

check_tmp

#-------------------------------------------------------------------------------
# Disable automount in gnome.
#-------------------------------------------------------------------------------
disable_gnome_automount
echo 1
#-------------------------------------------------------------------------------
# Disable tracker (may interfere with later umount).
#-------------------------------------------------------------------------------
kill_all_tracker_processes
echo 2
#-------------------------------------------------------------------------------
# Avoid unintended changes to the Image.
#-------------------------------------------------------------------------------
check_if_pwd_is_used_as_mountpoint
echo 3
#-------------------------------------------------------------------------------
# A little bit of configuration before calling the shell script.
#-------------------------------------------------------------------------------
readonly LINUXVERSION="${TMP}/linuxversion.txt"
readonly MOUNTPOINT="${TMP}/singlepartition"
if [ ! -d "${MOUNTPOINT}" ] ; then
  ${MKDIR_BIN} "${MOUNTPOINT}"
fi
echo 4
#-------------------------------------------------------------------------------
# Take over the configuration from the wrapper.
#-------------------------------------------------------------------------------
readonly DISTRIBUTIONFILES="os-release \
debian_version \
slackware-version \
arch-release \
gentoo-release \
fedora-release \
redhat-release \
SuSE-release \
frugalware-release \
altlinux-release \
mandriva-release \
meego-release \
angstrom-version \
mageia-release \
lsb-release"

#-------------------------------------------------------------------------------
# "The" loop...
#-------------------------------------------------------------------------------
while read PARTITION ; do
  if echo "${PARTITION}" | ${GREP_BIN} --extended-regexp "^[[:digit:]]:.*" \
  > /dev/null 2>&1 ; then
    OFFSETB="$( echo "${PARTITION}" | cut -d ":" -f 2 )"
    OFFSET="${OFFSETB%B}"
    if ${PKEXEC_BIN} ${MOUNT_BIN} --options loop,ro,offset="${OFFSET}" "${SOURCE}" "${MOUNTPOINT}" > /dev/null 2>&1 ; then
      if [ -f "${MOUNTPOINT}/fstab" -a -f "${MOUNTPOINT}/hosts" -a -f "${MOUNTPOINT}/passwd" ] ; then
        echo "/etc (partition) of a linux installation found in partition $( echo "${PARTITION}" | ${CUT_BIN} -d ":" -f 1 ):" >> "${LINUXVERSION}"
        for FILE in ${DISTRIBUTIONFILES} ; do
          if [ -f "${MOUNTPOINT}/${FILE}" ] ; then
            echo "${FILE}:">> "${LINUXVERSION}"
            ${CAT_BIN} "${MOUNTPOINT}/${FILE}" >> "${LINUXVERSION}"
            echo >> "${LINUXVERSION}"
          fi
        done
      fi
      if [ -f "${MOUNTPOINT}/etc/fstab" -a -f "${MOUNTPOINT}/etc/hosts" -a -f "${MOUNTPOINT}/etc/passwd" ] ; then
        echo "Linux installation (root) found in partition $( echo "${PARTITION}" | cut -d ":" -f 1 ):" >> "${LINUXVERSION}"
        for FILE in ${DISTRIBUTIONFILES} ; do
          if [ -f "${MOUNTPOINT}/etc/${FILE}" ] ; then
            echo "/etc/${FILE}:" >> "${LINUXVERSION}"
            ${CAT_BIN} "${MOUNTPOINT}/etc/${FILE}" >> "${LINUXVERSION}"
            echo >> "${LINUXVERSION}"
          fi
        done
      fi
      ${PKEXEC_BIN} ${UMOUNT_BIN} "${MOUNTPOINT}" 2>/dev/null
    fi
  fi
done < <( ${PARTED_BIN} --script --machine "${SOURCE}" unit b print )
echo 5
#-------------------------------------------------------------------------------
# Unmount one partition after another...
#-------------------------------------------------------------------------------
while ${GREP_BIN} --fixed-strings "${MOUNTPOINT}" /proc/mounts > /dev/null 2>&1 ; do
  ${PKEXEC_BIN} ${UMOUNT_BIN} "${MOUNTPOINT}" 2>/dev/null
done
echo 6
${RMDIR_BIN} "${MOUNTPOINT}"
echo 7
#-------------------------------------------------------------------------------
# Display content of the resultfile "osxversion.txt".
#-------------------------------------------------------------------------------
display_resultfile "${LINUXVERSION}"

exit 0
