Magellan Linux

Contents of /trunk/java-update/java-update.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1766 - (show annotations) (download) (as text)
Sat Feb 18 19:37:02 2012 UTC (12 years, 2 months ago) by niro
File MIME type: application/x-sh
File size: 5978 byte(s)
-initial release of java-update
1 #!/bin/bash
2 # Copyright 1999-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /home/cvsd/magellan-cvs/magellan-src/opengl-update/opengl-update.sh,v 1.10 2007-11-14 19:19:16 niro Exp $
5 # Author: Martin Schlemmer <azarah@gentoo.org>
6 # Further modifications by Donnie Berkholz <spyderous@gentoo.org>
7 # Further modifications based off submissions to bug #54984 <cyfred@gentoo.org>
8 # Further modifications by Jeremy Huddleston <eradicator@gentoo.org>
9 #
10 # hacked for use with magellan-linux
11 # <niro@magellan-linux.de>
12 #
13
14 hasq()
15 {
16 local x
17
18 local me=$1
19 shift
20
21 for x in $@
22 do
23 [[ ${x} = ${me} ]] && return 0
24 done
25
26 return 1
27 }
28
29 check_user()
30 {
31 if [[ $(id -u) -ne 0 ]]
32 then
33 echo "$0: Must be run as root."
34 exit 1
35 fi
36 }
37
38 get_current_implem()
39 {
40 local implem
41
42 # jre does always exist!
43 if [ -f ${ENV_D_JRE} ]
44 then
45 source "${ENV_D_JRE}"
46 if [[ -n ${JAVA_PROFILE} ]]
47 then
48 implem="${JAVA_PROFILE}"
49 fi
50 unset JAVA_PROFILE
51 fi
52
53 echo "${implem}"
54 }
55
56 get_implementations()
57 {
58 local implems
59
60 for dir in ${PREFIX}/java/*
61 do
62 if [ -d ${dir} ] &&
63 [[ ${dir##*/} != global ]] &&
64 ! hasq "${dir##*/}" "${implems}"
65 then
66 implems="${implems:+${implems} }${dir##*/}"
67 fi
68 done
69
70 echo "${implems}"
71 }
72
73 print_version()
74 {
75 echo "java-update ${VERSION}"
76 }
77
78 print_usage()
79 {
80 # Get grammar right in message
81 local IS_ARE IMPLEM_PLURAL
82
83 if [[ $(echo ${AVAIL_IMPLEMS} | wc -w) -eq 1 ]]
84 then
85 IS_ARE="is"
86 IMPLEM_PLURAL=""
87 else
88 IS_ARE="are"
89 IMPLEM_PLURAL="s"
90 fi
91
92 print_version
93
94 cat << FOO
95 Usage: ${0##*/} [<options>] <Java implementation>
96 Set the Java implementation.
97 Valid options:
98 --help|-h|-?: Prints this help.
99 --version: Shows the version of this utility.
100 --use-old: If an implementation is already set, use that one.
101 --prefix=<val>: Set the source prefix (default: /etc)
102
103 Usage: ${0##*/} --get-implementation
104 Print the current implementaion
105
106 This utility switches between Java implementations. There ${IS_ARE}
107 $(echo ${AVAIL_IMPLEMS} | wc -w) available implementation${IMPLEM_PLURAL}: ${AVAIL_IMPLEMS}
108
109 Examples:
110 ${0##*/} java-1.6-sun
111 This will setup things to use Java Version 6 from Sun.
112
113 ${0##*/} java-1.7-openjdk
114 This will setup things to use OpenJDK Version 7.
115
116 FOO
117 exit 1
118 }
119
120 parse_options()
121 {
122 local opt
123
124 while [[ ${#} -gt 0 ]]
125 do
126 opt=$1
127 shift
128 case ${opt} in
129 --use-old)
130 if [[ -n ${ACTION} ]]
131 then
132 ACTION="error"
133 echo "Invalid usage."
134 else
135 if [[ -n "${CURRENT_JAVA_IMPLEM}" ]] &&
136 hasq "${CURRENT_JAVA_IMPLEM}" "${AVAIL_IMPLEMS}"
137 then
138 ACTION="old-implementation"
139 fi
140 fi
141 ;;
142
143 --get-implementation)
144 if [[ -n ${ACTION} ]]
145 then
146 ACTION="error"
147 echo "Invalid usage."
148 else
149 ACTION="get-implementation"
150 fi
151 ;;
152
153 --prefix=*)
154 PREFIX="${opt#*=}"
155 AVAIL_IMPLEMS=$(get_implementations)
156 ;;
157
158 --help|-h|-?) ACTION="usage" ;;
159 --version) ACTION="version" ;;
160 *)
161 if hasq "${opt}" "${AVAIL_IMPLEMS}"
162 then
163 if [[ ${ACTION} != old-implementation ]]
164 then
165 if [[ -n ${ACTION} ]]
166 then
167 ACTION="error"
168 echo "Invalid usage."
169 else
170 ACTION="set-implementation"
171 NEW_JAVA_IMPLEM="${opt}"
172 fi
173 fi
174 else
175 echo "Unrecognized option: ${opt}"
176 ACTION="error"
177 fi
178 ;;
179 esac
180 done
181 }
182
183 set_new_implementation()
184 {
185 local JAVA_IMPLEM="$1"
186 local JAVA_LOCAL
187
188 check_user
189
190 # Set a sane umask... bug #83115
191 umask 022
192
193 if ! hasq "${JAVA_IMPLEM}" "${AVAIL_IMPLEMS}"
194 then
195 echo "Invalid profile selected."
196 exit 1
197 fi
198
199 echo "Switching to ${JAVA_IMPLEM} Java interface"
200
201 [ -f ${ENV_D_JRE} ] && rm -f ${ENV_D_JRE}
202 [ -f ${ENV_D_JDK} ] && rm -f ${ENV_D_JDK}
203 [ -f ${ENV_D_GLOBAL_CLASSPATH} ] && rm -f ${ENV_D_GLOBAL_CLASSPATH}
204 [ -f ${ENV_D_EOF_CLASSPATH} ] && rm -f ${ENV_D_EOF_CLASSPATH}
205
206 cat ${PREFIX}/java/${JAVA_IMPLEM}/jre.conf >> ${ENV_D_JRE}
207 echo "JAVA_PROFILE=\"${JAVA_IMPLEM}\"" >> ${ENV_D_JRE}
208
209 # set global classpath
210 if [ -f ${PREFIX}/java/${JAVA_IMPLEM}/global-classpath.conf ]
211 then
212 cat ${PREFIX}/java/${JAVA_IMPLEM}/global-classpath.conf >> ${ENV_D_GLOBAL_CLASSPATH}
213 elif [ -f ${PREFIX}/java/global/global-classpath.conf ]
214 then
215 cat ${PREFIX}/java/global/global-classpath.conf >> ${ENV_D_GLOBAL_CLASSPATH}
216 else
217 echo "Warning: global-classpath.conf is missing!"
218 fi
219
220 # set eof classpath
221 if [ -f ${PREFIX}/java/${JAVA_IMPLEM}/eof-classpath.conf ]
222 then
223 cat ${PREFIX}/java/${JAVA_IMPLEM}/eof-classpath.conf >> ${ENV_D_EOF_CLASSPATH}
224 elif [ -f ${PREFIX}/java/global/eof-classpath.conf ]
225 then
226 cat ${PREFIX}/java/global/eof-classpath.conf >> ${ENV_D_EOF_CLASSPATH}
227 else
228 echo "Warning: eof-classpath.conf is missing!"
229 fi
230
231 # set jdk too if installed
232 if [ -f ${PREFIX}/java/${JAVA_IMPLEM}/jdk.conf ]
233 then
234 cat ${PREFIX}/java/${JAVA_IMPLEM}/jdk.conf >> ${ENV_D_JDK}
235 echo "JAVA_PROFILE=\"${JAVA_IMPLEM}\"" >> ${ENV_D_JDK}
236 fi
237
238 env-rebuild
239
240 return 0
241 }
242
243 ## START PROGRAM ##
244
245 ENV_D_JRE="/etc/env.d/21java"
246 ENV_D_JDK="/etc/env.d/22java-jdk"
247 ENV_D_GLOBAL_CLASSPATH="/etc/env.d/22java-global-classpath"
248 ENV_D_EOF_CLASSPATH="/etc/env.d/30java-eof-classpath"
249 NEW_JAVA_IMPLEM=""
250 ACTION=""
251 PREFIX="/etc"
252 AVAIL_IMPLEMS=$(get_implementations)
253 CURRENT_JAVA_IMPLEM=$(get_current_implem)
254 VERSION="@@VERSION@@"
255
256 parse_options $@
257
258 case ${ACTION} in
259 get-implementation)
260 if [[ -n ${CURRENT_JAVA_IMPLEM} ]]
261 then
262 echo ${CURRENT_JAVA_IMPLEM}
263 retval=0
264 else
265 retval=2
266 fi
267 ;;
268
269 old-implementation)
270 set_new_implementation ${CURRENT_JAVA_IMPLEM}
271 retval=$?
272 ;;
273
274 set-implementation)
275 if [[ -n ${NEW_JAVA_IMPLEM} ]]
276 then
277 set_new_implementation ${NEW_JAVA_IMPLEM}
278 retval=$?
279 else
280 print_usage
281 retval=1
282 fi
283 ;;
284
285 version) print_version; retval=0 ;;
286 usage) print_usage; retval=0 ;;
287 error) print_usage; retval=1 ;;
288 *) print_usage; retval=1;;
289 esac
290
291 exit "${retval}"