#!/bin/sh
#
# Description: Safely migrate your imdd partitions onto a single
# drive (ad0 if possible, else twed0) and shut down all other imdd
# partitions/drives.
#
# This is mostly useful for migrating everything to ad0 while
# debugging the twe driver, but may have other uses too.
#
# Note that the /var/crash partition goes away, which may have some
# other side-effects.



### figure out what dir this program was found/run in (but if there
### was no path mentioned, don't just assume "./")
if echo "X$0" | awk '{ exit (index($0,"/") ? 1 : 0) }' ; then
    rundir="";
else
    rundir="`dirname $0`/"
fi

## See if the ad0 disk knows anything about imdd.  If so, it's a
## harddisk.  If not, it's nothing we can migrate to.
HAS_REAL_AD0=`( disklabel -A /dev/ad0s1a | grep -q imdd ) && echo "YES"`
HAS_REAL_AD2=`( disklabel -A /dev/ad2s1a | grep -q imdd ) && echo "YES"`

if [ "X$HAS_REAL_AD0" = "XYES" ] ; then
	ONEDRIVE=ad0
elif [ "X$HAS_REAL_AD2" = "XYES" ] ; then
	ONEDRIVE=ad2
else
	ONEDRIVE=twed0
fi

echo "Moving IMDD all onto $ONEDRIVE"

## Check that ${ONEDRIVE}s1a is in imdd0, and if not, put it there.
echo "Making sure ${ONEDRIVE}s1a is in imdd0..."
DRVS1A_IN=`( isi_imdd_config -i | grep imdd0 | grep -q ${ONEDRIVE}s1a ) || echo "NO"`
if [ "$DRVS1A_IN" = "NO" ] ; then
	/usr/bin/isi_hwtools/isi_imdd_remove -f /dev/${ONEDRIVE}s1a || exit
	isi_imdd_config -a imdd0 /dev/${ONEDRIVE}s1a
fi


## Check that ${ONEDRIVE}s1d is in imdd1, and if not, put it there.
echo "Making sure ${ONEDRIVE}s1d is in imdd1..."
DRVS1D_IN=`( isi_imdd_config -i | grep imdd1 | grep -q ${ONEDRIVE}s1d ) || echo "NO"`
if [ "$DRVS1D_IN" = "NO" ] ; then
	/usr/bin/isi_hwtools/isi_imdd_remove -f /dev/${ONEDRIVE}s1d || exit
	isi_imdd_config -a imdd1 /dev/${ONEDRIVE}s1d
fi


## Check that those both made it, else fail...
DRVS1A_IN=`( isi_imdd_config -i | grep imdd0 | grep -q ${ONEDRIVE}s1a ) || echo "NO"`
DRVS1D_IN=`( isi_imdd_config -i | grep imdd1 | grep -q ${ONEDRIVE}s1d ) || echo "NO"`
if [ "$DRVS1A_IN" = "NO" -o "$DRVS1D_IN" = "NO" ] ; then
	echo "ERROR -- Failed to migrate to $ONEDRIVE.  Aborting."
	exit
fi


## remove everything except ${ONEDRIVE}s1a from imdd0
echo "Removing all but ${ONEDRIVE}s1a from imdd0..."
eval "`isi_imdd_config -g | grep imdd0 | cut -f 2- | tr -s '\t ' '\n' | grep -v ${ONEDRIVE}s1a | sed -E 's~([^ ]+)~isi_imdd_config -x imdd0 \1~g'`"


## remove everything except ${ONEDRIVE}s1d from imdd1
echo "Removing all but ${ONEDRIVE}s1d from imdd1..."
eval "`isi_imdd_config -g | grep imdd1 | cut -f 2- | tr -s '\t ' '\n' | grep -v ${ONEDRIVE}s1d | sed -E 's~([^ ]+)~isi_imdd_config -x imdd1 \1~g'`"


## just kill imdd2 (/var/crash), imdd3 (root,lkg), and imdd4 (var,lkg)
echo "Shutting down imdd2, imdd3, imdd4..."
umount /var/crash
isi_imdd_config -u imdd2
isi_imdd_config -u imdd3
isi_imdd_config -u imdd4
isi_imdd_config -u imdd5


## disable everything except ${ONEDRIVE}.  This is undoable with
## imdd_enable all
echo "Disabling all but ${ONEDRIVE} from imdd in devices.py..."
${rundir}isi_imdd_enable ${ONEDRIVE}


echo "Hopefully complete."
isi_imdd_status

echo "You can revert to standard using \"${rundir}isi_imdd_enable all\""
