diff options
Diffstat (limited to 'src.etc/rc.common')
-rw-r--r-- | src.etc/rc.common | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src.etc/rc.common b/src.etc/rc.common new file mode 100644 index 0000000..344e3e7 --- /dev/null +++ b/src.etc/rc.common @@ -0,0 +1,96 @@ +#!/bin/sh +# +# /etc/rc.common +# Framework for service initialization scripts. +# +# Copyright (C) 2014 Patrick "P. J." McDermott +# +# This file may be reproduced, distributed, modified, and otherwise dealt in +# under the terms of the Expat License. + +set -u + +SCRIPT='' +LOG_MSG='' +EXTRA_COMMANDS='' + +main() +{ + local action="${2}" + local all_cmds= + local es= + + SCRIPT="${1}" + + . "${SCRIPT}" + + all_cmds=" start stop restart ${EXTRA_COMMANDS} " + if [ "x${all_cmds#* ${action} }" != "x${all_cmds}" ]; then + ${action} + es=${?} + log_end ${es} + return ${es} + else + usage + return 1 + fi +} + +tty_printf() +{ + local tty="$(tty)" && printf "${@}" >"${tty}" +} + +usage() { + local cmd= + + tty_printf 'Usage: %s {start|stop|restart' "${SCRIPT}" + if [ "x${EXTRA_COMMANDS:+set}" = 'xset' ]; then + tty_printf '|%s' ${EXTRA_COMMANDS} + fi + tty_printf '}\n' +} + +log() +{ + local fmt="${1}" + shift 1 + + LOG_MSG="$(printf "${fmt}" "${@}")" + tty_printf '[ ] %s...\r' "${LOG_MSG}" +} + +log_end() +{ + local es="${1}" + + case ${es} in + 0) + tty_printf '[[32m ok [39m] %s... done.\n' \ + "${LOG_MSG}" + ;; + 255) + tty_printf '[[33mwarn[39m] %s... done.\n' \ + "${LOG_MSG}" + ;; + *) + tty_printf '[[31mfail[39m] %s... failed.\n' \ + "${LOG_MSG}" + ;; + esac +} + +# Default function definitions + +stop() +{ + : +} + +restart() +{ + stop + start +} + +main "${@}" |