Magellan Linux

Annotation of /tags/openglupdate-2_2_13/opengl-update.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 633 - (hide annotations) (download) (as text)
Wed Nov 14 19:19:16 2007 UTC (16 years, 5 months ago) by niro
Original Path: trunk/opengl-update/opengl-update.sh
File MIME type: application/x-sh
File size: 9739 byte(s)
-fixed usage: tell something about the --help and --version option

1 niro 616 #!/bin/bash
2     # Copyright 1999-2004 Gentoo Foundation
3     # Distributed under the terms of the GNU General Public License v2
4 niro 633 # $Header: /home/cvsd/magellan-cvs/magellan-src/opengl-update/opengl-update.sh,v 1.10 2007-11-14 19:19:16 niro Exp $
5 niro 616 # 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     . /etc/init.d/functions
15    
16 niro 617 hasq()
17     {
18 niro 616 local x
19    
20     local me=${1}
21     shift
22 niro 617
23     for x in $@
24     do
25     [[ ${x} = ${me} ]] && return 0
26 niro 616 done
27 niro 617
28 niro 616 return 1
29     }
30    
31 niro 617 check_user()
32     {
33     if [[ $(id -u) -ne 0 ]]
34     then
35 niro 616 echo "${0}: Must be run as root."
36     exit 1
37     fi
38     }
39    
40 niro 617 get_current_implem()
41     {
42 niro 616 local implem
43 niro 617
44     if [[ -f ${ENV_D} ]]
45     then
46 niro 616 source ${ENV_D}
47 niro 617 if [[ -n ${OPENGL_PROFILE} ]]
48     then
49 niro 616 implem="${OPENGL_PROFILE}"
50 niro 617 elif [[ -n ${LDPATH} ]]
51     then
52 niro 616 implem="${LDPATH%%:*}"
53     implem="${implem##*opengl/}"
54     implem="${implem%/lib*}"
55     fi
56     unset LDPATH
57     unset OPENGL_PROFILE
58     fi
59    
60     echo ${implem}
61     }
62    
63 niro 617 get_implementations()
64     {
65 niro 616 local implems
66 niro 617
67     for dir in ${PREFIX}/lib{,32,64}/opengl/*
68     do
69     if [[ -d ${dir} ]] &&
70 niro 624 [[ ${dir##*/} != global ]] &&
71     ! hasq ${dir##*/} ${implems}
72 niro 617 then
73 niro 616 implems=${implems:+${implems} }${dir##*/}
74     fi
75     done
76 niro 617
77 niro 616 echo ${implems}
78     }
79    
80 niro 617 print_version()
81     {
82 niro 616 echo "opengl-update ${VERSION}"
83     }
84    
85 niro 617 print_usage()
86     {
87 niro 616 # Get grammar right in message
88     local IS_ARE IMPLEM_PLURAL
89 niro 617
90     if [[ $(echo ${AVAIL_IMPLEMS} | wc -w) -eq 1 ]]
91     then
92 niro 616 IS_ARE="is"
93     IMPLEM_PLURAL=""
94     else
95     IS_ARE="are"
96     IMPLEM_PLURAL="s"
97     fi
98    
99     print_version
100    
101     cat << FOO
102     Usage: ${0##*/} [<options>] <GL implementation>
103     Set the opengl implementation.
104     Valid options:
105 niro 633 --help|-h|-?: Prints this help.
106     --version: Shows the version of this utility.
107 niro 616 --use-old: If an implementation is already set, use that one.
108     --prefix=<val>: Set the source prefix (default: /usr)
109     --dst-prefix=<val>: Set the destination prefix (default: /usr)
110     --impl-headers: Use headers provided by this implementation to
111 niro 632 override global ones provided by opengl-update.
112 niro 616
113     Usage: ${0##*/} --get-implementation
114     Print the current implementaion
115    
116     Notes:
117     --impl-headers was default in <opengl-update-2.2.
118    
119     This utility switches between OpenGL implementations. There ${IS_ARE}
120     $(echo ${AVAIL_IMPLEMS} | wc -w) available implementation${IMPLEM_PLURAL}: ${AVAIL_IMPLEMS}
121    
122     Examples:
123     ${0##*/} xorg
124     This will setup things to use libGL.so from X.org.
125    
126     ${0##*/} nvidia
127     This will setup things to use libGL.so from the nVidia drivers.
128    
129     FOO
130     exit 1
131     }
132    
133 niro 617 parse_options()
134     {
135 niro 616 local opt
136 niro 617
137     while [[ ${#} -gt 0 ]]
138     do
139 niro 616 opt=${1}
140     shift
141     case ${opt} in
142     --use-old)
143 niro 617 if [[ -n ${ACTION} ]]
144     then
145 niro 616 ACTION="error"
146     echo "Invalid usage."
147     else
148 niro 617 if [[ -n "${CURRENT_GL_IMPLEM}" ]] &&
149     hasq ${CURRENT_GL_IMPLEM} ${AVAIL_IMPLEMS}
150     then
151 niro 616 ACTION="old-implementation"
152     fi
153 niro 617 fi
154     ;;
155    
156 niro 616 --get-implementation)
157 niro 617 if [[ -n ${ACTION} ]]
158     then
159 niro 616 ACTION="error"
160     echo "Invalid usage."
161     else
162     ACTION="get-implementation"
163 niro 617 fi
164     ;;
165    
166 niro 616 --prefix=*)
167     PREFIX=${opt#*=}
168     AVAIL_IMPLEMS=$(get_implementations)
169 niro 617 ;;
170    
171     --dst-prefix=*) DST_PREFIX=${opt#*=} ;;
172    
173     --impl-headers) USE_PROFILE_HEADERS="yes" ;;
174    
175 niro 618 --help|-h|-?) ACTION="usage" ;;
176 niro 617
177     --version) ACTION="version" ;;
178    
179 niro 616 *)
180 niro 617 if hasq ${opt} ${AVAIL_IMPLEMS}
181     then
182     if [[ ${ACTION} != old-implementation ]]
183     then
184     if [[ -n ${ACTION} ]]
185     then
186 niro 616 ACTION="error"
187     echo "Invalid usage."
188     else
189     ACTION="set-implementation"
190     NEW_GL_IMPLEM="${opt}"
191     fi
192 niro 617 fi
193 niro 616 else
194     echo "Unrecognized option: ${opt}"
195     ACTION="error"
196     fi
197 niro 617 ;;
198 niro 616 esac
199     done
200     }
201    
202 niro 624 set_new_implementation()
203 niro 617 {
204 niro 616 local GL_IMPLEM=${1}
205     local GL_LOCAL
206    
207     check_user
208    
209     # Set a sane umask... bug #83115
210     umask 022
211    
212 niro 617 if ! hasq ${GL_IMPLEM} ${AVAIL_IMPLEMS}
213     then
214 niro 616 echo "Invalid profile selected."
215     exit 1
216     fi
217    
218     echo "Switching to ${GL_IMPLEM} OpenGL interface"
219    
220 niro 617 rm -f ${ENV_D} &> /dev/null
221 niro 616
222 niro 617 LIBDIRS="lib32 lib lib64"
223     for LIBDIR in ${LIBDIRS}
224     do
225     # Special case handling of lib32 because it can be a symlink to
226     # emul libs
227     if [[ ${LIBDIR} = lib32 ]]
228     then
229     [[ -d ${PREFIX}/${LIBDIR}/opengl ]] || continue
230     else
231     [[ -d ${PREFIX}/${LIBDIR}/opengl ]] &&
232 niro 629 [[ ! -h ${PREFIX}/${LIBDIR} ]] || continue
233 niro 617 fi
234    
235     # Fallback on xorg if we don't have this implementation for this LIBDIR.
236     if [[ ! -d ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM} ]]
237     then
238     GL_LOCAL="xorg"
239     else
240     GL_LOCAL="${GL_IMPLEM}"
241     fi
242    
243     mkdir -p ${DST_PREFIX}/${LIBDIR}
244     pushd ${DST_PREFIX}/${LIBDIR} &> /dev/null
245     # First remove old symlinks
246     for file in libGL{,core}.{a,so,la}
247     do
248     [[ -h ${file} ]] && rm -f ${file}
249     done
250    
251     # Note that we don't do .so*, just .so on purpose. The
252     # loader knows to look in the profile dir, and the
253     # linked just needs the .so
254     for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/libGL{,core}.{so,a,la}
255     do
256     [[ -f ${file} ]] || continue
257     [[ -f ${file##*/} ]] && rm -f ${file##*/}
258    
259     # Fix libtool archives (#48297)
260     if [[ ${file%.la} != ${file} ]]
261     then
262     sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
263     else
264     ln -s ${file}
265     fi
266     done
267     popd &> /dev/null
268    
269     if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls ]]
270     then
271     mkdir -p ${DST_PREFIX}/${LIBDIR}/tls
272     pushd ${DST_PREFIX}/${LIBDIR}/tls &> /dev/null
273 niro 616 # First remove old symlinks
274 niro 617 for file in libGL{,core}.{a,so,la}
275     do
276 niro 616 [[ -h ${file} ]] && rm -f ${file}
277     done
278    
279 niro 617 for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/libGL{,core}.{so,a,la}
280     do
281     [[ -f ${file} ]] || continue
282     [[ -f ${file##*/} ]] && rm -f ${file##*/}
283 niro 616
284     # Fix libtool archives (#48297)
285 niro 617 if [[ ${file%.la} != ${file} ]]
286     then
287 niro 616 sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
288     else
289     ln -s ${file}
290     fi
291     done
292     popd &> /dev/null
293 niro 617 fi
294 niro 616
295 niro 617 local MODULEDIR
296     if [[ -e ${DST_PREFIX}/${LIBDIR}/xorg/modules ]]
297     then
298     MODULEDIR="xorg/modules"
299     else
300     MODULEDIR="modules"
301     fi
302 niro 616
303 niro 617 if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/extensions ]]
304     then
305     mkdir -p ${DST_PREFIX}/${LIBDIR}/${MODULEDIR}/extensions
306     pushd ${DST_PREFIX}/${LIBDIR}/${MODULEDIR}/extensions &> /dev/null
307     # First remove old symlinks
308     for file in libglx.so libglx.a
309     do
310     [[ -h ${file} ]] && rm -f ${file}
311     done
312 niro 616
313 niro 617 for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/extensions/*.{so,a,la}
314     do
315     [[ -f ${file} ]] || continue
316     [[ -f ${file##*/} ]] && rm -f ${file##*/}
317 niro 616
318 niro 617 # Fix libtool archives (#48297)
319     if [[ ${file%.la} != ${file} ]]
320     then
321     sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
322     else
323     ln -s ${file}
324     fi
325     done
326     popd &> /dev/null
327     fi
328 niro 616
329 niro 617 # Setup the includes
330     mkdir -p ${DST_PREFIX}/include/GL
331     pushd ${DST_PREFIX}/include/GL &> /dev/null
332     for file in gl.h glx.h glxtokens.h glext.h glxext.h glxmd.h glxproto.h
333     do
334     # IMPORTANT
335     # It is preferable currently to use the standard glext.h file
336     # however if an OpenGL provider must use a self produced glext.h
337     # then it should be installed to ${GL_IMPLEM}/include and the user
338     # can add the --impl-headers option to select it.
339 niro 616
340 niro 617 if [[ ${USE_PROFILE_HEADERS} = yes ]]
341     then
342     # Check the profile first.
343     if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file} ]]
344     then
345 niro 616 [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
346     ln -s ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file}
347     fi
348 niro 617 continue
349     fi
350 niro 616
351 niro 617 if [[ -e ${PREFIX}/${LIBDIR}/opengl/global/include/${file} ]]
352     then
353     [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
354     ln -s ${PREFIX}/${LIBDIR}/opengl/global/include/${file}
355     elif [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file} ]]
356     then
357     [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
358     ln -s ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file}
359     elif [[ -e ${PREFIX}/${LIBDIR}/opengl/xorg/include/${file} ]]
360     then
361     [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
362     ln -s ${PREFIX}/${LIBDIR}/opengl/xorg/include/${file}
363     fi
364     done
365     popd &> /dev/null
366 niro 616
367 niro 617 # Setup the $LDPATH
368     ldpath="${ldpath:+${ldpath}:}${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib"
369 niro 616
370 niro 617 done
371 niro 616
372 niro 621 # put only one LDPATH per line!
373 niro 620 local i
374     for i in $(echo ${ldpath} | sed "s:\::\ :g")
375     do
376     echo "LDPATH=\"${i}\"" >> ${ENV_D}
377     done
378 niro 617 echo "OPENGL_PROFILE=\"${GL_IMPLEM}\"" >> ${ENV_D}
379 niro 616
380 niro 617 env-rebuild
381    
382 niro 616 return 0
383     }
384    
385     ## START PROGRAM ##
386    
387     ENV_D="/etc/env.d/03opengl"
388     NEW_GL_IMPLEM=""
389     ACTION=""
390     PREFIX="/usr"
391     DST_PREFIX="/usr"
392     AVAIL_IMPLEMS=$(get_implementations)
393     CURRENT_GL_IMPLEM=$(get_current_implem)
394     USE_PROFILE_HEADERS="no"
395 niro 619 VERSION="@@VERSION@@"
396 niro 616
397     parse_options ${@}
398    
399     case ${ACTION} in
400     get-implementation)
401 niro 617 if [[ -n ${CURRENT_GL_IMPLEM} ]]
402     then
403 niro 616 echo ${CURRENT_GL_IMPLEM}
404     exit 0
405     else
406     exit 2
407     fi
408 niro 617 ;;
409    
410 niro 616 old-implementation)
411 niro 624 set_new_implementation ${CURRENT_GL_IMPLEM}
412 niro 616 exit $?
413 niro 617 ;;
414    
415 niro 616 set-implementation)
416 niro 617 if [[ -n ${NEW_GL_IMPLEM} ]]
417     then
418 niro 624 set_new_implementation ${NEW_GL_IMPLEM}
419 niro 616 exit $?
420     else
421     print_usage
422     exit 1
423     fi
424 niro 617 ;;
425    
426 niro 616 version)
427     print_version
428     exit 0
429 niro 617 ;;
430    
431 niro 616 usage)
432     print_usage
433     exit 0
434 niro 617 ;;
435    
436 niro 616 error)
437     print_usage
438     exit 1
439 niro 617 ;;
440    
441 niro 616 *)
442     print_usage
443     exit 1
444 niro 617 ;;
445 niro 616 esac