#!/bin/sh
# apmcontinue script for a Sony Vaio Z505H-series laptop under Red Hat Linux 6.2
#
# Written by Alex Stewart as part of the Sony Z505H fixup kit.
#
# NOTE: Make sure this file does not print anything to the screen on resume, as
# (due to a kernel bug) text screen output while an X console is in the
# foreground will screw up the X display by shifting it left.

# If autostart_eth0 is set to yes, this script will automatically attempt to
# bring up eth0 on a resume.  This is handy if you are usually connected to
# a network (and doesn't cause any harm if you resume it while not connected)
autostart_eth0=yes

logerr="/usr/bin/logger -t apm-$1 -p daemon.err --"
logmsg="/usr/bin/logger -t apm-$1 -p daemon.info --"

case "$1" in
  suspend)  
            # Unmount any NFS mounts.  This is a good idea for mobile machines
            # as when we resume we may not be connected to the same network
            # anymore, and stranded NFS mounts can be very annoying to clean up
            $logmsg "Attempting to unmount any nfs filesystems."
            /bin/umount -f -a -t nfs | $logerr

            # If eth0 is up, bring it down so we can remove the eepro100 module
            if /sbin/ifconfig -a | grep -q '^eth0'; then
              $logmsg "Bringing down eth0."
              /sbin/ifdown eth0
            fi

            # If eepro100 is loaded, rmmod it so it doesn't lock us up on
            # resume (this is a workaround for a bug in current 2.3/2.4 kernels
            # which shows up apparently when anything else is sharing an IRQ
            # with eepro100)
            if /sbin/lsmod | grep -q eepro100; then
              $logmsg "Removing eepro100 module."
              msg=`/sbin/rmmod eepro100 2>&1`
              if [ $? != 0 ]; then
                $logerr "$msg"
                # if we couldn't remove it, don't suspend, as we probably
                # won't be able to resume afterward.
                exit 1
              elif [ -n "$msg" ]; then
                # (rmmod doesn't normally print any non-error messages, but just
                # in case...)
                $logmsg "$msg"
              fi
            fi

            # If snd-card-ymfpci is loaded, it'll need to be bounced on resume,
            # (via the RESTORESOUND setting in /etc/sysconfig/apmd) which
            # will reset the mixer levels to 0.  Save the current mixer levels
            # so we can restore them afterward.
            if lsmod | grep -q snd-card-ymfpci && \
               [ -r /var/tmp/suspend_aumixrc ]; then
              $logmsg "Saving mixer settings."
              /usr/bin/aumix -S -f /etc/.aumixrc 2>&1 >/dev/null | $logerr
            fi
            ;;

  resume)   
            # restore any saved mixer settings
            if /sbin/lsmod | grep -q snd-card-ymfpci; then
              $logmsg "Restoring mixer settings."
              /usr/bin/aumix -L -f /etc/.aumixrc 2>&1 >/dev/null | $logerr
            fi

            if [ "$autostart_eth0" = "yes" ]; then
              # Attempt to bring eth0 back up automatically (handy when set up
              # for dhcp)
              $logmsg "Bringing up eth0..."
              ( /sbin/ifup eth0 | $logmsg ) 2>&1 | $logerr &
            fi

            ;;
esac

