#! /bin/bash

if ( echo $@ | grep -e "--version" ) 1> /dev/null; then
    echo "Script:       $0"
    echo "Distribution: ALTLinux"
    echo "Version:      0.16"
    exit 0
fi

CONFIGFILE=/etc/suspend.conf
INITDIR=/etc/init.d
RLVLDIR=/etc/rc.d
IFDOWN=/etc/sysconfig/network-scripts/ifdown
OPTDOWN=
IFUP=/etc/sysconfig/network-scripts/ifup
OPTUP=
LOCKFILE=/var/lock/subsys/swsusp
SETSYSFONT=/sbin/setsysfont
OPTSYSFONT=
DISTRIB=ALTLinux

# DO NOT EDIT THIS FILE (change the script ./suspend-0.16.sh and reinstall)

VERSION="0.16"
if ( echo $@ | grep -e "--version" ) 1> /dev/null; then
    echo "Installation script:     $0"
    echo "suspend script (Version: $VERSION)"
    exit 0
fi

PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin


TMP=/dev/shm/suspend.$$
if ! touch $TMP 2> /dev/null ; then
    TMP=/tmp/suspend.$$
    if ! touch $TMP 2> /dev/null ; then
        echo "Can't open a temporary file in /dev/shm nor /tmp"
        echo "Aborting !"
        exit 1
    fi
fi

ID=`id -u`
#if [ "$ID" != "0" ]; then
#    echo "You must be root to use this script."
#    echo "Use sudo to enable it for users."
#    exit 1
#fi

if ( echo $@ | grep -e "--verbose" ) 1> /dev/null; then
    if [ -w /dev/tty ] ;then
        VERBOSE='/dev/tty'
    else
        VERBOSE='/dev/console'
    fi
else
    VERBOSE='/dev/null'
fi

# This overides failsafe cancellation and forces suspend or installation
if ( echo $@ | grep -e "--force" ) 1> $VERBOSE; then
    FORCE='yes'
else
    FORCE='no'
fi

EXE=`basename $0`
unset AM_IN_INSTALLER


cat > $TMP <<EOF
Usage: $EXE [options]

The script file $EXE uses a configuration file $CONFIGFILE.
It is intended to cleanly call software suspend. Its 
options are as follows:
--help      Prints this help.
--force     Ignore devices that cannot be stopped or unmounted.
--kill      Kill processes that prevent from suspending.
--silent    Be silent on processing
--verbose   Be really verbose on standard output
--nosuspend Performs everything except actually suspend (configuration
                testing and debugging purpose)
To help suspension, it is strongly advisable to use automount for 
devices like cdrom floppy fat partitions, so that remount will
automatically take place when needed. For the same purpose
autoload of modules in kernel is a good point.
EOF

ID=`id -u`
if [ "$ID" != "0" ]; then
    echo "You must be root to use this script."
    echo "Use sudo to enable it for users."
    exit 1
fi

if [ -f "$CONFIGFILE" ] ; then
    . $CONFIGFILE
else
    echo "No configuration file $CONFIGFILE !!! Try reinstalling from suspend.sh?"
    cat $TMP
    rm $TMP
    exit 1
fi

if ( echo $@ | grep -e "--help" ) 1> $VERBOSE; then
    cat $TMP
    rm $TMP
    exit 0
fi
rm $TMP

VT_CONSOLE="/dev/tty$SWSUSP_VT"

if ( echo $@ | grep -e "--silent" ) 1> /dev/null; then
    CONSOLE='/dev/null'
    HWCLKDEBUG=""
else
    if [ -w $VT_CONSOLE ] ;then
        CONSOLE="$VT_CONSOLE"
    elif [ -w /dev/tty ] ;then
        CONSOLE='/dev/tty'
    else
        CONSOLE='/dev/console'
    fi
    HWCLKDEBUG="--debug"
fi

LOGCMD="tee -a $CONSOLE"

