## vim:ft=zsh ## Small shutdown/reboot wrapper to work together with my other tools ## Copyright (C) 2008, 2009 by Daniel Friesel ## License: WTFPL autoload warn fdie typeset filesystem garbage file typeset -a filesystems typeset tune2fs typeset -i force reboot simulate typeset gone 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] [place]' 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' print '' print ' place supply if machine will remain off for some time,' print ' either "gone" or the place one will be going to' return 0 ;; *) fdie "Unrecognized option: $1"; return 1 ;; esac shift } gone=$1 # 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 [[ -n $gone ]] { for file in ~/packages/leave.d/etc/{_any,$gone}/*(N); { if [[ ${file:e} == zsh ]] { execute source $file } else { execute $file } } } if pgrep -x amarokapp &> /dev/null; then execute dcop amarok MainApplication-Interface quit fi execute uinit -o text stop-all while read filesystem garbage; do if [[ $garbage == *[12] ]] && fgrep $filesystem /etc/mtab &> /dev/null; then filesystems+=$filesystem fi done < /etc/fstab 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 }