summaryrefslogtreecommitdiffstats
path: root/src.etc/rc.common
blob: 344e3e7190037ede212e5606c49fe31a00ee78df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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 '[ 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 "${@}"