File: hibernate.sh

package info (click to toggle)
hibernate 2.0+15+g88d54a8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, jessie-kfreebsd, sid, wheezy
  • size: 740 kB
  • ctags: 114
  • sloc: sh: 1,223; makefile: 17
file content (927 lines) | stat: -rw-r--r-- 26,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#!/bin/sh
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet
#
# Hibernate Script
# Copyright (C) 2004-2006 Bernard Blackham <bernard@blackham.com.au>
#
# 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="2.0"

# Add these to the $PATH just in case.
PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"
export PATH

# vecho N <echo params>: 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 <cat params>: 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 <function name>: adds the given function to the chain of
# functions to handle termination cleanups.
AddTerminationHandler() {
    TERMINATION_HANDLERS="$TERMINATIONS_HANDLERS $1"
}
TERMINATION_HANDLERS=""

# AddConfigHandler <function name>: 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 <function name>: 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 <option char>: adds the given option character to the
# list of possible short options. The option may be followed by : or ::
# (: for a mandatory parameter, :: for an optional parameter)
AddShortOption() {
    EXTRA_SHORT_OPTS="$EXTRA_SHORT_OPTS$1"
}
EXTRA_SHORT_OPTS=""

# AddLongOption <option char>: adds the given option character to the
# list of possible long options. The option may be followed by : or ::
# (: for a mandatory parameter, :: for an optional parameter)
AddLongOption() {
    EXTRA_LONG_OPTS="$EXTRA_LONG_OPTS,$1"
}
EXTRA_LONG_OPTS=""

# AddOptionHelp <option name> <option help>: Adds the given option name and
# help text to the help screen.
AddOptionHelp() {
    [ -n "$DISABLE_HELP" ] && return
    local ATEXT
    local BIT
    local WRAPPED_HELP
    ATEXT="  $1"
    [ -n "$CURRENT_SOURCED_SCRIPTLET" ] && ATEXT=`LeftRightPadText "$ATEXT" "[$CURRENT_SOURCED_SCRIPTLET]"`
    WRAPPED_HELP="`echo \"$2\" | WrapHelpText`"
    ATEXT="$ATEXT
$WRAPPED_HELP

"
    CMDLINE_OPTIONS_HELP="$CMDLINE_OPTIONS_HELP$ATEXT"
}
CMDLINE_OPTIONS_HELP=""

# AddConfigHelp <item name> <item help>: Deprecated. We no longer write
# config help in --help
AddConfigHelp() {
    return 0
}

# FindXServer: Tries to find a running X server on the system. If one is found,
# will set DISPLAY, XAUTHORITY and the XUSER variable, and return 0.
# Otherwise, returns 1.
FindXServer() {
    [ -n "$FIND_X_SERVER_RESULT" ] && return $FIND_X_SERVER_RESULT

    [ -z "$DISPLAY" ] && DISPLAY=:0
    export DISPLAY

    # See if we need to authenticate to this X server.
    PATH=$PATH:/usr/bin/X11:/usr/X11R6/bin
    export PATH
    xhost=`command -v xhost 2>/dev/null`
    if [ $? -ne 0 ] ; then
	vecho 0 "$EXE: Could not find xhost program. Disabling X scriptlets."
	FIND_X_SERVER_RESULT=1
	return 1
    fi

    # Find a useful XAUTHORITY and ideally a username too if we can!
    local xuser xauth xpid
    for display in $(seq 0 8); do
	xuser=$(last ":$display" | grep 'still logged in' | awk '{print $1}') || true
	if [ -n "$xuser" ]; then
	    xhome=$(getent passwd "$xuser" | cut -d: -f 6)
	    if [ -n "$xhome" ]; then
		xauth=$xhome/.Xauthority
	    fi
	fi
	if [ -z "$xhome" ] || [ -z "$xauth" ]; then
	    xauth=
	    xuser=
	    xhome=
	else 
	    break
	fi
    done
    if [ -z $xuser ] ; then
        for xpid in `pidof kwrapper ksmserver kdeinit gnome-session fvwm fvwm2 pwm blackbox fluxbox WindowMaker X XFree86 Xorg` ; do
	    # Ensure the process still exists, and we aren't hallucinating.
	    [ -d "/proc/$xpid/" ] || continue

	    xauth="`get_env_var_of_process $xpid XAUTHORITY`"
	    xhome="`get_env_var_of_process $xpid HOME`"
	    xuser=`/bin/ls -ld /proc/$xpid/ | awk '{print $3}'`
	    [ -z $xauth ] && [ -n $xhome ] && [ -f $xhome/.Xauthority ] && xauth=$xhome/.Xauthority

	    [ -z $xauth ] && continue

	    XAUTHORITY=$xauth su $xuser -c "$xhost" > /dev/null 2>&1 && break

	   xauth=
	   xuser=
       done
    fi
    if [ -n $xuser ] ; then
	XUSER=$xuser
	if [ -n "$xauth" ] ; then
	    XAUTHORITY=$xauth
	    export XAUTHORITY
	fi
    else
	FIND_X_SERVER_RESULT=1
	vecho 0 "$EXE: Could not attach to X server on $DISPLAY. Disabling X scriptlets."
	return 1
    fi

    FIND_X_SERVER_RESULT=0
    return 0
}

