#!/bin/sh

# Mostly based on mdcfg

. /usr/share/debconf/confmodule

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

	if [ "$DEV_COUNT" -lt 3 -a $RAID_TYPE = "5" ] ; then
		db_input critical partman-auto-raid/notenoughparts
		db_go partman-auto-raid/notenoughparts
		return
	fi

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

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

	NAMED_SPARES=`echo $SPARE_DEVICES|wc -w`

	RAID_DEVICES=`echo $DEVICES|sed -e "s/#/ /g"`

	SPARE_DEVICES=`echo $SPARE_DEVICES|sed -e "s/#/ /g"`

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

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

		MISSING_SPARES=""

		COUNT=${NAMED_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
		echo "Selected spare count: ${NAMED_SPARES}"
		echo "Spare devices count: ${SPARE_COUNT}"
		MDADM_PARAMS="-x ${SPARE_COUNT} ${RAID_DEVICES} ${MISSING_DEVICES} ${SPARE_DEVICES} ${MISSING_SPARES}"
	else 
		MDADM_PARAMS="${RAID_DEVICES}"
	fi

	echo "Commandline:"

	`mdadm --create /dev/md/${MD_NUM} --auto=yes --force -R -l raid${RAID_TYPE} -n ${DEV_COUNT} $MDADM_PARAMS`
}

# Try to load the necesarry modules.
# Supported schemes: RAID 0, RAID 1, RAID 5
depmod -a 1>/dev/null 2>&1
modprobe md 1>/dev/null 2>&1 || modprobe md-mod 1>/dev/null 2>&1
modprobe raid0 >/dev/null 2>&1
modprobe raid1 1>/dev/null 2>&1
modprobe raid5 >/dev/null 2>&1
mkdir -p /dev/md

# Make sure that we have md-support
if [ ! -e /proc/mdstat ]; then
	db_set partman-auto-raid/nomd "false"
	db_input critical partman-auto-raid/nomd
	db_go
	db_stop
	exit 0
fi

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

# Check we have recipe(s)
db_get partman-auto-raid/recipes
if [ -z "$RET" ]; then
	db_set partman-auto-raid/norecipes "false"
	db_input high partman-auto-raid/norecipes
	db_go
	db_stop
	exit 0
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