# No longer needed because swsusp does the same job?
#[ `cat /proc/swaps | grep partition | wc -l` -lt 1 ] && {
#    echo "No swap partition activated, cannot suspend"
#    exit 0
#}

#Defining a few more command-line options...

if ( echo $@ | grep -e "--kill" ) 1> $VERBOSE; then
    KILL_PROCS='yes'
else
    KILL_PROCS='no'
fi


# This does everything except suspension
if ( echo $@ | grep -e "--nosuspend" ) 1> $VERBOSE; then
    NOSUSPEND='yes'
else
    NOSUSPEND='no'
fi
#############################################################
# Function definitions
#############################################################
T_IFS=

#if ! which usleep 1> $VERBOSE 2> $VERBOSE ; then
#    # fallback function if usleep-utility isn't available
#    usleep() {
#        # try using perl
#        perl -e "select(undef,undef,undef,$1/1000000)" && return 0
#
#        # calculate sleep time in seconds
#        sleeptime=$((($1)/100000))
#        [ $((($1) % 100000)) -ne 0 ] && $sleeptime=$(($sleeptime+1))
#
#        sleep $sleeptime
#    }
#fi

CheckProgs() {
    ret=0;
    if [ $KILL_PROCS = "yes" ]; then
        for sig in 15 9 ;do
            for prog in $SWSUSP_NON_COMPATIBLE_PROGS; do
                ps ax > $TMP
                pids=`awk '($5 == "'$prog'"){print $1}' $TMP`
                for pid in $pids; do
                    echo -e "\033[1;32m * \033[0mKilling($sig) $prog($pid)..." | $LOGCMD > $VERBOSE
                    kill -$sig $pid 2> $VERBOSE | $LOGCMD > $VERBOSE
                    usleep 500000
                done
            done
        done
    fi
    for prog in $SWSUSP_NON_COMPATIBLE_PROGS; do
        ps ax > $TMP
        pids=`awk '($5 == "'$prog'"){print $1}' $TMP`
        if [ "$pids" != "" ];then
            echo -e "Exception (non compatible) program $prog: $pids" | $LOGCMD > $VERBOSE
            ret=1
        fi        
    done
    test -f $TMP && rm $TMP
    return $ret
}

KillProgs() {
    for prog in $SWSUSP_STOP_PROGS_BEFORE_SUSPEND; do
            echo -e "\033[1;32m * \033[0mKilling $prog..." | $LOGCMD > $VERBOSE
            killall $prog 1> $VERBOSE  2> $VERBOSE
    done
    return 0;
}


ExecuteProgs() {
    for prog in $SWSUSP_START_PROGS_AFTER_RESUME; do
            echo -e "\033[1;32m * \033[0mStarting $prog..." | $LOGCMD > $VERBOSE
            $prog 1> $VERBOSE  2> $VERBOSE &
    done
}


SwitchFromX() {
    which fgconsole > $VERBOSE && ORIGINAL_VT=`fgconsole`
    if [ "$SWSUSP_LEAVE_X_BEFORE_SUSPEND" != "yes" -a "$SWSUSP_LEAVE_X_BEFORE_SUSPEND" != "nvidia" ]; then
        return 0;
    fi
    echo -e "\033[1;32m * \033[0mSwitching to suspend console..." | $LOGCMD > $VERBOSE
    chvt $SWSUSP_VT
    ret=$?
    
    usleep 500000 # This to ensure usb mouse is released by X
    return $ret
}