# UsingSuspendMethod <method name>: is called when a suspend method is chosen.
# If a suspend method has already been chosen, the hibernate script will be
# terminated with an error.
UsingSuspendMethod() {
    if [ -n "$HIBERNATE_SUSPEND_METHOD" ] ; then
	vecho 0 "$EXE: More than one suspend method has been chosen."
	vecho 0 "Please edit your config file so only one method is used."
	exit 1
    fi
    HIBERNATE_SUSPEND_METHOD=$1
    return 0
}

##############################################################################
### Helper functions                                                       ###
##############################################################################

# SortSuspendBits: Returns a list of functions registered in the correct order
# to call for suspending, prefixed by their position number in the suspend
# chain.
SortSuspendBits() {
    # explicit path required to be ash compatible.
    /bin/echo -ne "$SUSPEND_BITS" | sort -n
}

# SortResumeBits: Returns a list of functions registered in the correct order
# to call for resuming, prefixed by their position number.
SortResumeBits() {
    # explicit path required to be ash compatible.
    /bin/echo -ne "$RESUME_BITS" | sort -rn
}

# WrapHelpText: takes text from stdin, wraps it with an indent of 5 and width
# of 70, and writes to stdout.
WrapHelpText() {
    awk '
BEGIN {
    indent=5
    width=70
    ORS=""
}
{
    if (substr($0, 1, 1) == " ")
	for(a=1;a<length($0);a++) {
	    if (substr($0,a,1) != " ") break
	    print " "
	}
    curpos=0
    for (i=1; i <= NF; i++) {
	if ($i != "" && i == 1) {
	    for (j=0; j < indent; j++) { print " " }
	}
	if (curpos + length($i) > width) {
	    curpos=0
	    print "\n"
	    for (j=0; j < indent; j++) { print " " }
	}
	print $i " "
	curpos = curpos + length($i) + 1
    }
    print "\n"
}
END {
    print "\n"
}
'
}

# LeftRightPadText <left> <right>": returns a string comprised of the two
# arguments with padding in between to make the string 78 characters long.
LeftRightPadText() {
    (echo "$1" ; echo "$2") | awk '
BEGIN {
    OFS=""; ORS="";
    getline
    print
    a=78-length()
    getline
    for(a-=length(); a>=0; a--) {print " "}
    print
}'

}

# PluginTermination <params>: queries all loaded scriptlets if they want to
# handle the given event. Returns 0 if the option was handled, 1 otherwise.
PluginTermination() {
    local i
    for i in $TERMINATION_HANDLERS ; do
	$i $@ && return 0
    done
    return 1
}

# PluginConfigOption <params>: queries all loaded scriptlets if they want to
# handle the given option. Returns 0 if the option was handled, 1 otherwise.
PluginConfigOption() {
    local i
    for i in $CONFIG_OPTION_HANDLERS ; do
	$i $@ && return 0
    done
    return 1
}

