#!/bin/sh
#
# Description: handy hack tool to automatically tweak
# ${DEVICES} to adjust settings to more debug-friendly
# versions.
#
# uses:
#  isi_imdd_enable small --
#    Cleanly sets imdd to only a single partition per, so that if
#    you crash your box, your reboot doesn't need to remirror all
#    four disks.
#  isi_imdd_enable ad0 --
#    Migrate all imdd partitions off of twed disks.  [only works on
#    8-port twed systems].  This helps when debugging the twe driver.

RCFILE=/etc/rc.conf
DEVICE_OVERRIDE=/etc/isilon_disks
LOCK_OVERRIDE="/usr/bin/isi_dongle_sync -p > /dev/null"

usage() {
	echo "Usage: $0 <all|show|ad0|twed0|small>"
	echo "  all - should undo any other changes."
	echo "  show - shows the relevant lines as currently set."
	echo "  ad0|ad2|twed0|da0 - select a single-disk for booting.  User"
	echo "    is responsible for picking right one and migrating"
	echo "    safely -- see isi_imdd_shutdown to do this automatically."
	echo "  small - tell imdd to aim for only 1 partition, not 4."
	echo "    be sure to reboot cleanly or run /etc/rc.d/isi_imdd_start"
	echo "    to get immediate effects of this."
	echo "  undead - undead all partitions"
	exit 1
}

enable_all() {
	# Remove 'imdd_maxpart=' line from ${RCFILE}
	grep -vw 'imdd_maxpart=' ${RCFILE} > ${RCFILE}.1
	if [ "`stat -f '%z' ${RCFILE}.1`" = "0" ]; then
		# Nothing left of the file; delete it
		rm -f ${RCFILE} ${RCFILE}.1
	else
		# Replace
		mv -f ${RCFILE}.1 ${RCFILE}
	fi

	# Truncate ${DEVICE_OVERRIDE} if it exists
	test \! -e ${DEVICE_OVERRIDE} || truncate -s0 ${DEVICE_OVERRIDE}

	# Push to dongle
	${LOCK_OVERRIDE}
}

              
if [ "$#" -ne 1 -o "$1" = "-h" ]; then
	usage
fi

if [ "$1" = "all" ]; then
	enable_all
elif [ "$1" = "ad0" -o "$1" = "ad2" -o  "$1" = "twed0" -o  "$1" = "da0" ]; then
	enable_all
	echo "$1" > ${DEVICE_OVERRIDE}
	${LOCK_OVERRIDE}
elif [ "$1" = "small" ]; then
	# Set 'imdd_maxpart=' line in ${RCFILE}
	if [ -s ${RCFILE} ]; then
		# Add to ${RCFILE}, removing any previous 'imdd_maxpart=' line
		grep -vw 'imdd_maxpart=' ${RCFILE} > ${RCFILE}.1
		echo 'imdd_maxpart="1"' | cat ${RCFILE}.1 - > ${RCFILE}
	else
		# Create ${RCFILE}
		echo 'imdd_maxpart="1"' > ${RCFILE}
	fi
elif [ "$1" = "undead" ]; then
	isi_imdd_status -a | grep idead | cut -d ' ' -f 1 | xargs isi_imdd_forcepart none
	test \! -f /var/run/isilon_disks || rm /var/run/isilon_disks ## remove drive_d designation for now...
	echo "All idead partitions have been cleared -- run isi_imdd_rebalance to use"
elif [ "$1" = "show" ]; then
	# Print ${RCFILE}
	if [ -s ${RCFILE} ]; then
		if grep -wq 'imdd_maxpart=' ${RCFILE}; then
			grep -w 'imdd_maxpart=' ${RCFILE} | sed -e "s~^~${RCFILE}:    ~"
		else
			echo "  [no imdd_maxpart statement in ${RCFILE} file]"
		fi
	else
		echo "  [no mirror-limiting ${RCFILE} file]"
	fi
	test -e ${DEVICE_OVERRIDE} || echo "  [no device-limiting ${DEVICE_OVERRIDE} file]"
	test \! -e ${DEVICE_OVERRIDE} -o -s ${DEVICE_OVERRIDE} || echo "  [empty device-limiting ${DEVICE_OVERRIDE} file]"
	test \! -s ${DEVICE_OVERRIDE} || cat ${DEVICE_OVERRIDE} | sed -e "s~^~${DEVICE_OVERRIDE}:    ~"
else
	echo "Didn't recognize enable-type $1".
	usage
fi
