summaryrefslogtreecommitdiff
blob: 65cd011fe6f125839cc8458e8506134407cd8d9a (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
#!/bin/bash
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

source /sbin/functions.sh
if [[ ${EUID} -ne 0 ]] ; then
	eerror "$0: must be root."
	exit 1
fi

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 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 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 /etc/init.d/${myscript} ]] ; then
		eerror "$0: /etc/init.d/${myscript} not found; aborting."
		exit 1
	fi
	shift
	for x in $* ; do
		if [[ ! -e /etc/runlevels/${x} ]] ; then
			ewarn "runlevel ${x} not found; skipping"
			continue
		fi
		if [[ -L /etc/runlevels/${x}/${myscript} ]] ; then
			ewarn "${myscript} already installed in runlevel ${x}; skipping"
			continue
		fi
		if [[ ! -x /etc/init.d/${myscript} ]] ; then
			ewarn "${myscript} not executable; skipping"
			continue
		fi
		ln -snf "/etc/init.d/${myscript}" "/etc/runlevels/${x}/${myscript}"
		if [[ $? -ne 0 ]] ; then
			eerror "$0: failed to add ${myscript} to ${x}."
			exit 1
		fi
		regen=1
		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 /etc/runlevels/; ls)
	else
		mylevels="$*"
	fi
	remlevels=""
	for x in ${mylevels} ; do
		if [[ -L /etc/runlevels/${x}/${myscript} ]] ; then
			regen=1
			rm -f "/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 /etc/runlevels/; ls)
	else
		mylevels="$*"
	fi
	myscripts=$(cd /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 /etc/runlevels/${y}/${x} ]] ; then
					echo -n "${y} "
				else
					printf "%${#y}s " " "
				fi
			done
			echo ""
		fi
	done
}

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

regen=0

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


# vim:ts=4