# EnsureHavePrerequisites: makes sure we have all the required utilities to run
# the script. It exits the script if we don't.
EnsureHavePrerequisites() {
    local i
    for i in awk grep sort getopt basename chvt ; do
	if ! command -v $i > /dev/null; then
	    vecho 0 "Could not find required program \"$i\". Aborting."
	    exit 1
	fi
    done
    # Improvise printf using awk if need be.
    if ! command -v printf > /dev/null 2>&1 ; then
	# This implementation fails on strings that contain double quotes.
	# It does the job for the help screen at least.
	printf() {
	    local AWK_FMT
	    local AWK_PARAMS
	    AWK_FMT="$1"
	    shift
	    AWK_PARAMS=""
	    for i in "$@" ; do
		AWK_PARAMS="$AWK_PARAMS, \"$i\""
	    done
	    awk "BEGIN { printf ( \"$AWK_FMT\" $AWK_PARAMS ) }"
	}
    fi
    # Improvise mktemp in case we need it too!
    if ! command -v mktemp > /dev/null 2>&1 ; then
	# Use a relatively safe equivalent of mktemp. Still suspectible to race
	# conditions, but highly unlikely.
	mktemp() {
	    local CNT
	    local D
	    local FN
	    local RND1
	    local RND2
	    CNT=1
	    if [ -z "$RANDOM" ] ; then
	    	# A fix for shells that do not have this bash feature
		RND1=$(dd if=/dev/urandom count=1 2>/dev/null|cksum|cut -c"1-5")
		RND2=$(dd if=/dev/urandom count=1 2>/dev/null|cksum|cut -c"1-5")
	    else
	        RND1=$RANDOM
		RND2=$RANDOM
	    fi
	    while true ; do
		D=`date +%s`
		FN=/tmp/tmp.hibernate.$$$D$RND1$RND2$CNT
		[ -f $FN ] && continue
		touch $FN && break
		CNT=$(($CNT+1))
	    done
	    echo $FN
	}
    fi
    return 0
}

# Usage: dump the abridged usage options to stdout.
Usage() {
    cat <<EOT
Usage: $EXE [options]
Activates software suspend and control its parameters.

$CMDLINE_OPTIONS_HELP
For configuration file options for hibernate.conf, man hibernate.conf.

Hibernate Script $VERSION                      (C) 2004-2006 Bernard Blackham
EOT
    return
}

# PluginGetOpt <params>: pass the given params to each scriplet in turn that
# requested parameters until one accepts them. Return 0 if a scriplet did
# accept them, and 1 otherwise.
PluginGetOpt() {
    local opthandler
    for opthandler in $CMDLINE_OPTION_HANDLERS ; do
	$opthandler $* && return 0
    done
    return 1
}

# DoGetOpt <getopt output>: consume getopt output and set options accordingly.
DoGetOpt() {
    local opt
    local optdata
    while [ -n "$*" ] ; do
	opt="$1"
	shift
	case $opt in
	    -F|--config-file)
		# Dealt with previously
		shift
		;;
	    -f|--force)
		FORCE_ALL=1
		;;
	    -k|--kill)
		KILL_PROGRAMS=1
		;;
	    --dry-run)
		OPT_DRY_RUN=1
		;;
	    -v|--verbosity)
		OPT_VERBOSITY="${1#\'}"
		OPT_VERBOSITY="${OPT_VERBOSITY%\'}"
		EnsureNumeric "verbosity" $OPT_VERBOSITY && VERBOSITY="$OPT_VERBOSITY"
		shift
		;;
	    -q)
		;;
	    --)
		;;
	    *)
		# Pass off to scriptlets See if there's a parameter given.
		case $1 in
		    -*)
			optdata=""
			;;
		    *)
			optdata=${1#\'}
			optdata=${optdata%\'}
			shift
		esac
		if ! PluginGetOpt $opt $optdata ; then
		    echo "Unknown option $opt on command line!"
		    exit 1
		fi
		;;
	esac
    done
}

# ParseOptions <options>: process all the command-line options given
ParseOptions() {
    local opts
    opts="`getopt -n \"$EXE\" -o \"Vhfksv:nqF:$EXTRA_SHORT_OPTS\" -l \"help,force,kill,verbosity:,dry-run,config-file:,version$EXTRA_LONG_OPTS\" -- \"$@\"`" || exit 1
    DoGetOpt $opts
}

# CheckImplicitAlternateConfig <$0> : checks $0 to see if we should be
# implicitly using a different configuration file.
CheckImplicitAlternateConfig() {
    # Don't find one if we've already been specified one.
    [ -n "$CONFIG_FILE" ] && return 0

    local self
    self="${0##*/}"
    case $self in
	hibernate-*) 
	    CONFIG_FILE="$SWSUSP_D/${self#hibernate-}.conf"
	    vecho 3 "$EXE: Using implicit configuration file $CONFIG_FILE"
	    ;;
	*)
	    CONFIG_FILE="${DEFAULT_CONFIG_FILE}"
	    ;;
    esac
    return 0
}

