## vim:ft=zsh autoload warn fdie typeset filesystem line IFS=$'\n' typeset -a filesystems typeset tune2fs typeset -i force reboot simulate function execute { (( simulate )) || $* } while [[ $1 == -* ]] { case $1 in -n) simulate=1 ;; -r) reboot=1 ;; --force) force=1 ;; *) 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 } 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] < 2 )) { echo "notice: filesystem $filesystem due to check at next boot" } } if (( reboot )) { execute sudo reboot } else { execute sudo halt }