summaryrefslogtreecommitdiff
blob: 2e320cb6924080340168f1a86f21949d175a541f (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
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

BEGIN {

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

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

	pipe = "ls -1 /etc/env.d/."
	while ((pipe | getline tmpstring) > 0)
		scripts = scripts " /etc/env.d/" tmpstring
	close(pipe)

	split(scripts, TMPENVFILES)

	# Make sure that its a file we are working with,
	# and do not process scripts, source or backup files.
	# NOTE:  do not use 'for (x in TMPENVFILES)', as gawk
	#        have this notion that it should mess with the
	#        order it list things then ....
	for (x = 1;;x++) {
	
		if (x in TMPENVFILES) {
		
			if ((isfile(TMPENVFILES[x])) &&
			    (TMPENVFILES[x] !~ /((\.(sh|c|bak))|\~)$/)) {

				ENVCOUNT++

				ENVFILES[ENVCOUNT] = TMPENVFILES[x]
			}
		} else
			break
	}

	if (ENVCOUNT == 0) {

		eerror("No files to process!")
		exit 1
	}

	ENVCACHE = SVCDIR "/envcache"
	SHPROFILE = "/etc/profile.env"
	CSHPROFILE = "/etc/csh.env"

	# SPECIALS are treated differently.  For each env.d file, the variables are
	# appended seperated with a ':'.  If not in specials, for each env.d file,
	# the variable are just set to the new value.
	tmpspecials = \
		"ADA_INCLUDE_PATH:ADA_OBJECTS_PATH:CLASSPATH:" \
		"CONFIG_PROTECT:CONFIG_PROTECT_MASK:INFOPATH:" \
		"KDEDIRS:LDPATH:MANPATH:PATH:PKG_CONFIG_PATH:" \
		"PRELINK_PATH:PRELINK_PATH_MASK:PYTHONPATH:ROOTPATH"
	split(tmpspecials, SPECIALS, ":")

	unlink(ENVCACHE)

	for (count = 1;count <= ENVCOUNT;count++) {
		
		while ((getline < (ENVFILES[count])) > 0) {

			# Filter out comments
			if ($0 !~ /^[[:space:]]*#/) {

				split($0, envnode, "=")

				if (envnode[2] == "")
					continue

				if ($0 == "")
					continue

				# LDPATH should not be in environment
				if (envnode[1] == "LDPATH")
					continue

				# In bash there should be no space between the variable name and
				# the '=' ...
				if (envnode[1] ~ /[^[:space:]]*[[:space:]]+$/)
					continue

				# strip variable name and '=' from data
				sub("^[[:space:]]*" envnode[1] "[[:space:]]*=", "")
				# strip all '"' and '\''
				gsub(/\"/, "")
				gsub(/\'/, "")
				# strip leading and trailing spaces
				gsub(/^[[:space:]]*/, "")
				gsub(/[[:space:]]*$/, "")

				if (envnode[1] in ENVTREE) {

					DOSPECIAL = 0

					for (x in SPECIALS) {

						# Is this a special variable ?
						if (envnode[1] == SPECIALS[x])
							DOSPECIAL = 1
					}

					if (DOSPECIAL) {
						split(ENVTREE[envnode[1]], tmpstr, ":")

						# Check that we do not add dups ...
						NODUPS = 1
						for (x in tmpstr)
							if (tmpstr[x] == $0)
								NODUPS = 0
						
						if (NODUPS)
							# Once again, "CONFIG_PROTECT" and "CONFIG_PROTECT_MASK"
							# are handled differently ...
							if ((envnode[1] == "CONFIG_PROTECT") || (envnode[1] == "CONFIG_PROTECT_MASK"))
								ENVTREE[envnode[1]] = ENVTREE[envnode[1]] " " $0
							else
								ENVTREE[envnode[1]] = ENVTREE[envnode[1]] ":" $0
					} else
						ENVTREE[envnode[1]] = $0
				} else
					ENVTREE[envnode[1]] = $0
			}
		}

		close(ENVFILES[count])
	}

	for (x in ENVTREE)
		print "export " x "=\"" ENVTREE[x] "\"" >> (ENVCACHE)

	for (x in ENVTREE) {
	
		# Print this a second time to make sure all variables
		# are expanded ..
		print "export " x "=\"" ENVTREE[x] "\"" >> (ENVCACHE)
		print "echo \"" x "=${" x "}\"" >> (ENVCACHE)
	}

	close (ENVCACHE)

	unlink(SHPROFILE)
	unlink(CSHPROFILE)

	# Add warning header for SHPROFILE
	print "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update." > (SHPROFILE)
	print "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES" >> (SHPROFILE)
	print "# GO INTO /etc/profile NOT /etc/profile.env" >> (SHPROFILE)
	print "" >> (SHPROFILE)
	
	# Add warning header for CSHPROFILE
	print "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update." > (CSHPROFILE)
	print "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES" >> (CSHPROFILE)
	print "# GO INTO /etc/csh.cshrc NOT /etc/csh.env" >> (CSHPROFILE)
	print "" >> (CSHPROFILE)


	pipe = "bash " ENVCACHE
	while ((pipe | getline) > 0) {

		sub(/=/, "='")
		sub(/$/, "'")

		print "export " $0 >> (SHPROFILE)

		sub(/=/, " ")

		print "setenv " $0 >> (CSHPROFILE)
	}
	
	close(pipe)
	close(SHPROFILE)
	close(CSHPROFILE)
}


# vim:ts=4