# PreliminaryGetopt <options> : detects a few command-line options that need to
# be dealt with before scriptlets and before other command-line options.
PreliminaryGetopt() {
    local opt
    set -- `getopt -q -o hv:F: -l help,verbosity:,config-file:,version -- "$@"`
    while [ -n "$*" ] ; do
	opt="$1"
	shift

	case $opt in
	    -h|--help) HELP_ONLY=1 ;;
	    -F|--config-file)
		CONFIG_FILE="${1#\'}"
		CONFIG_FILE="${CONFIG_FILE%\'}"
		;;
	    -v|--verbosity)
		OPT_VERBOSITY="${1#\'}"
		OPT_VERBOSITY="${OPT_VERBOSITY%\'}"
		EnsureNumeric "verbosity" $OPT_VERBOSITY && VERBOSITY="$OPT_VERBOSITY"
		shift
		;;
	    --version)
		echo "Hibernate Script $VERSION"
		exit 0
		;;
	    --) return 0 ;;
	esac
    done
    return 0
}

# LoadScriptlets: sources all scriptlets in $SCRIPTLET_PATH directories
LoadScriptlets() {
    local prev_pwd
    local scriptlet
    local scriptlet_name
    local scriptlet_dir
    local prev_path
    CURRENT_SOURCED_SCRIPTLET=""
    for scriptlet_dir in $SCRIPTLET_PATH ; do
	[ -d "$scriptlet_dir" ] || continue
	[ -z "`/bin/ls -1 $scriptlet_dir`" ] && continue
	for scriptlet in $scriptlet_dir/* ; do
	    # Avoid editor backup files.
	    case "$scriptlet" in *~|*.bak) continue ;; esac

	    # Don't source a scriptlet by name more than once.
	    scriptlet_name="${scriptlet##*/}"

	    eval "prev_path=\"\${HAVE_SOURCED_SCRIPTLET_$scriptlet_name}\""

	    if [ -n "$prev_path" ] ; then
		vecho 0 "$EXE: Scriptlet $scriptlet_name exists in both $scriptlet and $prev_path"
		vecho 0 "$EXE: Cowardly refusing to load $scriptlet_name a second time."
		continue
	    fi
	    eval "HAVE_SOURCED_SCRIPTLET_$scriptlet_name=$scriptlet"

	    # And now source it!

	    CURRENT_SOURCED_SCRIPTLET="$scriptlet_name"
	    . $scriptlet
	done
    done
    if [ -z "$CURRENT_SOURCED_SCRIPTLET" ] ; then
	echo "WARNING: No directories in scriptlet search path contained any scriptlets."
	echo "Hence, this script probably won't do anything."
	return 0
    fi
    CURRENT_SOURCED_SCRIPTLET=""
}

# BoolIsOn <option> <value>: converts a "boolean" to either 1 or 0, and takes
# into account yes/no, on/off, 1/0, etc. If it is not valid, it will complain
# about the option and exit. Note, the *opposite* is actually returned, as true
# is considered 0 in shell world.
BoolIsOn() {
    local val
    val=`echo $2|tr '[A-Z]' '[a-z]'`
    [ "$val" = "on" ] && return 0
    [ "$val" = "off" ] && return 1
    [ "$val" = "true" ] && return 0
    [ "$val" = "false" ] && return 1
    [ "$val" = "yes" ] && return 0
    [ "$val" = "no" ] && return 1
    [ "$val" = "1" ] && return 0
    [ "$val" = "0" ] && return 1
    echo "$EXE: Invalid boolean value ($2) for option $1 in configuration file"
    exit 1
}

