#!/bin/sh

. /lib/partman/lib/base.sh

# TODO: Add error checking and error messages !!!!!!!!!!!!

OLDIFS="$IFS"

create_ext2 () {
	frpart=${1##*/}
	db_subst partman-dmraid/progress/createfs/info DEVICE $frpart
	db_progress INFO partman-dmraid/progress/createfs/info
	# TODO: check that ext2 is actually available
	log-output -t partman-dmraid mkfs.ext2 -q $1
}

# Save status (including size) of current dmraid partitions
DM_STATUS="$(dmsetup status --target linear)"

# If a SATA RAID partition is in use as swap, it cannot be removed
disable_swap

# Make sure dmraid devices get re-enabled on errors
trap 'log-output -t partman-dmraid dmraid -ay' HUP INT QUIT PIPE TERM EXIT

# Remove existing devmapper devices for SATA RAID partitions
# TODO: should probably umount partitions if device is in use
for frdisk in $(dmraid -s -c | grep -v "No RAID disks"); do
	for frpart in $(ls /dev/mapper/$frdisk* 2>/dev/null); do
		[ $frpart != /dev/mapper/$frdisk ] || continue

		# Remove pending actions on the partitions
		devdir=$DEVICES/$(echo $frpart | sed 's:/:=:g')
		if [ -d $devdir ]; then
			rm -f $devdir/*/method
			rm -f $devdir/*/format
		fi

		# Remove the device mapper node
		dmsetup remove $frpart || exit 1
	done
done

# Commit the changes
confirm_changes "partman-dmraid" || exit 0
commit_changes partman-dmraid/commit_failed || exit $?

# Reset signal handling to defaults
trap - HUP INT QUIT PIPE TERM EXIT

# Refresh dmraid partition info
log-output -t partman-dmraid dmraid -ay

# Use number of linear devices as approximation of steps
PB_MAX=$(dmsetup status --target linear | wc -l)
db_progress START 0 $PB_MAX partman-dmraid/progress/createfs/title

# Restore IFS as it gets clobbered somewhere
IFS="$OLDIFS"

# Create an initial filesystem so new partitions are detected properly
# HACKALERT: we assume that the size of the device is a sufficient indicator
# that a partition was changed
# FIXME: this does not work if the partition number changes!

for frdisk in $(dmraid -s -c | grep -v "No RAID disks"); do
	for frpart in $(ls /dev/mapper/$frdisk* 2>/dev/null); do
		[ $frpart != /dev/mapper/$frdisk ] || continue
		if oldstatus=$(echo "$DM_STATUS" | grep "^${frpart#/dev/mapper/}:"); then
			oldsize=$(echo $oldstatus | cut -d" " -f 3)
			# If the device is passed, it is not included in the output!
			newsize=$(dmsetup status $frpart | cut -d" " -f 2)
			if [ $newsize -ne $oldsize ]; then
				create_ext2 $frpart
			fi
		else
			create_ext2 $frpart
		fi
		db_progress STEP 1
	done
done

db_progress SET $PB_MAX
db_progress STOP

stop_parted_server

restart_partman

exit 0
