#! /bin/bash

export LANG=C

if ( echo $@ | /bin/grep -e "--version" ) 1> /dev/null; then
    echo "Script:       $0"
    echo "Installed by: suspend.sh"
    echo "Installation: Sun Jul 27 13:32:26 JST 2003"
    echo "Distribution: Momonga"
    echo "Version:      0.14.2"
    exit 0
fi

CONFIGFILE=/etc/suspend.conf
INITDIR=/etc/init.d
RLVLDIR=
IFDOWN=/etc/init.d/network
OPTDOWN=stop
IFUP=/etc/init.d/network
OPTUP=start
LOCKFILE=/var/lock/subsys/swsusp
SETSYSFONT=/sbin/setsysfont
OPTSYSFONT=
DISTRIB=Momonga

# default: strg-alt-f9 = vt9 = /dev/tty9
VERBOSE_VT='9'
VERBOSE_CONSOLE="/dev/tty$VERBOSE_VT"

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

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



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 $@ | /bin/grep -e "--silent" ) 1> /dev/null; then
    VERBOSE='/dev/null'
    HWCLKDEBUG=""
else
    if [ -w $VERBOSE_CONSOLE ] ;then
	VERBOSE="$VERBOSE_CONSOLE"
    elif [ -w /dev/tty ] ;then
	VERBOSE='/dev/tty'
    else
	VERBOSE='/dev/console'
    fi
    HWCLKDEBUG="--debug"
fi

LOGCMD="tee -a $VERBOSE"

if ( echo $@ | /bin/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 $@ | /bin/grep -e "--force" ) 1> $VERBOSE; then
    FORCE='yes'
else
    FORCE='no'
fi


EXE=`basename $0`
/bin/cat > $TMP <<EOF
The script file $EXE uses a configuration file $CONFIGFILE.
It is intended to cleanly call software suspension stuff. Its 
options are as follows:
--help		Prints this help.
--install	Install this file in $INSTALLDIR and a configuration
		template as $CONFIGFILE. Existing script will be 
		overwritten, but not configuration file, unless --force
		is used. 
--force		Ignore devices that cannot be stopped or unmounted.
                Overwrite existing configuration file in installation
		procedure. 
--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

if [ -f "$CONFIGFILE" ] ; then
    . $CONFIGFILE
else
    echo "No configuration file $CONFIGFILE !!!"
    /bin/cat $TMP
    /bin/rm $TMP
    exit 1
fi

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

[ `/bin/cat /proc/swaps | /bin/grep partition | /usr/bin/wc -l` -lt 1 ] && {
    echo "No swap partition activated, cannot suspend"
    exit 0
}

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

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


# This does everything except suspension
if ( echo $@ | /bin/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() {
		# 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
		/bin/ps ax > $TMP
		pids=`/bin/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
	/bin/ps ax > $TMP
	pids=`/bin/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 && /bin/rm $TMP
    return $ret
}

KillProgs() {
    for prog in $SWSUSP_STOP_PROGS_BEFORE_SUSPEND; do
	    echo -e "\033[1;32m * \033[0mKilling $prog..." | $LOGCMD > $VERBOSE
	    /usr/bin/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
    /usr/bin/chvt $VERBOSE_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
	/usr/X11R6/bin/xauth 2> $VERBOSE 1> $VERBOSE <<EOF
add :3 . 00
generate :3.0 .
EOF
	if [ -x /usr/bin/X11/xmessage -a "$DISTRIB" != "Gentoo" ] ;then
	    /usr/X11R6/bin/xinit /usr/bin/X11/xmessage -font huge -display :3.0 -center -timeout 2 "  Fake Xserver for restoring nvidia cards properly  " -- :3
	else
	    /usr/X11R6/bin/xinit /bin/false -- :3 2> $VERBOSE 1> $VERBOSE
	fi
	/usr/X11R6/bin/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
	/usr/bin/chvt "$ORIGINAL_VT"
    else
	echo "Original virtual terminal not memorized (trying 7)"
	/usr/bin/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=`/sbin/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=" $services_to_execute "
	elif [ "$startstop" = "STOP" ] ; then
	    startstop="stop"
	    tmp_scriptlist=" $services_to_execute "
	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 [ `/bin/grep -c " $MNT " /proc/mounts` != 0 ]; then
		echo -e "\033[1;32m * \033[0mTerminating users of $MNT..." | $LOGCMD > $VERBOSE
		/sbin/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 [ `/bin/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
		/sbin/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 [ `/bin/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 [ `/sbin/ifconfig | /bin/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
	return 0
    fi
    /bin/cat /proc/modules | $LOGCMD > $VERBOSE
    modules=`/bin/awk '($3==0){print $1}' /proc/modules`
    for module in $modules ;do
	echo -e "\033[1;32m * \033[0mRemoving $module" | $LOGCMD > $VERBOSE
	/sbin/modprobe -r $module 2> $VERBOSE 1> $VERBOSE
#	usleep 100000
    done
    modaft=`/bin/awk '($3==0){print $1}' /proc/modules`
    while [ "$modaft" != "$modules" ] ; do
	#/bin/cat /proc/modules | $LOGCMD > $VERBOSE
	modules=$modaft
	usleep 500000
	for module in $modules ;do
	    echo -e "\033[1;32m * \033[0mRemoving $module" | $LOGCMD > $VERBOSE
	    /sbin/modprobe -r $module 2> $VERBOSE 1> $VERBOSE
#	    usleep 100000
	done
	modaft=`/bin/awk '($3==0){print $1}' /proc/modules`	
    done
    echo -e "\033[1;33mmodules failed to unload:\033[0m" | $LOGCMD > $VERBOSE
    /bin/cat /proc/modules | $LOGCMD > $VERBOSE
}

ReloadModules() {
    ret=0
    if [ "$SWSUSP_INSERTMODS" = "modules.autoload" ] ;then
    # for gentoo users
	echo -e "\033[1;33mLoading modules listed /etc/modules.autoload\033[0m" | $LOGCMD > $VERBOSE
	# daRav: taken from gentoo's /etc/init.d/modules
        # Loop over every line in /etc/modules.autoload.
        (/bin/cat /etc/modules.autoload; 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
	    /sbin/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 ! (/sbin/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
	/sbin/hwclock --systohc $HWCLKDEBUG
	/bin/sleep 1 #This is to be sure that the clock syncing is done
    fi
    if [ -x $SETSYSFONT ];then
	/usr/bin/chvt $VERBOSE_VT
    fi

    /bin/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
	    /sbin/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
    if [ $ret = 0 ] ;then    
	sleep 5
	/bin/ps ax > $TMP
	pids=`/bin/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
	/usr/bin/chvt $VERBOSE_VT
	$SETSYSFONT $OPTSYSFONT
    fi
    return $ret
}

RestoreClock() {
    ret=0
    echo -e "\033[1;32m * \033[0mRestoring clock..." | $LOGCMD > $VERBOSE
    /sbin/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;32mRemounting devices...\033[0m" | $LOGCMD > $VERBOSE
	RemountDevices
    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
	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
     /bin/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

    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;31mUnmounting devices...\033[0m" | $LOGCMD > $VERBOSE
    if ! UmountDevices ;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=`/bin/cat $LOCKFILE`
    if (/bin/ps $pid > $VERBOSE 2> $VERBOSE) ;then
	echo "File $LOCKFILE is present. Suspension script `/bin/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=$?
/bin/rm $LOCKFILE

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

exit $ret