IsANumber() {
    case "$1" in *[!0-9]*|"") return 1 ;; esac
    return 0
}

EnsureNumeric() {
    IsANumber "$2" && return 0
    echo "$EXE: Invalid numeric value ($2) for option $1 in configuration file"
    exit 1
}

# ProcessConfigOption: takes a configuration option and its parameters and
# passes it out to the relevant scriptlet.
ProcessConfigOption() {
    local option
    local params
    option=`echo $1|tr '[A-Z]' '[a-z]'`
    shift
    params="$@"
    case $option in
	alwaysforce)
	    [ -z "$FORCE_ALL" ] &&
		BoolIsOn "$option" "$params" && FORCE_ALL=1
	    ;;
	alwayskill)
	    [ -z "$KILL_PROGRAMS" ] &&
		BoolIsOn "$option" "$params" && KILL_PROGRAMS=1
	    ;;
	logfile)
	    [ -z "$LOGFILE" ] &&
		LOGFILE="$params"
	    ;;
	logtimestamp)
	    if BoolIsOn "$option" "$params" ; then
		LOG_TIMESTAMP=1
	    else
		LOG_TIMESTAMP=0
	    fi
	    ;;
	logverbosity)
	    EnsureNumeric "$option" "$params" && LOG_VERBOSITY="$params"
	    ;;
	swsuspvt|hibernatevt)
	    EnsureNumeric "$option" "$params" && SWSUSPVT="$params"
	    ;;
	verbosity)
	    [ -z "$OPT_VERBOSITY" ] && EnsureNumeric "$option" "$params" &&
		VERBOSITY="$params"
	    ;;
	distribution)
	    [ -z "$DISTRIBUTION" ] && DISTRIBUTION=`echo $params | tr '[A-Z]' '[a-z]'`
	    ;;
	xdisplay)
	    DISPLAY="$params"
	    export DISPLAY
	    ;;
        include)
	    vecho 3 "$EXE: Including configuration from $params"
	    if ! ReadConfigFile "$params" ; then
		echo "$EXE: Unable to read configuration file $params (from Include directive)."
		exit 1
	    fi
	    ;;
	trymethod)
	    if [ -z "$HIBERNATE_SUSPEND_METHOD" ] ; then
		NO_COMPLAIN_UNSUPPORTED=1
		vecho 1 "$EXE: Trying method in $1..."
		if ! ReadConfigFile "$params" ; then
		    echo "$EXE: Unable to read configuration file $params (from TryMethod directive)."
		fi
		NO_COMPLAIN_UNSUPPORTED=
	    fi
	    ;;
	*)
	    if ! PluginConfigOption $option $params ; then
		# See if we're trying something new, and haven't yet got a suspend method
		if [ -n "$NO_COMPLAIN_UNSUPPORTED" ] && [ -z "$HIBERNATE_SUSPEND_METHOD" ] ; then
		    return 1
		else
		    echo "$EXE: Unknown configuration option ($option)"
		    exit 1
		fi
	    fi
	    ;;
    esac
    return 0
}

# ReadConfigFile: reads in a configuration file from stdin and sets the
# appropriate variables in the script. Returns 0 on success, exits on errors
conffiles_read=
ReadConfigFile() {
    local option params
    local file_name; file_name="$1"
    
    if [ ! -f "${file_name}" ] ; then
	# Search in /etc/hibernate
	if [ -f "$SWSUSP_D/$file_name" ] ; then
	    file_name="$SWSUSP_D/$file_name"
	else
	    echo "WARNING: No configuration file found (${file_name})."
	    echo "This script probably won't do anything."
	    return 1
	fi
    fi

    if [ -n "$(echo ${conffiles_read} | grep ${file_name})" ]; then
  	vecho 2 "$EXE: Skipping already included config file $file_name"
  	return 0
    fi

    conffiles_read="${conffiles_read} ${file_name}"

    while true ; do
	# Doing the read this way allows means we don't require a new-line
	# at the end of the file.
	read option params
	[ $? -ne 0 ] && [ -z "$option" ] && break
	[ -z "$option" ] && continue
	case $option in ""|\#*) continue ;; esac # avoids a function call (big speed hit)
	ProcessConfigOption $option $params || break
    done < ${file_name}
    return 0
}

