#!/initrd/bin/ash

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

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

# Clear tty1
> /dev/tty1

cd /

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

SERVICES="haldaemon messagebus xfs iiim acpid lm_sensors apmd network"

for subsys in $SERVICES ; do
        [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
                || continue
        /etc/init.d/$subsys stop
done

for i in /var/lock/subsys/* ; do
        # Check if the script is there.
        [ -f "$i" ] || continue

        # Get the subsystem name.
        subsys=${i#/var/lock/subsys/}

        # Networking could be needed for NFS root.
        [ $subsys = network ] && continue

        # Bring the subsystem down.
        if [ -f /etc/init.d/$subsys.init ]; then
                /etc/init.d/$subsys.init stop
        elif [ -f /etc/init.d/$subsys ]; then
                /etc/init.d/$subsys stop
        else
                rm -f "$i"
        fi
done

# Check if we are running from a Knoppix-CD or HD

INSTALLED=""
# [ -e /BOOYO/bin/ash ] || INSTALLED=yes

case "$0" in
  *halt)
	message="${YELLOW}BOOYO 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
EJECT=""
if test -f /media/cdrom/LIVECD/base.sqfs; then
 EJECT="/media/cdrom"
fi
if test -f /media/cdrecorder/LIVECD/base.sqfs; then
 EJECT="/media/cdrecorder"
fi

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

sleep 2

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

sync

if grep -q /usr /proc/mounts; then
    busybox umount /usr
    busybox rmdir /usr
else
    busybox rm -f usr
fi

busybox rm -f bin etc lib sbin #remove links into /adios
busybox ln -s initrd/bin initrd/etc initrd/lib initrd/sbin initrd/usr /
killall -9 udevd 2>/dev/null #udev is no longer needed at this point

echo "Unmounting filesystems:"
[ -f /proc/bus/usb/devices ] && umount /proc/bus/usb 2>/dev/null
for i in /dev/loop/*; do
    losetup -d $i 2>/dev/null
done
umount -a 2>/dev/null

rmmod -a 2>/dev/null

if [ -n "$EJECT" ]; then
    echo "Ejecting the CD:"
    eject /dev/cdrom
fi

echo "$message"
exec $command $options

