#!/bin/bash
#
# (C) 2025      Axel Konrad <ak-li@siduction.org>
#     2008-2010 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.
#
#--------------------------------------------------------------------------

if [ "$DEBUG" = "TRUE" ]; then
	set -x
fi

# $1  program to use to make the partitions
# $2  disk that will partitioned

PROG=$1
DISK=$2

PARTITION_BEFORE="$(mktemp -p /tmp/ .partition_before.XXXXXXXXXX)"

PARTITION_AFTER="$(mktemp -p /tmp/ .partition_after.XXXXXXXXXX)"

#-------------------------------------------------
# 1. save old partition table into file b4
#-------------------------------------------------
fdisk -l "$DISK" | grep "^${DISK}" > $PARTITION_BEFORE
#
#-------------------------------------------------
# 2. call partiton program
#-------------------------------------------------
${PROG} ${DISK}
#
#-------------------------------------------------
# 3. get the new partition save it into after
#-------------------------------------------------
fdisk -l "$DISK" | grep "^${DISK}" > $PARTITION_AFTER
#
# Only go on if both files exist
# take care when PARTITION_BEFORE was empty
# otherwise deny any work 
#-------------------------------------------------
# 4. now compare and if there are changes format
# the "newly" created partitions
#-------------------------------------------------
#
if [ -s $PARTITION_AFTER ] && [ -s $PARTITION_BEFORE ]; then
	while read line; do
		part_after=$(cut -d" " -f1 <<< "$line")
		line_before=$(grep -w "$part_after" "$PARTITION_BEFORE")
		
		# We use the PARTTYPE UUID
		if [ ! "$line" = "$line_before" ]; then
			case $(lsblk -n -o PARTTYPE "$part_after") in

			# Linux filesystem
			0fc63daf-8483-4772-8e79-3d69d8477de4)
				echo "Formatting the partition $part with ext4."
				mkfs.ext4 -F "$part" &>/dev/null
				;;

			# EFI System
			c12a7328-f81f-11d2-ba4b-00a0c93ec93b)
				echo "Formatting the partition $part (ESP) with vfat."
				mkfs.vfat "$part" &>/dev/null
				;;

			# Linux swap
			0657fd6d-a4ab-43c4-84e5-0933c84b4f4f)
				echo "Make swap on partition $part."
				mkswap "$part" &>/dev/null
				;;
			*)
				echo "Skipping partition $part"
				;;
			esac
		fi
	done < "$PARTITION_AFTER"
else
	true # do nothing, cleanup and leave
fi

test -e "$PARTITION_BEFORE" && rm -f "$PARTITION_BEFORE"
test -e "$PARTITION_AFTER"  && rm -f "$PARTITION_AFTER"