# AddInbuiltHelp: Documents the above in-built options.
AddInbuiltHelp() {
    AddOptionHelp "-h, --help" "Shows this help screen."
    AddOptionHelp "--version" "Shows the Hibernate Script version."
    AddOptionHelp "-f, --force" "Ignore errors and suspend anyway."
    AddOptionHelp "-k, --kill" "Kill processes if needed, in order to suspend."
    AddOptionHelp "-v<n>, --verbosity=<n>" "Change verbosity level (0 = errors only, 3 = verbose, 4 = debug)"
    AddOptionHelp "-F<file>, --config-file=<file>" "Use the given configuration file instead of the default ($CONFIG_FILE)"
    AddOptionHelp "--dry-run" "Don't actually do anything."

    AddConfigHelp "HibernateVT N" "If specified, output from the suspend script is redirected to the given VT instead of stdout."
    AddConfigHelp "Verbosity N" "Determines how verbose the output from the suspend script should be:
   0: silent except for errors
   1: print steps
   2: print steps in detail
   3: print steps in lots of detail
   4: print out every command executed (uses -x)"
    AddConfigHelp "LogFile <filename>" "If specified, output from the suspend script will also be redirected to this file - useful for debugging purposes."
    AddConfigHelp "LogTimestamp <boolean>" "If logging to file, will place timestamps on each log entry. This is only recommended if you are trying to optimise suspend/resume time, otherwise it will just slow the process down."
    AddConfigHelp "LogVerbosity N" "Same as Verbosity, but controls what is written to the logfile."
    AddConfigHelp "AlwaysForce <boolean>" "If set to yes, the script will always run as if --force had been passed."
    AddConfigHelp "AlwaysKill <boolean>" "If set to yes, the script will always run as if --kill had been passed."
    AddConfigHelp "Distribution <debian|fedora|mandrake|redhat|gentoo|suse|slackware>" "If specified, tweaks some scriptlets to be more integrated with the given distribution."
    AddConfigHelp "Include <filename>" "Immediately include configuration directives from the given file."
    AddConfigHelp "XDisplay <display location>" "Specifies where scriptlets that use the X server should find one. (Default: :0)"
}

EnsureHaveRoot() {
    if [ x"`id -u`" != "x0" ] ; then
	echo "$EXE: You need to run this script as root."
	exit 1
    fi
    return 0
}

