#!/bin/sh
# Filename:      grml-reboot
# Purpose:       reboot grml system
# Authors:       grml-team (grml.org), (c) Klaus Knopper, (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
# Latest change: Sam Mr 31 14:02:06 CEST 2007 [mika]
################################################################################

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

. /etc/grml_colors
. /etc/grml/lsb-functions

cd /

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

# Check if we are running from a GRML-CD or HD
INSTALLED=""
[ -e /GRML/etc/grml_cd ] || INSTALLED=yes
# Assume that we don't want the eject+prompt stuff when running in one
# of the special modes:
grep -qe ' toram' -qe ' usb' -qe 'serial' -qe 'fromhd' /proc/cmdline && INSTALLED=yes

case "$0" in
  *halt)
        level=0
        message="grml system will be halted..."
        command="halt"
        ;;
  *reboot)
        level=6
        message="Preparing for reboot..."
        command="reboot"
        ;;
  *)
        echo "Usage: call this script as \"halt\" or \"reboot\" please.">&2
        exit 1
        ;;
esac

mysleep() {
  for i in `seq 1 40` ; do
    usleep 75000
    echo -n "$1.${NORMAL}"
  done
  echo ""
}

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

# We may kill our network connection here before unmounting NFS. Bad luck.
# poweroff pcmcia devices
if [ -d /sys/bus/pcmcia -a -x /sbin/pccardctl ] ; then
   log_begin_msg "Shutting down PCMCIA devices..."
   pccardctl eject >/dev/null 2>&1
   log_end_msg $?
fi

if [ -n "$INSTALLED" ] ; then
   echo
   log_begin_msg_nn "Running /etc/init.d/rc ${level}: "
   /etc/init.d/rc $level 1>/dev/null 2>&1 &
   mysleep "$GREEN"
   log_end_msg 0
fi

# Remove all truecrypt mappings...
if [ -x /usr/sbin/truecrypt ] ; then
   if grep -q truecrypt /proc/modules ; then
      log_begin_msg "Unmapping truecrypt volumes."
      MSG=$(truecrypt -d 2>1)
      echo $MSG | grep "No volumes mapped" && eend 0
      [ -z "$MSG" ] && eend 0 || eend 1
   fi
fi

# Now kill them all
killall5 -15
sleep 1
log_begin_msg_nn "Sending all processes the TERM signal: "
mysleep "$BLUE" ; log_end_msg $?

killall5 -9
sleep 1
log_begin_msg_nn "Sending all processes the KILL signal: "
mysleep "$RED" ; log_end_msg $?

# check for nfsdir
if grep -q nfsdir /proc/cmdline ; then
   log_begin_msg "Bootoption nfsdir detected, syncing filesystems."
   sync && sleep 1 ; log_end_msg $?
else
   # Unmount network filesystems first before shutting down network
   NETMOUNTS="$(awk '{if($1~/:/){print $2}}' /proc/mounts 2>/dev/null)"
   if [ -n "$NETMOUNTS" ]; then
      log_begin_msg "Unmounting network filesystems."
      umount -t nfs -arvf 2>/dev/null ; log_end_msg $?
   fi
   # Shutdown network
   NETDEVICES="$(/sbin/ifconfig | awk '/^[^ ]+/ {print $1}' | grep -v '^lo$')"
   if [ -n "$NETDEVICES" ]; then
      pidof pump >/dev/null 2>&1 && { pump -k ; sleep 2; }
      killall dhclient dhclient3 2>/dev/null
      log_begin_msg "Shutting down network device..."
      for n in $NETDEVICES; do
          echo "${SUBMSG} ${WHITE}$n${NORMAL}"
          ifdown $n 1>/dev/null 2>&1
          ifconfig $n down
      done ; log_end_msg $?
   fi
   log_begin_msg "Syncing local filesystems..."
   sync && sleep 1 ; log_end_msg $?
fi

# Turn off swap, then unmount file systems.
log_begin_msg "Turning off swap."
swapoff -a >/dev/null 2>&1 ; log_end_msg $?

# Udev
log_begin_msg "Deactivating udev:"
echo -n "   ${GREEN}-${NORMAL} "
/etc/init.d/udev stop ; log_end_msg $?
[ -n $INSTALLED ] || mkdir -p /dev/pts
[ -n $INSTALLED ] || cp -a /GRML/dev/console /GRML/dev/tty[0-9]* /GRML/dev/loop* /GRML/dev/initctl /GRML/dev/null /dev/
[ -n $INSTALLED ] || ln -s /GRML/dev/* /dev/ 2>/dev/null

# Read in boot parameters
CMDLINE="`cat /proc/cmdline 2>/dev/null`"
# noprompt or noeject option given?
NOPROMPT=${NOPROMPT:-''}
case "$CMDLINE" in *noprompt*) NOPROMPT="yes"; ;; esac
NOEJECT=${NOEJECT:-''}
case "$CMDLINE" in *noeject*) NOEJECT="yes"; ;; esac
[ -r /etc/noprompt ] && NOPROMPT='yes'
[ -r /etc/noeject ]  && NOPROMPT='yes' && NOEJECT='yes'

# Turn on autoeject of CD-ROMs
if [ -z "$NOEJECT" ]; then
   for dev in /proc/sys/dev/cdrom*/lock;      do [ -f "$dev" ] && echo 0 > "$dev"; done
   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)"
fi

# Now umount everything but root
log_begin_msg "Unmounting file systems."

# Be safe in case someone messed with /etc/mtab
if [ -z "$INSTALLED" ] ; then
   rm -f /etc/mtab
   ln -snf /proc/mounts /etc/mtab
fi

# 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

# Remove remaining unused modules (Kernel 2.4)
rmmod -a >/dev/null 2>&1

log_end_msg 0

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

if [ -z "$INSTALLED" -a -z "$NOPROMPT" ]; then
   # do not prompt for removal when running in grml2ram mode:
   if ! mount | grep -q 'on /cdrom ' ; then
      echo "CD not mounted, nothing to eject therefore."
   else
      umount -l /cdrom
      [ -n "$CDROM" -a -z "$NOEJECT" ] && eject -p "$CDROM" 2>/dev/null
      echo ""
      echo "${GREEN}Please remove CD, close cdrom drive and hit return.${NORMAL}"
      read
   fi
fi

echo
echo "$message" >/dev/console

# Finally halt or reboot
/etc/init.d/$command stop

## END OF FILE #################################################################
