summaryrefslogtreecommitdiffstats
path: root/bin/env-update.sh
blob: 8f14eb8948f7c61a3f6adb00a1cce5c3daa656d8 (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
215
216
217
218
219
#!/bin/bash
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: /var/cvsroot/gentoo-src/portage/bin/env-update.sh,v 1.2 2004/10/04 13:56:50 vapier Exp $

############################################
############################################
# ENVIRONMENT SETUP
############################################

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

# Make sure our environment is sane
if [[ ! -z "${MAKELINKS}" ]] ; then
	export MAKELINKS=0
else
	export MAKELINKS=1
fi
export ROOT="${ROOT:=/}"
[[ ${ROOT} == */ ]] || export ROOT="${ROOT}/"

export ENVDIR="${ROOT}etc/env.d"
mkdir -p ${ENVDIR}
chmod 755 ${ENVDIR}
specials="
	KDEDIRS PATH CLASSPATH LDPATH MANPATH INFODIR INFOPATH ROOTPATH
	CONFIG_PROTECT CONFIG_PROTECT_MASK PRELINK_PATH PYTHONPATH
	PRELINK_PATH_MASK ADA_INCLUDE_PATH ADA_OBJECTS_PATH"
colon_separated="
	ADA_INCLUDE_PATH ADA_OBJECTS_PATH LDPATH PATH MANPATH ROOTPATH
	PRELINK_PATH PRELINK_PATH_MASK PYTHON_PATH"

export LDSOCONF="${ROOT}etc/ld.so.conf"

export PRELINKCONF="${ROOT}etc/prelink.conf"
defaultprelinkpaths=":/bin:/sbin:/usr/bin:/usr/sbin:/lib:/usr/lib"

export PROFILEENV="${ROOT}etc/profile.env"
export CSHENV="${ROOT}etc/csh.env"

# make sure we aren't tricked with previous 'my_envd_' variables
unset $(set | grep '^my_envd_' | cut -d= -f1)

############################################
############################################
# ENV.D PARSING
############################################

do_has() {
	local x
	local me="$1"
	shift

	for x in "$@" ; do
		[[ ${x} == ${me} ]] && return 0
	done
	return 1
}
has() {
	local ret
	local ifs="${IFS}"
	unset IFS
	do_has $1 ${!2}
	ret=$?
	export IFS="${ifs}"
	return ${ret}
}
is_special() {
	has $1 specials
}
is_colon_separated() {
	has $1 colon_separated
}

for envd in $(ls ${ENVDIR} | sort) ; do
	# make sure file is a vaild env'd entry and not a backup file
	num="${envd:0:2}"
	if [[ ! -z ${num//[0-9]} ]] ; then
		continue
	elif [[ ${envd} == *~ || ${envd} == *.bak ]] ; then
		continue
	fi

	# use bash to make sure the file is valid
	envd="${ENVDIR}/${envd}"
	if ! (source "${envd}") ; then
		echo "!!! Error parsing ${envd}!"
		exit 1
	fi

	# parse env.d entries
	cnfvars="$(grep '^[[:alpha:]_][[:alnum:]_]*=' "${envd}")"
	export IFS=$'\n'
	for cnfv in ${cnfvars} ; do
		var="${cnfv/=*}"
		val="${cnfv:${#var}+1}"
		if [ "${val:0:1}" == "\"" ] ; then
			val="${val:1:${#val}-2}"
		fi
		myvar="my_envd_${var}"
		if is_special ${var} ; then
			if [[ ! -z "${!myvar}" ]] ; then
				if is_colon_separated ${var} ; then
					sep=":"
				else
					sep=" "
				fi
			else
				sep=""
			fi
			export ${myvar}="${!myvar}${sep}${val}"
		else
			export ${myvar}="${val}"
		fi
	done
	unset IFS
done

############################################
############################################
# LD.SO.CONF HANDLING
############################################

# create a : sep list from ld.so.conf
export OLD_LDPATH=""
if [ -s "${LDSOCONF}" ] ; then
	while read line ; do
		if [[ "${line:0:1}" == "#" ]] ; then
			continue
		fi
		export OLD_LDPATH="${OLD_LDPATH}:${line}"
	done < ${LDSOCONF}
	export OLD_LDPATH="${OLD_LDPATH:1}"
fi

# has the ldpath changed ?  if so, recreate
if [[ "${OLD_LDPATH}" != "${my_envd_LDPATH}" ]] ; then
	cat << EOF > ${LDSOCONF}
# ld.so.conf autogenerated by env-update; make all changes to
# contents of /etc/env.d directory
${my_envd_LDPATH//:/
}
EOF
fi

############################################
############################################
# HANDLE PRELINK PATHS
############################################

if prelink --version >& /dev/null ; then
	# we assume LDPATH and PATH aren't empty ... if they were, we got other problems
	envdprelinkcheckpaths="${my_envd_LDPATH}:${my_envd_PATH}"
	if [[ ! -z "${my_envd_PRELINK_PATH}" ]] ; then
		envdprelinkcheckpaths="${envdprelinkcheckpaths}:${my_envd_PRELINK_PATH}"
	fi

	if [[ ! -z "${my_envd_PRELINK_PATH_MASK}" ]] ; then
		export prelink_mask=":${PRELINK_PATH_MASK}:"
		envdprelinkpaths=""
		export IFS=":"
		for dir in ${envdprelinkcheckpaths} ; do
			if [[ ${dir:0-1} == / ]] ; then
				noslashdir="${dir:0:${#dir}-1}"
			else
				dir="${dir}/"
				noslashdir="${dir}"
			fi
			if [[ ${prelink_mask/:${dir}:/} == ${prelink_mask} \
				&& ${prelink_mask/:${noslashdir}:/} == ${prelink_mask} ]] ; then
				envdprelinkpaths="${envdprelinkpaths}:${dir}"
			fi
		done
		unset IFS
	else
		envdprelinkpaths=":${envdprelinkcheckpaths}"
	fi

	cat << EOF > ${PRELINKCONF}
# prelink.conf autogenerated by env-update; make all changes to
# contents of /etc/env.d directory
${defaultprelinkpaths//:/
-l }
${envdprelinkpaths//:/
-h }
EOF
fi
unset my_envd_LDPATH

############################################
############################################
# RUN EXTERNAL PROGRAMS NOW
############################################

echo ">>> Regenerating ${ROOT}etc/ld.so.cache..."
if [[ ${MAKELINKS} -eq 0 ]] ; then
	(cd / ; /sbin/ldconfig -X -r ${ROOT} >& /dev/null)
else
	(cd / ; /sbin/ldconfig -r ${ROOT} >& /dev/null)
fi

cat << EOF > ${PROFILEENV}
# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.
# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES
# GO INTO /etc/profile NOT /etc/profile.env

$(set | grep '^my_envd_' | sed -e 's:^my_envd_:export :')
EOF

cat << EOF > ${CSHENV}
# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.
# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES
# GO INTO /etc/csh.cshrc NOT /etc/csh.env

$(set | grep '^my_envd_' | sed -e 's:^my_envd_\([[:alpha:]_][[:alnum:]_]*\)=:setenv \1 :')
EOF