#!/bin/sh

# NOTE: This shit is brain damaged busybox sh syntax!

. /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() {
	# we get here something like:
	# $raidtype $devcount $sparecount $fstype $mountpoint $devs $sparedevs
	# 10 2 0 xfs / /dev/chassis/SYS/HDD0/p2#/dev/chassis/SYS/HDD12/p2
	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)"
	T=''
	ALL=''
	for X in ${RAID_DEVICES} ; do
		T=$(readlink -f $X)
		ALL="${ALL} $T"
	done
	RAID_DEVICES="${ALL}"

	SPARE_DEVICES=$(echo $SPARE_DEVICES | sed -e "s/#/ /g")
	SPARE_DEVICES="$(expand_raidid $SPARE_DEVICES)"
	T=''
	ALL=''
	for X in ${SPARE_DEVICES} ; do
		T=$(readlink -f $X)
		ALL="${ALL} $T"
	done
	SPARE_DEVICES="${ALL}"

	MD_NUM=''
	ALL=$( echo ${RAID_DEVICES} | sed -e 's,/dev/,,g' -e 's, ,|,g')
	L=$(grep '^md' /proc/mdstat | grep -E " (${ALL})\[")
	COUNT=$( echo "$L" | wc -l)
	if [ -n "$COUNT" ]; then
		# there is already a md with these devs running
		if [ $COUNT -gt 1 ]; then
			# misconfiguration - different /dev/md* are using them
			X=$(echo "$L" | sed -e 's,:.*,,' | tr '\n' ' ')
			logger -t auto-raidcfg 'Misconfiguration - the partitions' \
				"${RAID_DEVICES} are already in use by the raid devices" \
				"${X}. If appropriate, stop these devices" \
				'(mdadm --stop /dev/mdN), zero out the md superblock' \
				'of the partitions (mdadm --zero-superblock /dev/sdXn)' \
				'and try again!'
			exit 1
		elif [ $COUNT -eq 1 ]; then
			MD_NUM="${L%% *}"
			# there is one mdN, which is using the partitions already.
			# ok, if exactly these are used. 
			X=$( echo "$L" | sed -e "s, raid${RAID_TYPE} , ,")
			if [ "$X" = "$L" ]; then
				logger -t auto-raidcfg "Misconfiguration - raid ${MD_NUM} is" \
					"already running, but is not a raid${RAID_TYPE}. Please" \
					'correct the problem (e.g. by destroying it) and try again.'
				exit 1
			fi
			L="$X"
			ALL=$( echo ${RAID_DEVICES} | sed -e 's,/dev/,,g')
			for X in ${ALL} ; do
				# we allow it to be a spare
				T=$( echo "$L" | sed -re "s, ${X}\[[^ ]+,,")
				if [ "$T" = "$L" ]; then
					logger -t auto-raidcfg 'Misconfiguration - the raid' \
						"${MD_NUM} is already running but does not contain" \
						"partition ${X}. Please correct the problem and try" \
						'again.'
					exit 1
				fi
				L="$T"
			done
			ALL=$( echo ${SPARE_DEVICES} | sed -e 's,/dev/,,g')
			# the case, that a spare is configured for this raid and is an
			# active part of another raid would be a mdraid bug - does not exit.
			# So just give a warning, if it is not in here.
			for X in ${ALL} ; do
				# spare or not, ok if it is part of the raid
				T=$( echo "$L" | sed -re "s, ${X}\[[^ ]+,,")
				if [ "$T" = "$L" ]; then
					logger -t auto-raidcfg "Warning - raid ${MD_NUM} is" \
						"already running but has no spare $X . You should" \
						'correct this by adding it later.'
				fi
				L="$T"
			done
			L="${L#*: }"
			for X in ${L} ; do
				[ "${X#*]}" = "$X"  ] && continue
				T="${X%[[]*}"
				if [ "${X#*](S)}" = "$X"  ]; then
					logger -t auto-raidcfg "Warning - raid ${MD_NUM} is" \
						"already already running and uses partition ${T}," \
						'however it is not part of your receipt. Please' \
						'correct the problem (e.g. by destroying this raid)' \
						'and try again.'
					exit 1
				fi
				logger -t auto-raidcfg "Warning - raid ${MD_NUM} is" \
					"already running and uses partition ${T} as" \
					'spare, however, the current receipt does not list' \
					'it as a spare. You may remove it later.'
			done
			logger -t auto-raidcfg "Using raid ${MD_NUM} as is."
			MD_NUM="${MD_NUM#md}"
			db_set partman-auto-raid/raidnum $MD_NUM
			return 0
		fi
	fi


	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 auto-raidcfg "Selected spare count: $NAMED_SPARES"
		logger -t auto-raidcfg "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 auto-raidcfg \
		mdadm --create /dev/md$MD_NUM --auto=yes --force -R -l raid$RAID_TYPE \
		      -n $DEV_COUNT $MDADM_PARAMS
	then
		logger -t auto-raidcfg "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 auto-raidcfg "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