SwitchToX() {
    if [ "$SWSUSP_LEAVE_X_BEFORE_SUSPEND" = "nvidia" ]; then
        export HOME=/dev/shm
        xauth 2> $VERBOSE 1> $VERBOSE <<EOF
add :3 . 00
generate :3.0 .
EOF
	if [ -x /usr/bin/X11/xmessage -a "$DISTRIB" != "Gentoo" -a "$DISTRIB" != "ALTLinux" ] ;then
            xinit /usr/bin/X11/xmessage -font huge -display :3.0 -center -timeout 2 "  Fake Xserver for restoring nvidia cards properly  " -- :3
        else
            xinit /bin/false -- :3 2> $VERBOSE 1> $VERBOSE
        fi
        xauth 2> $VERBOSE 1> $VERBOSE <<EOF
remove :3
remove :3.0
EOF
    fi

    test $SWSUSP_LEAVE_X_BEFORE_SUSPEND != "no" && echo -e "\033[1;32m * \033[0mSwitching back to X..." | $LOGCMD > $VERBOSE

    # if fgconsole is available, the original VT will be stored in this var
    if [ "$ORIGINAL_VT" != "" ]; then
        chvt "$ORIGINAL_VT"
    else
        echo "Original virtual terminal not memorized (trying 7)"
        chvt 7
    fi
    
    return $?
}

