#!/usr/bin/bash
#
# (C) 2025 Axel Konrad <ak-li@siduction.org>
#     2008 Joaquim Boura <x-un-i@berlios.de>
#
# 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 2
# of the License, or (at your option) any later version.
#
# 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 package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# On Debian GNU/Linux systems, the text of the GPL license can be
# found in /usr/share/common-licenses/GPL.
#
#--------------------------------------------------------------------------
# Simple script to umount all partitions before the installer starts his work
# or to return 1 if any partition is mounted and could not be umounted
# called with one parameter ( value does not matter) just inquiries if there 
# is at least one partition mounted returning 1 in this case.
# when only swap is mounted then return 2
#--------------------------------------------------------------------------

umount_all_drives() {
	local ok=0
	local do_it=$1
	
	# Kill any process using /fll/hdinstall. If install-gui is killed,
	# fll-installer and its rsync processes might still be running.
	[ -d /fll/hdinstall ] && fuser -k /fll/hdinstall
	
	local TempFile=$(mktemp -p /tmp/ .XXXXXXXXXX)
	
	# find the device with the default FLL_MOUNTPOINT to exclude it
	local FllDev="/fll/siduction"
	# Will break if there is no FllDev as it will exclude everything
	# There must always be an FLL_MOUNTPOINT to copy from though
	# Could it sanely disagree with /etc/default/distro ?
	mount | grep "^/" | grep -v "/fll/persist" | grep -v "^$FllDev" | cut -d" " -f1,3,5 > "$TempFile"

	while read device mountpoint typ; do 
		case "$typ" in 
		ext2|ext3|ext4|reiserfs|vfat|jfs|xfs|ntfs)
			uuid=$(lsblk -no UUID "$device")
			if [ ! "/fll${device#/dev}" = "$mountpoint" ];  then
				if [ "$do_it" = "check" ]; then 
					ok=1
				else
					if [ ! "/fll/$uuid" = "$mountpoint" ]; then
						umount "$mountpoint"
						ok="$?"
					else
						ok=0
					fi
				fi
			else
				ok=0
				# Caveat: on fromiso we must allow the partition
				# to remain mounted so user is playing russian
				# roulette with his partitions
				#
			fi
			;;	
		auto|udf*)  
			ok=0
			;;# nothing to do skip
		squashfs|iso9660)
			ok=0
			;;
		*)
			ok=255 # unknow reason
		esac
		if [ "$ok" -ne 0 ]; then
			break
		fi
	done < "$TempFile"
	[ -e "$TempFile" ] && rm -f "$TempFile"

	# now handle aktive swap devices 
	swapon --noheadings --show=NAME > "$TempFile"
	if [ "$ok" -eq 0 ] && [ -s "$TempFile" ]; then
		if [ "$do_it" = "check" ]; then
			ok=2
		else
			while read part ; do
				if ! swapoff "$part"; then
					ok=2
					break
				fi
			done < "$TempFile"
		fi
	fi
	[ -e "$TempFile" ] && rm -f "$TempFile "
	
	return $ok
}

if [ $# -eq 1 ] ; then
	par="$1"
else
	par="doit"
fi

umount_all_drives $par
rc="$?"

exit $rc

