summaryrefslogtreecommitdiff
blob: 46945e9e1005f012722edaade58e2c7d116374b8 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header$

function print_start() {
	print "source /sbin/functions.sh" >> TMPCACHE
	print "" >> TMPCACHE
	print "need() {" >> TMPCACHE
	print "	echo \"NEED $*\"; return 0" >> TMPCACHE
	print "}" >> TMPCACHE
	print "" >> TMPCACHE
	print "use() {" >> TMPCACHE
	print "	echo \"USE $*\"; return 0" >> TMPCACHE
	print "}" >> TMPCACHE
	print "" >> TMPCACHE
	print "before() {" >> TMPCACHE
	print "	echo \"BEFORE $*\"; return 0" >> TMPCACHE
	print "}" >> TMPCACHE
	print "" >> TMPCACHE
	print "after() {" >> TMPCACHE
	print "	echo \"AFTER $*\"; return 0" >> TMPCACHE
	print "}" >> TMPCACHE
	print "" >> TMPCACHE
	print "provide() {" >> TMPCACHE
	print "	echo \"PROVIDE $*\"; return 0" >> TMPCACHE
	print "}" >> TMPCACHE
	print "" >> TMPCACHE
}

function print_header1(mtime) {
	print "#*** " MYFILENAME " ***" >> TMPCACHE
	print "" >> TMPCACHE
	print "SVCNAME=\"" MYFILENAME "\"" >> TMPCACHE
	print "SVCNAME=\"${SVCNAME##*/}\"" >> TMPCACHE

	# Support deprected myservice variable
	print "myservice=\"${SVCNAME}\"" >> TMPCACHE
	
	print "echo \"RCSCRIPT ${SVCNAME}\"" >> TMPCACHE
	print "" >> TMPCACHE
	print "echo \"MTIME " mtime "\"" >> TMPCACHE
	print "" >> TMPCACHE
}

function print_header2(mtime) {
	print "(" >> TMPCACHE
	print "  # Get settings for rc-script ..." >> TMPCACHE
	print "" >> TMPCACHE
	print "  [ -e /etc/conf.d/net ]                   && \\" >> TMPCACHE
	print "  [ \"${SVCNAME%%.*}\" = \"net\" ]           && \\" >> TMPCACHE
	print "  [ \"${SVCNAME##*.}\" != \"${SVCNAME}\" ] && source /etc/conf.d/net" >> TMPCACHE
	print "" >> TMPCACHE
	print "  [ -e \"/etc/conf.d/${SVCNAME}\" ]        && source \"/etc/conf.d/${SVCNAME}\"" >> TMPCACHE
	print "" >> TMPCACHE
	print "  [ -e /etc/rc.conf ]                      && source /etc/rc.conf" >> TMPCACHE
	print "" >> TMPCACHE
	print "  depend() {" >> TMPCACHE
	print "    return 0" >> TMPCACHE
	print "  }" >> TMPCACHE
	print "" >> TMPCACHE
}

function print_end() {
	print "" >> TMPCACHE
	print "  depend" >> TMPCACHE
	print ")" >> TMPCACHE
	print "" >> TMPCACHE
}

BEGIN {

	extension("/lib/rcscripts/filefuncs.so", "dlload")

	# Get our environment variables
	SVCDIR = ENVIRON["SVCDIR"]
	if (SVCDIR == "") {
		eerror("Could not get SVCDIR!")
		exit 1
	}

	# Since this could be called more than once simultaneously, use a
	# temporary cache and rename when finished.  See bug 47111
	("/bin/mktemp "SVCDIR"/depcache.XXXXXXX") | getline TMPCACHE
	if (TMPCACHE == "") {
		eerror("Failed to create temporary cache!")
		exit 1
	}

	pipe = "ls /etc/init.d/*"
	while ((pipe | getline tmpstring) > 0)
		scripts = scripts " " tmpstring
	close(pipe)

	split(scripts, TMPRCSCRIPTS)

	# Make sure that its a file we are working with,
	# and do not process scripts, source or backup files.
	for (x in TMPRCSCRIPTS)
		if (((isfile(TMPRCSCRIPTS[x])) || (islink(TMPRCSCRIPTS[x]))) &&
		    (TMPRCSCRIPTS[x] !~ /((\.(c|bak))|\~)$/)) {

			RCCOUNT++

			RCSCRIPTS[RCCOUNT] = TMPRCSCRIPTS[x]
		}

	if (RCCOUNT == 0) {
		eerror("No scripts to process!")
		dosystem("rm -f "TMPCACHE)
		exit 1
	}

	print_start()

	for (count = 1;count <= RCCOUNT;count++) {
		
		MYFNR = 1
		MYFILENAME = RCSCRIPTS[count]
		STAT_DATA[1] = 1

		while (((getline < (RCSCRIPTS[count])) > 0) && (!NEXTFILE)) {

			# If line start with a '#' and is the first line
			if (($0 ~ /^[[:space:]]*#/) && (MYFNR == 1)) {
	
				# Remove any spaces and tabs
				gsub(/[[:space:]]+/, "")

				if ($0 == "#!/sbin/runscript") {

					if (RCSCRIPTS[count] ~ /\.sh$/) {

						ewarn(RCSCRIPTS[count] " is invalid (should not end with '.sh')")
						NEXTFILE = 1
						continue
					}

					if (stat(MYFILENAME, STAT_DATA) != 0)
						ewarn("Could not stat \"" MYFILENAME "\"")
				
					ISRCSCRIPT = 1
					print_header1(STAT_DATA["mtime"])
				} else  {
			
					NEXTFILE = 1
					continue
				}
			}

			# Filter out comments and only process if its a rcscript
			if (($0 !~ /^[[:space:]]*#/) && (ISRCSCRIPT)) {

				# If line contain 'depend()', set GOTDEPEND to 1
				if ($0 ~ /depend[[:space:]]*\(\)/) {
				
					GOTDEPEND = 1

					print_header2()
					print "  # Actual depend() function ..." >> TMPCACHE
				}
	
				# We have the depend function...
				if (GOTDEPEND) {

					# Basic theory is that COUNT will be 0 when we
					# have matching '{' and '}'
					COUNT += gsub(/{/, "{")
					COUNT -= gsub(/}/, "}")
		
					# This is just to verify that we have started with
					# the body of depend()
					SBCOUNT += gsub(/{/, "{")

					# Make sure depend() contain something, else bash
					# errors out (empty function).
					if ((SBCOUNT > 0) && (COUNT == 0))
						print "  \treturn 0" >> TMPCACHE
		
					# Print the depend() function
					print "  " $0 >> TMPCACHE
		
					# If COUNT=0, and SBCOUNT>0, it means we have read
					# all matching '{' and '}' for depend(), so stop.
					if ((SBCOUNT > 0) && (COUNT == 0)) {

						GOTDEPEND = 0
						COUNT = 0
						SBCOUNT = 0
						ISRCSCRIPT = 0

						print_end()
						
						NEXTFILE = 1
						continue
					}
				}
			}

			MYFNR++
		}

		close(RCSCRIPTS[count])

		NEXTFILE = 0

	}

	
	assert(dosystem("rm -f "SVCDIR"/depcache"), "system(rm -f "SVCDIR"/depcache)")
	assert(dosystem("mv "TMPCACHE" "SVCDIR"/depcache"), "system(mv "TMPCACHE" "SVCDIR"/depcache)")
}


# vim:ts=4