summaryrefslogtreecommitdiff
path: root/etc/functions/off
blob: 98922a21f7289e04fe4346d68714239f36a0b0bf (plain)
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
## vim:ft=zsh
## Small shutdown/reboot wrapper to work together with my other tools
## Copyright (C) 2008 by Daniel Friesel <derf@derf.homelinux.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>

autoload warn fdie
typeset filesystem line IFS=$'\n'
typeset -a filesystems
typeset tune2fs
typeset -i force reboot simulate

function execute {
	if (( simulate )) {
		echo $*
	} else {
		$*
	}
}

while [[ $1 == -* ]] {
	case $1 in
		-n) simulate=1 ;;
		-r) reboot=1 ;;
		--force) force=1 ;;
		-h)
			print 'off - shutdown / restart the system'
			print 'Usage: off [--force] [-hnr]'
			print 'Options:\n'
			print '  -n    simulate. Do not actually shutdown'
			print '  -r    reboot'
			print '  -h    show this message'
			print '\--force shutdown even if the machine is a server'
			return 0
		;;
		*) fdie "Unrecognized option: $1"; return 1 ;;
	esac
	shift
}

# Don't shut down a server too easily
if [[ $force != 1 && $hosts[$HOST] == *:server:* ]] {
	warn "This seems to be a server... not shutting down"
	echo "Use 'off --force' if you really mean it"
	return 1
}

if ps aux | fgrep amarokapp &> /dev/null; then
	execute dcop amarok MainApplication-Interface quit
fi

execute uinit stop-all
for line in $(cat /etc/fstab); {
	if [[ $line == *[12] ]] {
		filesystems+=${${(s: :)line}[1]}
	}
}
for filesystem in $filesystems; {
	tune2fs=($(sudo tune2fs -l $filesystem | fgrep -i 'mount count' | grep -o '[0-9]*'))
	if (( tune2fs[2] - tune2fs[1] < 5 )) {
		echo "notice: filesystem $filesystem due to check in $((tune2fs[2] - tune2fs[1])) mounts"
	}
}
if (( reboot )) {
	execute sudo reboot
} else {
	execute sudo halt
}