# DoWork: Does the actual calling of scriptlet functions. We wrap this to make
# it easy to decide whether or not to pipe its output to $LOGPIPE or not.
DoWork() {
    # Trap Ctrl+C
    trap ctrlc_handler INT HUP

    # Do everything we need to do to suspend. If anything fails, we don't
    # suspend.  Suspend itself should be the last one in the sequence.

    local ret
    local CHAIN_UP_TO
    local bit

    CHAIN_UP_TO=0
    for bit in `SortSuspendBits` ; do
	local new_CHAIN_UP_TO
	new_CHAIN_UP_TO="`awk \"BEGIN{print substr(\\\"$bit\\\", 1, 2)}\"`" || break
	[ -n "$new_CHAIN_UP_TO" ] && CHAIN_UP_TO=$new_CHAIN_UP_TO || continue
	bit=${bit##$CHAIN_UP_TO}
	vecho 1 "$EXE: [$CHAIN_UP_TO] Executing $bit ... "
	[ -n "$OPT_DRY_RUN" ] && continue
	$bit
	ret="$?"
	# A return value >= 2 denotes we can't go any further, even with --force.
	if [ $ret -ge 2 ] ; then
	    # If the return value is 3 or higher, be silent.
	    if [ $ret -eq 2 ] ; then
		vecho 1 "$EXE: $bit refuses to let us continue."
		vecho 0 "$EXE: Aborting."
		EXIT_CODE=2
	    fi
	    break
	fi
	# A return value of 1 means we can't go any further unless --force is used
	if [ $ret -gt 0 ] && [ x"$FORCE_ALL" != "x1" ] ; then
	    vecho 0 "$EXE: Aborting suspend due to errors in $bit (use --force to override)."
	    EXIT_CODE=2
	    break
	fi
	if [ -n "$SUSPEND_ABORT" ] ; then
	    vecho 0 "$EXE: Suspend aborted by user."
	    EXIT_CODE=3
	    break
	fi
    done

    # Resume and cleanup and stuff.
    for bit in `SortResumeBits` ; do
	THIS_POS="`awk \"BEGIN{print substr(\\\"$bit\\\", 1, 2)}\"`"
	[ -z "$THIS_POS" ] && continue
	bit=${bit##$THIS_POS}
	[ "$THIS_POS" -gt "$CHAIN_UP_TO" ] && continue
	vecho 1 "$EXE: [$THIS_POS] Executing $bit ... "
	[ -n "$OPT_DRY_RUN" ] && continue
	$bit
    done
    return $EXIT_CODE
}

ctrlc_handler() {
    SUSPEND_ABORT=1
}

# Gets the value of the environment variable in the environment space of the
# processes identified by its PID.
get_env_var_of_process()
{
    local pid; pid="$1"
    local envvar; envvar="$2"
    tr '\0' '\n' </proc/$pid/environ | sed -ne "s/^$envvar=//p"
}

############################### MAIN #########################################

# Some starting values:
VERBOSITY=0
LOG_VERBOSITY=1
LOGPIPE="cat"
ERROR_TEXT=""

EnsureHavePrerequisites

# Test for options that will affect future choices
PreliminaryGetopt "$@"

CheckImplicitAlternateConfig $0

# Generating help text is slow. Avoid it if we can.
if [ -n "$HELP_ONLY" ] ; then
    AddInbuiltHelp
    LoadScriptlets
    Usage
    exit 0
fi
DISABLE_HELP=1

EnsureHaveRoot
LoadScriptlets
ReadConfigFile "${CONFIG_FILE}"
# Verify we actually got a suspend method we could use.
if [ -z "$HIBERNATE_SUSPEND_METHOD" ] ; then
    echo "$EXE: No suitable suspend methods were found on your machine."
    echo "$EXE: You need to install a kernel with support for suspending to"
    echo "$EXE: disk or RAM and reboot, then try again."
    exit 1
fi
ParseOptions "$@"

# Set a logfile if we need one.
if [ -n "$LOGFILE" ]; then
    if [ ! -f "$LOGFILE" ]; then
	touch "$LOGFILE"
	chmod 644 "$LOGFILE"
	chown root:adm "$LOGFILE"
    fi
    LOGPIPE="tee -a -i $LOGFILE"
fi

# Redirect everything to a given VT if we've been given one
if [ -n "$SWSUSPVT" ] && [ -c /dev/tty$SWSUSPVT ] ; then
    exec >/dev/tty$SWSUSPVT 2>&1
fi

# Use -x if we're being really verbose!
[ $VERBOSITY -ge 4 ] && set -x

echo "Starting suspend at "`date` | $LOGPIPE > /dev/null

EXIT_CODE=0

if [ "$LOGPIPE" = "cat" ] ; then
    DoWork
else
    # Sigh. Our portable way to obtain exit codes has issues in bash, so if
    # we're using bash, we do it the not-so-portable way :)
    if [ -n "$USING_BASH" ] ; then
	DoWork | $LOGPIPE
	eval 'EXIT_CODE=${PIPESTATUS[0]}'
    else
	# Evilness required to pass the exit code back to us in a pipe.
	trap "" INT
	exec 3>&1
	eval `
	    exec 4>&1 >&3 3>&-
	    {
		DoWork 4>&-
		echo "EXIT_CODE=$EXIT_CODE" >&4
	    } | $LOGPIPE`
    fi
fi

PluginTermination

echo "Resumed at "`date` | $LOGPIPE > /dev/null

exit $EXIT_CODE

# $Id$