#!/bin/sh

PATH=/sbin:/bin:/usr/bin:/usr/sbin
export PATH

# override tool behaviour through distro-defaults
FLL_DISTRO_MODE="installed"
FLL_DISTRO_NAME="sidux"
[ -r /etc/default/distro ] && . /etc/default/distro
FLL_DISTRO_NAME="$(echo $FLL_DISTRO_NAME | tr [a-z] [A-Z])"

[ ! "$FLL_DISTRO_MODE" = "live" ] && exit 0

case "$1" in
	start)
		;;
	stop)
		;;
	restart|force-reload)
		;;
esac

NORMAL="[0;39m"
RED="[1;31m"
GREEN="[1;32m"
YELLOW="[1;33m"
BLUE="[1;34m"
MAGENTA="[1;35m"
CYAN="[1;36m"
WHITE="[1;37m"

case "$0" in
	*halt)
		message="${YELLOW}${FLL_DISTRO_NAME} halted.${NORMAL}"
		command="halt"
		options="-p -d -i -f"
		;;
	*reboot)
		message="${GREEN}Preparing for reboot...${NORMAL}"
		command="reboot"
		options="-d -i -f"
		;;
	*)
		echo "$0: call this script as \"halt\" or \"reboot\" please!"
		exit 1
		;;
esac

# No sync and no wtmp entry if running from CD
options="$options -n"

mysleep()
{
	for i in $(seq 1 40); do
		sleep "0.075s"
		echo -n "$1.${NORMAL}" 
	done
	echo "" 
}

# Disable kernel messages
echo "0" > /proc/sys/kernel/printk

# handle wpa_supplicant gracefully
if [ -x /etc/init.d/wpa-ifupdown ]; then
	/etc/init.d/wpa-ifupdown stop >/dev/null 2>&1
fi

# poweroff pcmcia devices
if lsmod | grep -q pcmcia_core >/dev/null 2>&1; then
	echo -n "${BLUE}Shutting down PCMCIA devices...${NORMAL}" 
	pccardctl eject >/dev/null 2>&1
	sleep 2
	echo "" 
fi

# Now kill them all
killall5 -15
sleep 1
echo -n "${BLUE}Sent all processes the TERM signal...${NORMAL}" 
mysleep "$BLUE"

killall5 -9
sleep 1
echo -n "${RED}Sent all processes the KILL signal...${NORMAL}" 
mysleep "$RED"

# Unmount network filesystems first before shutting down network
NETMOUNTS="$(awk '{if($1~/:/){print $2}}' /proc/mounts 2>/dev/null)"
if [ -n "$NETMOUNTS" ]; then
	echo "${BLUE}Unmounting network filesystems.${NORMAL}" 
	umount -t nfs -arvf 2>/dev/null 
fi

# Shutdown network
echo "${BLUE}Shutting down network devices...${NORMAL}" 
/etc/init.d/networking stop >/dev/null 2>&1

# read cmdline params
NOEJECT=""
NOPROMPT=""
read CMDLINE <<EOT
$(cat /proc/cmdline 2>/dev/null)
EOT
case "$CMDLINE" in *noeject*)  NOEJECT="yes";  ;; esac
case "$CMDLINE" in *noprompt*) NOPROMPT="yes"; ;; esac

CDROM="$(awk '/ \/cdrom /{print $1;exit 0;}' /proc/mounts)"

# This uses standard sysvinit
FINALCMD="/etc/live-hack/$command"
rm -rf /etc/live-hack >/dev/null 2>&1
mkdir -p /etc/live-hack
[ -x "/etc/live-hack/$command" ]  || cp -p "/sbin/$command" /etc/live-hack/
[ -x /usr/bin/eject ]             && cp -p /usr/bin/eject /etc/live-hack/eject
LD_LINUX=ld-linux.so.2
[ -x /lib/ld-linux-x86-64.so.2 ]  && LD_LINUX=ld-linux-x86-64.so.2
[ -x "/etc/live-hack/$LD_LINUX" ] || cp -p "/lib/$LD_LINUX" /etc/live-hack/

# Turn off swap, then unmount file systems.
swapoff -a >/dev/null 2>&1

echo "${BLUE}Unmounting file systems.${NORMAL}" 

cd /

# Umount everything but root
umount -arvf 2>/dev/null 
if [ "$?" != "0" ] ; then
	# Free loopback devices if necessary, so we can unmount the host media
	for i in /dev/loop*; do losetup -d "$i" 2>/dev/null ; done
	# And retry
	umount -arf 2>/dev/null 
fi

# Remove remaining unused modules
rmmod -a >/dev/null 2>&1

echo "$message" 

# eject CD-ROM, noprompt implies noeject (backward compatibility)
if [ -z "$NOPROMPT" ]; then
	[ -n "$CDROM" -a -z "$NOEJECT" ] && "/etc/live-hack/$LD_LINUX" /etc/live-hack/eject -m -p "$CDROM" >/dev/null 2>&1
	echo "${CYAN}Please remove CD, close cdrom drive and hit return.${NORMAL}" 
	read
fi

# Now halt or reboot.
exec "/etc/live-hack/$LD_LINUX" "$FINALCMD" $options 
