#!/bin/sh # -*- sh -*- # vim:ft=sh:ts=8:sw=4:noet # # Hibernate Script # Copyright (C) 2004-2006 Bernard Blackham # # The hibernate-script package is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program. If not, write to the Free Software Foundation, Inc., 59 Temple # Place - Suite 330, Boston, MA 02111-1307, USA. # umask 077 USING_ZSH= USING_BASH= NEED_POSIX= # For zsh sanity... # allows splitting strings on whitespace in zsh. setopt SH_WORD_SPLIT 2>/dev/null && USING_ZSH=1 || true # allows sourced files to know they're sourced in zsh. unsetopt FUNCTION_ARGZERO 2>/dev/null || true # Detect bash and tell it not to be POSIX, because we like it better that way. shopt > /dev/null 2>&1 && USING_BASH=1 [ -n "$USING_BASH" ] && set +o posix 2>/dev/null [ -z "$USING_BASH$USING_ZSH" ] && NEED_POSIX=1 # We prefer to use dash if we've got it. if [ -z "$NEED_POSIX" ] && [ -z "$TRIED_DASH" ] && command -v dash > /dev/null 2>&1 ; then if [ "${0##*/}" = "$0" ] ; then myself=`command -v $0` || myself= elif [ -x "$0" ] ; then myself=$0 fi TRIED_DASH=1 export TRIED_DASH [ -n "$myself" ] && exec dash $myself $* fi unset TRIED_DASH SWSUSP_D="/etc/hibernate" SCRIPTLET_PATH="$SWSUSP_D/scriptlets.d /usr/local/share/hibernate/scriptlets.d /usr/share/hibernate/scriptlets.d" DEFAULT_CONFIG_FILE="$SWSUSP_D/hibernate.conf" EXE="${0##*/}" VERSION="1.99" # Add these to the $PATH just in case. PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin" export PATH # vecho N : acts like echo but with verbosity control - If it's # high enough to go to stdout, then it'll get logged as well. Else write it to # the log file if it needs to. Otherwise, ignore it. vecho() { local v v="$1" shift [ "x$LOG_TIMESTAMP" = "x1" ] && set -- $(date "+%b %e %H:%M:%S.%2N") "$@" if [ "$v" -le $VERBOSITY ] ; then echo $@ else if [ "$v" -le $LOG_VERBOSITY -a "$LOGPIPE" != "cat" ] ; then echo "$@" | $LOGPIPE > /dev/null fi fi } # vcat N : acts like cat but with verbosity control - If it's # high enough to go to stdout, then it'll get logged as well. Else write it to # the log file if it needs to. Otherwise, ignore it. vcat() { local v v="$1" shift if [ "$v" -le $VERBOSITY ] ; then cat "$@" else if [ "$v" -le $LOG_VERBOSITY -a "$LOGPIPE" != "cat" ] ; then cat "$@" | $LOGPIPE > /dev/null else cat > /dev/null fi fi } ############################################################################## ### The following functions can be called safely from within scriptlets ###### ############################################################################## # AddSuspendHook NN name: Adds a function to the suspend chain. NN must be a # number between 00 and 99, inclusive. Smaller numbers get called earlier on # suspend. AddSuspendHook() { SUSPEND_BITS="$1$2\\n$SUSPEND_BITS" } SUSPEND_BITS="" # AddResumeHook NN name: Adds a function to the resume chain. NN must be a number # between 00 and 99, inclusive. Smaller numbers get called later on resume. AddResumeHook() { RESUME_BITS="$1$2\\n$RESUME_BITS" } RESUME_BITS="" # AddTerminationHandler : adds the given function to the chain of # functions to handle termination cleanups. AddTerminationHandler() { TERMINATION_HANDLERS="$TERMINATIONS_HANDLERS $1" } TERMINATION_HANDLERS="" # AddConfigHandler : adds the given function to the chain of # functions to handle extra configuration options. AddConfigHandler() { CONFIG_OPTION_HANDLERS="$CONFIG_OPTION_HANDLERS $1" } CONFIG_OPTION_HANDLERS="" # AddOptionHandler : adds the given function to the chain of # functions to handle extra command line options. The scriptlet must also # register the options with AddShortOption or AddLongOption AddOptionHandler() { CMDLINE_OPTION_HANDLERS="$CMDLINE_OPTION_HANDLERS $1" } CMDLINE_OPTION_HANDLERS="" # AddShortOption