#!/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='' ALL_CMDS='' LOG_MSG='' EXTRA_COMMANDS='' main() { local action="${2}" 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 cmds= cmds="$(printf '%s|' ${ALL_CMDS})" tty_printf 'Usage: %s {%s}\n' "${SCRIPT}" "${cmds%|}" } 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 '[ ok ] %s... done.\n' \ "${LOG_MSG}" ;; 255) tty_printf '[warn] %s... done.\n' \ "${LOG_MSG}" ;; *) tty_printf '[fail] %s... failed.\n' \ "${LOG_MSG}" ;; esac } # Default function definitions stop() { : } restart() { stop start } main "${@}"