ExecuteServices() {
    ret=0
    # What action to execute on init scripts?
    startstop="$1"
    shift
    # What scripts to execute at all?
    services_to_execute=" $@ "

    if [ "$DISTRIB" != "Gentoo" ] ;then
        # in gentoo this does not work       
        runlevel=`runlevel | cut -d\  -f2`

        # only start/stop services really active in this runlevel and
        # do it in the correct order
        if [ "$startstop" = "start" ] ; then
            startscripts="$RLVLDIR/rcS.d/S* $RLVLDIR/rc$runlevel.d/S*"
        elif [ "$startstop" = "stop" ] ; then
            startscripts="$RLVLDIR/rc$runlevel.d/S*"

            # stop services - reverse the order in which the scripts are called
            for script in $startscripts ; do
                tmp_scriptlist="$script $tmp_scriptlist"
            done
            # (order of rc0.d/S* scripts *must not* be reversed!)
            startscripts="$tmp_scriptlist $RLVLDIR/rc0.d/S*"
        fi

        # execute all needed init scripts
        tmp_scriptlist=""
        for startscript in $startscripts ; do
            service=${startscript##*/S??} # strip off directory and startup sequence number
            if [ -z "${services_to_execute##* $service *}" ] ; then
                # service is active, start/stop it
                tmp_scriptlist="$tmp_scriptlist $service"
            fi
        done
        if [ "$startstop" = "START" ] ; then
            startstop="start"
            tmp_scriptlist=""
	    for service in $services_to_execute ; do
		test -x $RLVLDIR/rc$runlevel.d/S??$service || continue
		tmp_scriptlist="$tmp_scriptlist $service"
	    done
        elif [ "$startstop" = "STOP" ] ; then
            startstop="stop"
            tmp_scriptlist=""
	    for service in $services_to_execute ; do
		test -x $RLVLDIR/rc$runlevel.d/S??$service || continue
		tmp_scriptlist="$tmp_scriptlist $service"
	    done
        fi
        echo -e "\033[1;33m$tmp_scriptlist\033[0m" | $LOGCMD > $VERBOSE
        for service in $tmp_scriptlist ; do
            test -x /etc/init.d/"$service" || continue
            /etc/init.d/"$service" "$startstop" 2>&1 | $LOGCMD > $VERBOSE || ret=$? # remember the last error code
        done
    else
        # gentoo use this
        echo -e "\033[1;33m$services_to_execute\033[0m" | $LOGCMD > $VERBOSE
 
        if [ "$startstop" = "stop" ] ; then
            # start services - reverse the order in which the scripts are called
            for script in $services_to_execute ; do
                tmp_scriptlist="$script $tmp_scriptlist"
            done
            startscripts="$tmp_scriptlist"
        else
            startscripts="$services_to_execute"
        fi
        
        # execute all needed init scripts
        for service in $startscripts ; do
            echo -n " $service" | $LOGCMD > $VERBOSE
            /etc/init.d/"$service" "$startstop" 2>&1 | $LOGCMD > $VERBOSE  || ret=$? # remember the last error code
        done
    fi
    echo -e "\033[0m" | $LOGCMD > $VERBOSE
    return $ret # return the last error code
}

StopServices() {
    ExecuteServices "stop" "$SWSUSP_RESTART_SERVICES"
    ExecuteServices "STOP" "$SWSUSP_STOP_SERVICES_BEFORE_SUSPEND"
    return 0 # we don't want to abort if stopping one of the services failed
}

RestartServices() {
    ExecuteServices "start" "$SWSUSP_RESTART_SERVICES"
    ExecuteServices "START" "$SWSUSP_START_SERVICES_AFTER_RESUME"
    return $?
}

UmountDevices() {
    ret=0
    if [ $KILL_PROCS = "yes" ]; then
        for MNT in $SWSUSP_UMOUNTS; do
            if [ `grep -c " $MNT " /proc/mounts` != 0 ]; then
                echo -e "\033[1;32m * \033[0mTerminating users of $MNT..." | $LOGCMD > $VERBOSE
                fuser -s -k -15 $MNT 2> $VERBOSE | $LOGCMD > $VERBOSE
            fi
        done
        #sleep a few seconds before SIGKILL and umount
        sleep 3
    fi

    #Now kill procs and do umount
    for MNT in $SWSUSP_UMOUNTS; do
        if [ `grep -c " $MNT " /proc/mounts` != 0 ]; then
            if [ $KILL_PROCS = "yes" ]; then
                echo -e "\033[1;32m * \033[0mSending SIGKILL to remaining users of $MNT" | $LOGCMD > $VERBOSE
                fuser -s -k -9 $MNT 2> $VERBOSE | $LOGCMD > $VERBOSE
            fi  
            echo -e "\033[1;32m * \033[0mUnmounting $MNT" | $LOGCMD > $VERBOSE
            # make sure everything really is being unmounted
            if ! ( umount $MNT ); then
                ret=1
            fi
        fi
    done
    return $ret
}

RemountDevices() {
    ret=0
    for MNT in $SWSUSP_REMOUNTS ;do
        if [ `grep -c " $MNT " /proc/mounts` = 0 ]; then
            echo -e "\033[1;32m * \033[0mMounting $MNT..." | $LOGCMD > $VERBOSE
            /bin/mount $MNT
        fi
    done
}

StopInterfaces() {
    ret=0
    for IF in $SWSUSP_DOWNIFS; do
        if [ `ifconfig | grep -c "^$IF "` != 0 ]; then
            if [ ! -x $IFDOWN ] ;then
                echo -e "\033[1;32m * \033[0mNo script $IFDOWN !!!" | $LOGCMD > $VERBOSE
                return 2
            else
                echo "Bringing $IF down..." | $LOGCMD > $VERBOSE
                if ! ($IFDOWN $OPTDOWN $IF | $LOGCMD > $VERBOSE) ;then
                    echo -e "\033[1;32m * \033[0mBringing $IF down failed !" | $LOGCMD > $VERBOSE
                    ret=1
                else
                    T_IFS="$IF $T_IFS"
                fi
            fi
        fi
    done
    return $ret
}

RestartInterfaces() {
    ret=0
    if [ "$SWSUSP_UPIFS" = "auto" ] ;then
        SWSUSP_UPIFS="$T_IFS"
    fi
    for IF in $SWSUSP_UPIFS ;do
        echo -e "\033[1;32m * \033[0mBringing up $IF..." | $LOGCMD > $VERBOSE
        if ! ($IFUP $OPTUP $IF | $LOGCMD > $VERBOSE) ;then
            ret=1
        fi
    done
    return $ret
}

UnloadModules() {
    if [ "$SWSUSP_UNLOAD_MODULES_BEFORE_SUSPEND" != "yes" ] ;then
        ret=0
        if [ "$SWSUSP_REMOVEMODS" != "" ] ;then
            for MOD in $SWSUSP_REMOVEMODS ;do
                echo -e "\033[1;32m * \033[0mUnloading $MOD..." | $LOGCMD > $VERBOSE
                if ! (modprobe -r $MOD 2> $VERBOSE 1> $VERBOSE) ;then
                        ret=1
                fi
            done
        fi
        if [ "$SWSUSP_UNLOAD_MODULES_BEFORE_SUSPEND" != "auto" ] ;then
            echo -e "\033[1;32m * \033[0mUnloading autocleaned modules" | $LOGCMD > $VERBOSE
            if ! (rmmod -a 2> $VERBOSE 1> $VERBOSE) ;then
                    ret=1
            fi
        fi
        return $ret
    fi
    cat /proc/modules | $LOGCMD > $VERBOSE
    modules=`awk '($3==0){print $1}' /proc/modules`
    for module in $modules ;do
        echo -e "\033[1;32m * \033[0mRemoving $module" | $LOGCMD > $VERBOSE
        modprobe -r $module 2> $VERBOSE 1> $VERBOSE
        # usleep 100000
    done
    modaft=`awk '($3==0){print $1}' /proc/modules`
    while [ "$modaft" != "$modules" ] ; do
        #cat /proc/modules | $LOGCMD > $VERBOSE
        modules=$modaft
        usleep 500000
        for module in $modules ;do
            echo -e "\033[1;32m * \033[0mRemoving $module" | $LOGCMD > $VERBOSE
            modprobe -r $module 2> $VERBOSE 1> $VERBOSE
        done
        modaft=`awk '($3==0){print $1}' /proc/modules`
    done
    echo -e "\033[1;33mmodules failed to unload:\033[0m" | $LOGCMD > $VERBOSE
    cat /proc/modules | $LOGCMD > $VERBOSE
}

ReloadModules() {
    ret=0
    if [ "$SWSUSP_INSERTMODS" = "modules.autoload" -o "$SWSUSP_INSERTMODS" = "modules" ] ;then
    # for gentoo and ALTLinux users
	echo -e "\033[1;33mLoading modules listed /etc/$SWSUSP_INSERTMODS\033[0m" | $LOGCMD > $VERBOSE
        # daRav: taken from gentoo's /etc/init.d/modules
        # Loop over every line in /etc/modules.autoload.
        (cat /etc/$SWSUSP_INSERTMODS; echo) | # make sure there is a LF at the end
        while read MOD args
        do
            case "${MOD}" in
                \#*|"") continue ;;
            esac
            echo -e "\033[1;32m * \033[0mLoading $MOD..." | $LOGCMD > $VERBOSE
            modprobe ${MOD} ${args} 2> $VERBOSE 1> $VERBOSE
            #sleep 1
        done
    else
        for MOD in $SWSUSP_INSERTMODS ;do
            echo -e "\033[1;32m * \033[0mLoading $MOD..." | $LOGCMD > $VERBOSE
            if ! (modprobe $MOD 2> $VERBOSE 1> $VERBOSE) ;then
                ret=1
                fi
        done
    fi
    return $ret
}

Suspend() {
    ret=0
    if [ "$SWSUSP_SAVE_CLOCK_ON_SUSPEND" = "yes" ] ;then
        echo -e "\033[1;32m * \033[0mSaving clock state..." | $LOGCMD > $VERBOSE
        hwclock --systohc $HWCLKDEBUG
        sleep 1 #This is to be sure that the clock syncing is done
    fi
    if [ -x $SETSYSFONT ];then
        chvt $SWSUSP_VT
    fi

    sync
    if [ "$NOSUSPEND" = "yes" ] ;then
        echo -e "\033[1;31m\nSuspension system call not performed...\n\033[0m" | $LOGCMD > $VERBOSE
        if [ "$SWSUSP_FORCE_SUSPEND_MODE" = "acpi" -o "$SWSUSP_FORCE_SUSPEND_MODE" = "ACPI" ];then
            echo "/bin/echo 4 > /proc/acpi/sleep" | $LOGCMD > $VERBOSE
        else
            echo "/bin/echo \"0 $SWSUSP_FORCE_SUSPEND_MODE\" > /proc/sys/kernel/swsusp" "&& echo > /proc/swsusp/activate"
        fi
    else
        if [ -w "$SWSUSP_DISABLE_HW_DISK_CACHE" ] ;then
            echo -e "\033[1;32m * \033[0mDisabling hardware disk cache on $SWSUSP_DISABLE_HW_DISK_CACHE" | $LOGCMD > $VERBOSE
            hdparm -W 0 $SWSUSP_DISABLE_HW_DISK_CACHE
        fi
        echo -e "\033[1;33m\nSuspension system call in progress...\n\033[0m" | $LOGCMD > $VERBOSE
        if [ "$SWSUSP_FORCE_SUSPEND_MODE" = "acpi" -o "$SWSUSP_FORCE_SUSPEND_MODE" = "ACPI" ];then
            if ! (/bin/echo 4 > /proc/acpi/sleep &) ;then
                ret=1
            fi
        else
            if ! (/bin/echo "0 $SWSUSP_FORCE_SUSPEND_MODE" > /proc/sys/kernel/swsusp && echo > /proc/swsusp/activate) ;then
                ret=1
            fi
        fi
    fi
    # Some people have reported that echo 4 > /proc/sleep acpi never returns.
    # The following kills the echo process if it's still hanging around.
    if [ $ret = 0 ] ;then    
        #sleep 5
        ps ax > $TMP
        pids=`awk '($5 == "/bin/echo"){print $1}' $TMP`
        for pid in $pids; do
            echo -e "\033[1;32m * \033[0mKilling($sig) $prog($pid)..." | $LOGCMD > $VERBOSE
            #kill -$sig $pid 2> $VERBOSE | $LOGCMD > $VERBOSE
            usleep 500000
        done
    fi
    if [ -x $SETSYSFONT ];then
        chvt $SWSUSP_VT
        $SETSYSFONT $OPTSYSFONT
    fi
    return $ret
}

RestoreClock() {
    ret=0
    echo -e "\033[1;32m * \033[0mRestoring clock..." | $LOGCMD > $VERBOSE
    hwclock --hctosys $HWCLKDEBUG | $LOGCMD > $VERBOSE
    ret=$?
    sleep 1 #This is to be sure that the clock syncing is done
    return $ret
}


RESUME() {
    state=$1
    resumestate=7
    
    if [ $state -ge $resumestate ];then
        echo -e "\033[1;32mResuming\033[0m (suspend script vers. $VERSION)..." | $LOGCMD > $VERBOSE
        RestoreClock
    fi

    resumestate=$(($resumestate - 1))
    
    if [ $state -ge $resumestate ];then
        echo -e "\033[1;32mReload modules...\033[0m" | $LOGCMD > $VERBOSE
        ReloadModules
    fi

    resumestate=$(($resumestate - 1))
    
    if [ $state -ge $resumestate ];then
        echo -e "\033[1;32mStarting interfaces...\033[0m" | $LOGCMD > $VERBOSE
        RestartInterfaces
    fi

    resumestate=$(($resumestate - 1))
    
    
    if [ $state -ge $resumestate ];then
        echo -e "\033[1;32mRestarting progs...\033[0m" | $LOGCMD > $VERBOSE
        ExecuteProgs
    fi
        

    resumestate=$(($resumestate - 1))
    
    if [ $state -ge $resumestate ];then
        echo -e "\033[1;32mRestarting services...\033[0m" | $LOGCMD > $VERBOSE
        RestartServices
    fi

    resumestate=$(($resumestate - 1))

    if [ $state -ge $resumestate ];then
        echo -e "\033[1;32mRemounting devices...\033[0m" | $LOGCMD > $VERBOSE
        RemountDevices
    fi

    resumestate=$(($resumestate - 1))

    if [ $state -ge $resumestate ];then
        SwitchToX
    fi
    
    resumestate=$(($resumestate - 1))
    # resumestate should be zero at this point
    return $resumestate
}

FAIL() {
   if [ $FORCE = "yes" ]; then
#  Then continue anyway.
     echo | $LOGCMD > $VERBOSE
     echo "WARNING...FAILSAFES OVERIDDEN..." | $LOGCMD > $VERBOSE
     echo "Do not mount shared drives from other OS's while suspended" | $LOGCMD > $VERBOSE
     echo | $LOGCMD > $VERBOSE
   else
     echo | $LOGCMD > $VERBOSE
     echo "         ********* SUSPEND CANCELLED *********" | $LOGCMD > $VERBOSE
     echo | $LOGCMD > $VERBOSE
     RESUME $1
     rm $LOCKFILE
     echo -e "\033[1;31m\nBack! Suspend failed. Try to configure $CONFIGFILE.\033[0m\n\n" | $LOGCMD > $VERBOSE
     exit 255
   fi 
}





SUSPEND() {
    state=0

    if ! CheckProgs ;then
        FAIL $state
    fi

    if ! SwitchFromX ;then
        FAIL $state
    fi

    echo -e "\033[1;31mPreparing to suspend\033[0m (suspend script vers. $VERSION)..." | $LOGCMD > $VERBOSE

    echo -e "\033[1;31mWaiting for sync...\033[0m" | $LOGCMD > $VERBOSE
    sync 

    if [ "$SWSUSP_MOUNT_USING_AUTOFS" = "yes" ] ;then
	echo -e "\033[1;31mUsing AUTOFS - waiting 7 sec...\033[0m" | $LOGCMD > $VERBOSE
	usleep 7000000
    fi 

    state=$(($state + 1))

    echo -e "\033[1;31mUnmounting devices...\033[0m" | $LOGCMD > $VERBOSE
    if ! UmountDevices ;then
        FAIL $state
    fi

    state=$(($state + 1))

    echo -e "\033[1;31mStopping services...\033[0m" | $LOGCMD > $VERBOSE
    if ! StopServices ;then
        FAIL $state
    fi
    
    state=$(($state + 1))

    echo -e "\033[1;31mKilling progs...\033[0m" | $LOGCMD > $VERBOSE
    if ! KillProgs ;then
        FAIL $state
    fi

    state=$(($state + 1))

    echo -e "\033[1;31mStopping interfaces...\033[0m" | $LOGCMD > $VERBOSE
    if ! StopInterfaces ;then
        FAIL $state
    fi

    state=$(($state + 1))

    echo -e "\033[1;31mUnloading modules...\033[0m" | $LOGCMD > $VERBOSE
    if ! UnloadModules ;then
        FAIL $state
    fi

    state=$(($state + 1))

    if ! Suspend ;then
        FAIL $state
    fi
    state=$(($state + 1))
    return $state
}

#############################################################
# Main 
#############################################################

if [ -f $LOCKFILE ] ;then
    pid=`cat $LOCKFILE`
    if (ps $pid > $VERBOSE 2> $VERBOSE) ;then
        echo "File $LOCKFILE is present. Suspension script `cat $LOCKFILE`"
        echo "should be in progress. If this is not the case, then erase"
        echo "this file"
        exit 1
    fi
fi
echo $$ > $LOCKFILE

SUSPEND
state=$?
RESUME $state
ret=$?
rm $LOCKFILE

echo -e "\033[1;32m\nWelcome back! Suspend done.\033[0m\n\n" | $LOGCMD > $VERBOSE

exit $ret

# vi:ts=8:
