#!/bin/sh

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

# Clean input/output
exec >/dev/console 2>&1 </dev/console

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

# Check if we are running from Ramdisk or HD
INSTALLED=""
if [ -e /proc/sys/kernel/real-root-dev ]; then
read ROOTDEV </proc/sys/kernel/real-root-dev 2>/dev/null
case "$ROOTDEV" in 256|0x100) ;; *) INSTALLED="$ROOTDEV"; ;; esac
else
INSTALLED="yes"
fi

case "$0" in
  *halt)
	message="
${YELLOW}KNOPPIX halted.${NORMAL}"
	command="halt"
	options="-p -d -i -f"
	;;
  *reboot)
	message="${GREEN}Preparing for reboot...${NORMAL}"
	command="reboot"
	options="-r -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
[ -n "$INSTALLED" ] || options="$options -n"

mysleep() {
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
do
usleep 75000
echo -n "$1.${NORMAL}"
done
echo ""
}

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

# poweroff pcmcia devices
if pidof cardmgr >/dev/null 2>&1; then
echo -n "${BLUE}Shutting down PCMCIA devices...${NORMAL}"
cardctl 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
NETDEVICES="$(awk -F: '/eth.:/{print $1}' /proc/net/dev 2>/dev/null)"
if [ -n "$NETDEVICES" ]; then
pidof pump >/dev/null 2>&1 && { pump -k ; sleep 2; }
echo -n "${BLUE}Shutting down network device${NORMAL}"
for n in $NETDEVICES; do
echo -n " ${MAGENTA}$n${NORMAL}"
ifconfig $n down
done
echo ""
fi

# Note: This needs the KNOPPIX version of static "init" which contains
# the "halt" command plus rmmod/umount calls
FINALCMD="/sbin/$command"
if [ -z "$INSTALLED" ]; then
[ -x /etc/init ] || cp -p /sbin/init /etc/
[ -x /etc/init ] && FINALCMD="/etc/init"
fi

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

echo "${BLUE}Unmounting file systems.${NORMAL}"
# Turn on autoeject of CD-Rom
# Read in boot parameters
NOEJECT=""
read CMDLINE </proc/cmdline 2>/dev/null
case "$CMDLINE" in *noeject*) NOEJECT="yes"; ;; esac
for dev in /proc/sys/dev/cdrom*/lock; do [ -f "$dev" ] && echo 0 > "$dev"; done
if [ -z "$NOEJECT" ]; then
for dev in /proc/sys/dev/cdrom*/autoeject; do [ -f "$dev" ] && echo 1 > "$dev"; done
fi

# Turn on auto-eject feature of cdrom (2.2 kernel)
# if [ -z "$INSTALLED" ]; then
# CDROM="$(awk '/ \/cdrom /{print $1;exit 0;}' /proc/mounts)"
# [ -n "$CDROM" ] && eject -s -a on "$CDROM" 2>/dev/null
# fi

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

# For a harddisk installation: mount / ro
[ -n "$INSTALLED" ] && mount -o remount,ro / 2>/dev/null

echo "$message" >/dev/console

# Now halt or reboot.
exec $FINALCMD $options >/dev/console 2>&1 </dev/console
