summaryrefslogtreecommitdiffstats
path: root/rc_add
blob: de0b8c30d3ebfa295806c9a13b51801167a147fc (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
#!/bin/sh
#
# rc_add - add an init script to a runlevel
#
# Copyright (c) 2005 Natanael Copa
#
# Distributed under GPL-2
#

PROGRAM=`basename $0`

#load the libraries
. /sbin/functions.sh

STARTNUM=50
RCDIR="$ROOT/etc/rcL.d"

die() {
	echo "$1" >&2
	exit 1
}

# print usage and die
usage() {
	echo "$PROGRAM $VERSION"
	echo "usage: $PROGRAM [-hv] [-s number] script"
	echo ""
	echo "  -h  Show help and exit."
	echo "  -k  Also add a corresponding kill link for shutdown/reboot."
	echo "  -s  Set two-digit start number (00-99). Default is $STARTNUM."
	echo "  -S  Add service as a system init service (rcS.d)."
	echo "  -v  Turn on verbose output."
	echo ""
	exit 1
}

#parse args
unset vflag
while getopts "hks:Sv" opt ; do
	case "$opt" in
		h)  usage;;
		k)  KILL="-k";;
		s)  STARTNUM="$OPTARG";;
		S)  RCDIR="ROOT/rcS.d";;
		v)  vflag="-v"; VERBOSE=1 ;;
		\?) usage;;
	esac
done
shift `expr $OPTIND - 1`

# check if script is specified
[ $# -lt 1 ] && usage

while [ $# -gt 0 ] ; do
	SCRIPT="$1"

	# validate the start number
	echo "$STARTNUM" | grep '^[0-9][0-9]$' > /dev/null || die "Start number needs to be a 2 digit number. (00-99)"

	mkdir -p "$ROOT/$RCDIR"
	cd "$ROOT/$RCDIR"

	LINKTARGET="../init.d/$SCRIPT"
	[ -f "$LINKTARGET" ] || die "Could not find init script $LINKTARGET"
	SLINK="S$LEVEL$STARTNUM$SCRIPT"
	[ "$VERBOSE" ] && echo "Creating $ROOT/$RCDIR/$SLINK."
	ln -sf $LINKTARGET "$ROOT/$RCDIR/$SLINK" || exit $?

	if [ "$KILL" ] ; then
		KLINK="K$LEVEL$STARTNUM$SCRIPT"
		[ "$VERBOSE" ] && echo "Creating $ROOT/etc/rcK.d/$KLINK."
		mkdir -p "$ROOT/etc/rcK.d"
		ln -sf $LINKTARGET "$ROOT/etc/rcK.d/$KLINK" || exit $?
	fi

	shift
done
exit 0