summaryrefslogtreecommitdiff
blob: 90c0b7aac7ea91d76a2a087337e38cc97d00d373 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

source /sbin/functions.sh

usage() {
cat << FOO
usage: rc-update -a|add script runlevel2 [runlevel2 ...]
       rc-update -d|del script [runlevel1 ...]
       rc-update -s|show [runlevel1 ...]

examples:
       # rc-update add sysklogd default
       Adds the sysklogd script (in /etc/init.d) to the "default" runlevel.

       # rc-update del sysklogd
       Deletes the sysklogd script from all runlevels.  The original script
       is not deleted, just any symlinks to the script in /etc/runlevels/*.

       # rc-update del sysklogd default wumpus
       Delete the sysklogd script from the default and wumpus runlevels.
       All other runlevels are unaffected.  Again, the sysklogd script
       residing in /etc/init.d is not deleted, just any symlinks in
       /etc/runlevels/default and /etc/runlevels/wumpus.

       # rc-update show
       Show all the available scripts and list at which runlevels they
       will execute.
FOO
	exit 1
}

add() {
	local x=
	local myscript=

	if [[ $# -lt 3 ]] ; then
		eerror "$0: at least two arguments expected after \"$1\"."
		exit 1
	fi
	shift
	myscript="$1"
	if [[ ! -e ${ROOT}/etc/init.d/${myscript} ]] ; then
		eerror "$0: '${ROOT}/etc/init.d/${myscript}' not found; aborting."
		exit 1
	fi
	shift
	for x in $* ; do
		if [[ ! -e ${ROOT}/etc/runlevels/${x} ]] ; then
			ewarn "runlevel ${x} not found; skipping"
			continue
		fi
		if [[ -L ${ROOT}/etc/runlevels/${x}/${myscript} ]] ; then
			ewarn "${myscript} already installed in runlevel ${x}; skipping"
			continue
		fi
		if [[ ! -x ${ROOT}/etc/init.d/${myscript} ]] ; then
			ewarn "${myscript} not executable; skipping"
			continue
		fi
		ln -snf "/etc/init.d/${myscript}" "${ROOT}/etc/runlevels/${x}/${myscript}"
		if [[ $? -ne 0 ]] ; then
			eerror "$0: failed to add ${myscript} to ${x}."
			exit 1
		fi
		einfo "${myscript} added to runlevel ${x}"
	done
}

del() {
	local x=
	local mylevels=
	local myscript=
	local remlevels=

	if [[ $# -lt 2 ]] ; then
		eerror "$0: at least one argument expected after \"$1\"."
		exit 1
	fi
	shift
	myscript=$1
	shift
	if [[ $# -eq 0 ]] ; then
		mylevels=$(cd "${ROOT}"/etc/runlevels/; ls)
	else
		mylevels="$*"
	fi
	remlevels=""
	for x in ${mylevels} ; do
		if [[ -L ${ROOT}/etc/runlevels/${x}/${myscript} ]] ; then
			rm -f "${ROOT}/etc/runlevels/${x}/${myscript}"
			remlevels="${remlevels} ${x}"
		fi
	done
	if [[ -z ${remlevels} ]] ; then
		einfo "${myscript} not found in any of the specified runlevels."
	else
		einfo "${myscript} removed from the following runlevels:${remlevels}"
	fi
}

show() {
	local x=
	local y=
	local mylevels=
	local myscripts=

	shift
	if [[ $# -eq 0 ]] ; then
		mylevels=$(cd "${ROOT}"/etc/runlevels/; ls)
	else
		mylevels="$*"
	fi
	myscripts=$(cd "${ROOT}"/etc/init.d; ls)

	for x in ${myscripts} ; do
		if [[ ${x%%.sh} = "${x}" ]] ; then
			printf "%20s | " ${x:0:19}
			for y in ${mylevels} ; do
				if [[ -L ${ROOT}/etc/runlevels/${y}/${x} ]] ; then
					echo -n "${y} "
				else
					printf "%${#y}s " " "
				fi
			done
			echo ""
		fi
	done
}

check_is_root() {
	if [[ ${EUID} -ne 0 ]] ; then
		eerror "$0: must be root."
		exit 1
	fi
}

if [[ $# -lt 1 ]] ; then
	usage
	exit 1
fi

if [[ -n ${ROOT} ]] ; then
	[[ ${ROOT:0-1} == "/" ]] && export ROOT=${ROOT:0:${#ROOT}-1}
	einfo "Working with files in root ${ROOT} ..."
fi

case "$1" in
	add|-a)
		check_is_root
		add "$@"
		;;
	del|delete|-d)
		check_is_root
		del "$@"
		;;
	show|-s)
		show "$@"
		;;
	*)
		usage
		exit 1
		;;
esac

# vim:ts=4