#!/bin/sh

sysv() {
  /sbin/shutdown "$@"
}

single() {
  runsvchdir single
}

abort() {
  printf '%s\n' "$1" >&2
  exit 1
}

usage() {
  abort "Usage: ${0##*/} [-fF] [-kchPr] time [warning message]"
}

runit_init() {

  while getopts akrhPHfFnct: opt; do
    case "$opt" in
      a|n|H) abort "'-$opt' is not implemented";;
      t) ;;
      f) touch /fastboot;;
      F) touch /forcefsck;;
      k) action=true;;
      c) action=cancel;;
      h|P) action="/sbin/runit-init 0";;
      r) action="/sbin/runit-init 6";;
      [?]) usage;;
    esac
  done
  shift $((OPTIND - 1))

  [ $# -eq 0 ] && usage

  time=$1; shift
  message="${*:-system is going down}"

  if [ "$action" = "cancel" ]; then
    kill "$(cat /run/shutdown.pid)"
    if [ -e /etc/nologin ] && ! [ -s /etc/nologin ]; then
      rm /etc/nologin
    fi
    echo "${*:-shutdown cancelled}" | wall
    exit
  fi

  touch /run/shutdown.pid 2>/dev/null || abort "Not enough permissions to execute ${0#*/}"
  echo $$ >/run/shutdown.pid

  case "$time" in
    now) time=0;;
    +*) time=${time#+};;
    *:*) abort "absolute time is not implemented";;
    *) abort "invalid time";;
  esac

  for break in 5 0; do
    [ "$time" -gt "$break" ] || continue
    [ "$break" = 0 ] && touch /etc/nologin

    printf '%s in %s minutes\n' "$message" "$time" | wall
    printf 'shutdown: sleeping for %s minutes... ' "$(( time - break ))"
    sleep $(( (time - break) * 60 ))
    time="$break"
    printf '\n'

    [ "$break" = 0 ] && rm /etc/nologin
  done

  printf '%s NOW\n' "$message" | wall

  $action

}

initsystem=$( ps -p 1 -o comm= )
 
action=single

case "$initsystem" in
'init')
  sysv "$@"
  ;;
'runit')
  runit_init "$@"
  ;;
*)
  exit
esac
