#!/bin/sh

. /usr/share/debconf/confmodule

# Mostly based on mdcfg

expand_raidid() {
	new_raid_devices=
	for raid_device; do
		if [ "${raid_device#raidid=}" != "$raid_device" ]; then
			for match in $(cat "/var/lib/partman/raidids/${raid_device#raidid=}"); do
				new_raid_devices="${new_raid_devices:+$new_raid_devices }$match"
			done
		else
			new_raid_devices="${new_raid_devices:+$new_raid_devices }$raid_device"
		fi
	done
	echo "$new_raid_devices"
}

create_raid() {
	RAID_TYPE="$1"
	DEV_COUNT="$2"

	if ([ "$DEV_COUNT" -lt 3 ] && [ $RAID_TYPE = "5" ]) ||
	   ([ "$DEV_COUNT" -lt 4 ] && [ $RAID_TYPE = "6" ]) ||
	   ([ "$DEV_COUNT" -lt 2 ] && [ $RAID_TYPE = "10" ]); then
		db_input critical partman-auto-raid/notenoughparts
		db_go partman-auto-raid/notenoughparts
		exit 9
	fi

	SPARE_COUNT="$3"
	REQUIRED=$(($DEV_COUNT + $SPARE_COUNT))

	FS_TYPE="$4"
	MOUNTPOINT="$5"
	RAID_DEVICES="$6"
	SPARE_DEVICES="$7"

	RAID_DEVICES=$(echo $RAID_DEVICES | sed -e "s/#/ /g")
	RAID_DEVICES="$(expand_raidid $RAID_DEVICES)"

	SPARE_DEVICES=$(echo $SPARE_DEVICES | sed -e "s/#/ /g")
	SPARE_DEVICES="$(expand_raidid $SPARE_DEVICES)"

	NAMED_SPARES=$(echo $SPARE_DEVICES | wc -w)

	if [ "$RAID_TYPE" != "0" ]; then
		# Count them
		SELECTED=$(echo $RAID_DEVICES | wc -w)

		MISSING_DEVICES=""
		# Add "missing" for as many devices as weren't selected
		while [ "$SELECTED" -lt "$DEV_COUNT" ]; do
			MISSING_DEVICES="$MISSING_DEVICES missing"
			let SELECTED++
		done

		COUNT=$NAMED_SPARES
		MISSING_SPARES=""
		while [ "$COUNT" -lt "$SPARE_COUNT" ]; do
			MISSING_SPARES="$MISSING_SPARES missing"
			let COUNT++
		done
	fi

	# Find the next available md-number
	MD_NUM=$(grep ^md /proc/mdstat | sed -e 's/^md\(.*\) : active .*/\1/' | \
		sort | tail -n 1)
	if [ -z "$MD_NUM" ]; then
		MD_NUM=0
	else
		let MD_NUM++
	fi

	# If we haven't already stashed the number of the first RAID device
	# we used, do so now.
	db_get partman-auto-raid/raidnum
	if [ -z "$RET" ]; then
		db_set partman-auto-raid/raidnum $MD_NUM
	fi

	echo "Raid devices count: $DEV_COUNT"
	if [ "$RAID_TYPE" != "0" ]; then
		logger -t partman-auto-raid "Selected spare count: $NAMED_SPARES"
		logger -t partman-auto-raid "Spare devices count: $SPARE_COUNT"
		MDADM_PARAMS="-x $SPARE_COUNT $RAID_DEVICES $MISSING_DEVICES $SPARE_DEVICES $MISSING_SPARES"
	else
		MDADM_PARAMS="$RAID_DEVICES"
	fi

	if ! log-output -t partman-auto-raid \
		mdadm --create /dev/md$MD_NUM --auto=yes --force -R -l raid$RAID_TYPE \
		      -n $DEV_COUNT $MDADM_PARAMS
	then
		logger -t partman-auto-raid "Error creating array /dev/md$MD_NUM"
		exit 1
	fi
}

# Try to load the necessary modules.
depmod -a 1>/dev/null 2>&1
modprobe md-mod 1>/dev/null 2>&1
mkdir -p /dev/md

# Make sure that we have md-support
if [ ! -e /proc/mdstat ]; then
	db_set mdcfg/nomd false
	db_input critical mdcfg/nomd
	db_go
	exit 9
fi

# Force mdadm to be installed on the target system
apt-install mdadm || true

# Check we have recipe(s)
db_get partman-auto-raid/recipe
if [ -z "$RET" ]; then
	logger -t partman-auto-raid "Error: No recipe specified in partman-auto-raid/recipe"
	exit 1
fi

# Try to act on each recipe we were given
recipes=$RET
while [ -n "$recipes" ]; do
	tmp=$recipes
	recipes=$(echo $tmp | sed -e 's/^[^.]*\.\(.*\)$/\1/');
	recipe=$(echo $tmp | sed -e 's/^\([^.]*\)\..*$/\1/');

	if [ "$recipe" = "$recipes" ]; then
		recipes=''
	fi

	# Do the recipe!
	echo $recipe >/tmp/partman-auto-raid-recipe
	read raidtype devcount sparecount fstype mountpoint devs sparedevs \
		</tmp/partman-auto-raid-recipe
	create_raid $raidtype $devcount $sparecount $fstype $mountpoint \
		$devs $sparedevs
done

exit 0
