#! /bin/bash
# Copyright (C) 2006 Carlos Villegas and Petter Reinholdtsen
# Full notice of GPL license can be found in the main 
# project file: checkLSB.
#
# Author: Petter Reinholdtsen
# Author: Carlos Villegas
# Created:   2006-07-26
# Last updated: 2006-08-11
#
# Alter the boot process to test different hotspot combinations.
# Among other things, the init scripts in /etc/init.d and their 
# symbolic link pool (/etc/rcX.d) are modified and packages are 
# installed according to the hotspots to be tried.
# 
# 
# Run >>./testhotspots.sh change hotspot1 hotspot2 hotspot3 to do a dry run and
# run >>./testhotspots.sh -d to perform the changes

date=`date +%y%m%d`


if [[ "$2" == "dash" || "$3" == "dash" || "$4" == "dash" || "$5" == "dash" || "$6" == "dash" ]] ;
then
	dash=1;
	echo "# Dash Hotspot to be tested"
else
	dash=0;
fi


if [[ "$2" == "networking" || "$3" == "networking" || "$4" == "networking" || "$5" == "networking" || "$6" == "networking" ]];
then
	networking=1;
	echo "# Networking Hotspot to be tested"
else
	networking=0;
fi


if [[ "$2" == "hwclock" || "$3" == "hwclock" || "$4" == "hwclock" || "$5" == "hwclock" || "$6" == "hwclock" ]];
then
	hwclock=1;
	echo "# Hardware clock Hotspot to be tested"
else
	hwclock=0;
fi

if [[ "$2" == "insserv" || "$3" == "insserv" || "$4" == "insserv" || "$5" == "insserv" || "$6" == "insserv" ]];
then
	insserv=1;
	echo "# Reordering with Insserv Hotspot to be tested"
else
	insserv=0;
fi

if [[ "$2" == "parallel" || "$3" == "parallel" || "$4" == "parallel" || "$5" == "parallel" || "$6" == "parallel" ]];
then
	parallel=1;
	echo "# Parallel Hotspot to be tested"
else
	parallel=0;
fi


if [[ "$2" == "preload" || "$3" == "preload" || "$4" == "preload" || "$5" == "preload" || "$6" == "preload" ]];
then
	preload=1;
	echo "# Preload Hotspot to be tested"
else
	preload=0;
fi


case "$1" in
change)
	

	echo cp -r /etc/init.d /etc/init.d_$date


	if [[ "$networking" == 1 ]];
	then
	echo cp /etc/init.d_new/networking /etc/init.d/
	fi


	if [[ "$hwclock" == 1 ]];
	then
	echo cp /etc/init.d_new/hwclock.sh /etc/init.d/
	fi

	if [[ "$insserv" == 1 ]];
	then
	echo apt-get install insserv
	echo update-bootsystem-insserv
	echo "Check for a correct boot order"
	fi

	if [[ "$parallel" == 1 ]];
	then
	echo mv /etc/default/rcS /etc/default/rcS_old
	echo mv /etc/default/rcS_shell /etc/default/rcS
	fi

	if [[ "$preload" == 1 ]];
	then
	echo apt-get install preload
	echo "Adjust the /etc/proload.conf"
	fi


	if [[ "$dash" == 1 ]];
	then
	echo apt-get install dash;
	echo dpkg-divert --add /bin/sh;
	echo mv /bin/sh /bin/sh_old;
	echo cd /bin
	echo ln -s dash sh
	echo cd -
	fi

	;;
restore)

	if [[ "$dash" == 1 ]];
	then
	echo dpkg-divert --remove /bin/sh
	echo rm /bin/sh
	echo mv /bin/sh_old /bin/sh
	echo apt-get remove --purge dash
	echo apt-get install --reinstall bash
	fi

	if [[ "$insserv" == 1 ]];
	then
	echo update-bootsystem-insserv restore
	echo apt-get remove --purge insserv
	fi

	if [[ "$parallel" == 1 ]];
	then
	echo mv /etc/default/rcS /etc/default/rcS_shell
	echo mv /etc/default/rcS_old /etc/default/rcS
	fi

	if [[ "$preload" == 1 ]];
	then
	echo apt-get remove --purge preload
	fi
	
	echo rm -r /etc/init.d
	echo cp -r /etc/init.d_$date /etc/init.d
	
	;;
*)
	echo "Usage: testhotpots.sh [ change | restore ] hotspot-name1 hotspot-name2 ..."
	;;
esac
	
	
