#!/bin/bash # Copyright 1999-2004 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: /home/cvsd/magellan-cvs/magellan-src/opengl-update/opengl-update.sh,v 1.10 2007-11-14 19:19:16 niro Exp $ # Author: Martin Schlemmer # Further modifications by Donnie Berkholz # Further modifications based off submissions to bug #54984 # Further modifications by Jeremy Huddleston # # hacked for use with magellan-linux # # hasq() { local x local me=$1 shift for x in $@ do [[ ${x} = ${me} ]] && return 0 done return 1 } check_user() { if [[ $(id -u) -ne 0 ]] then echo "$0: Must be run as root." exit 1 fi } get_current_implem() { local implem # jre does always exist! if [ -f ${ENV_D_JRE} ] then source "${ENV_D_JRE}" if [[ -n ${JAVA_PROFILE} ]] then implem="${JAVA_PROFILE}" fi unset JAVA_PROFILE fi echo "${implem}" } get_implementations() { local implems for dir in ${PREFIX}/java/* do if [ -d ${dir} ] && [[ ${dir##*/} != global ]] && ! hasq "${dir##*/}" "${implems}" then implems="${implems:+${implems} }${dir##*/}" fi done echo "${implems}" } print_version() { echo "java-update ${VERSION}" } print_usage() { # Get grammar right in message local IS_ARE IMPLEM_PLURAL if [[ $(echo ${AVAIL_IMPLEMS} | wc -w) -eq 1 ]] then IS_ARE="is" IMPLEM_PLURAL="" else IS_ARE="are" IMPLEM_PLURAL="s" fi print_version cat << FOO Usage: ${0##*/} [] Set the Java implementation. Valid options: --help|-h|-?: Prints this help. --version: Shows the version of this utility. --use-old: If an implementation is already set, use that one. Usage: ${0##*/} --get-implementation Print the current implementaion This utility switches between Java implementations. There ${IS_ARE} $(echo ${AVAIL_IMPLEMS} | wc -w) available implementation${IMPLEM_PLURAL}: ${AVAIL_IMPLEMS} Examples: ${0##*/} java6-sun This will setup things to use Java Version 6 from Sun. ${0##*/} java7-openjdk This will setup things to use OpenJDK Version 7. FOO exit 1 } parse_options() { local opt while [[ ${#} -gt 0 ]] do opt=$1 shift case ${opt} in --use-old) if [[ -n ${ACTION} ]] then ACTION="error" echo "Invalid usage." else if [[ -n "${CURRENT_JAVA_IMPLEM}" ]] && hasq "${CURRENT_JAVA_IMPLEM}" "${AVAIL_IMPLEMS}" then ACTION="old-implementation" fi fi ;; --get-implementation) if [[ -n ${ACTION} ]] then ACTION="error" echo "Invalid usage." else ACTION="get-implementation" fi ;; --help|-h|-?) ACTION="usage" ;; --version) ACTION="version" ;; *) if hasq "${opt}" "${AVAIL_IMPLEMS}" then if [[ ${ACTION} != old-implementation ]] then if [[ -n ${ACTION} ]] then ACTION="error" echo "Invalid usage." else ACTION="set-implementation" NEW_JAVA_IMPLEM="${opt}" fi fi else echo "Unrecognized option: ${opt}" ACTION="error" fi ;; esac done } set_new_implementation() { local JAVA_IMPLEM="$1" local JAVA_LOCAL check_user # Set a sane umask... bug #83115 umask 022 if ! hasq "${JAVA_IMPLEM}" "${AVAIL_IMPLEMS}" then echo "Invalid profile selected." exit 1 fi echo "Switching to ${JAVA_IMPLEM} Java interface" [ -f ${ENV_D_JRE} ] && rm -f ${ENV_D_JRE} [ -f ${ENV_D_JDK} ] && rm -f ${ENV_D_JDK} [ -f ${ENV_D_GLOBAL_CLASSPATH} ] && rm -f ${ENV_D_GLOBAL_CLASSPATH} [ -f ${ENV_D_EOF_CLASSPATH} ] && rm -f ${ENV_D_EOF_CLASSPATH} cat ${PREFIX}/java/${JAVA_IMPLEM}/jre.conf >> ${ENV_D_JRE} echo "JAVA_PROFILE=\"${JAVA_IMPLEM}\"" >> ${ENV_D_JRE} # set global classpath if [ -f ${PREFIX}/java/${JAVA_IMPLEM}/global-classpath.conf ] then cat ${PREFIX}/java/${JAVA_IMPLEM}/global-classpath.conf >> ${ENV_D_GLOBAL_CLASSPATH} elif [ -f ${PREFIX}/java/global/global-classpath.conf ] then cat ${PREFIX}/java/global/global-classpath.conf >> ${ENV_D_GLOBAL_CLASSPATH} else echo "Warning: global-classpath.conf is missing!" fi # set eof classpath if [ -f ${PREFIX}/java/${JAVA_IMPLEM}/eof-classpath.conf ] then cat ${PREFIX}/java/${JAVA_IMPLEM}/eof-classpath.conf >> ${ENV_D_EOF_CLASSPATH} elif [ -f ${PREFIX}/java/global/eof-classpath.conf ] then cat ${PREFIX}/java/global/eof-classpath.conf >> ${ENV_D_EOF_CLASSPATH} else echo "Warning: eof-classpath.conf is missing!" fi # set jdk too if installed if [ -f ${PREFIX}/java/${JAVA_IMPLEM}/jdk.conf ] then cat ${PREFIX}/java/${JAVA_IMPLEM}/jdk.conf >> ${ENV_D_JDK} echo "JAVA_PROFILE=\"${JAVA_IMPLEM}\"" >> ${ENV_D_JDK} fi env-rebuild return 0 } ## START PROGRAM ## ENV_D_JRE="/etc/env.d/21java" ENV_D_JDK="/etc/env.d/22java-jdk" ENV_D_GLOBAL_CLASSPATH="/etc/env.d/22java-global-classpath" ENV_D_EOF_CLASSPATH="/etc/env.d/30java-eof-classpath" NEW_JAVA_IMPLEM="" ACTION="" PREFIX="/etc" AVAIL_IMPLEMS=$(get_implementations) CURRENT_JAVA_IMPLEM=$(get_current_implem) VERSION="@@VERSION@@" parse_options $@ case ${ACTION} in get-implementation) if [[ -n ${CURRENT_JAVA_IMPLEM} ]] then echo ${CURRENT_JAVA_IMPLEM} retval=0 else retval=2 fi ;; old-implementation) set_new_implementation ${CURRENT_JAVA_IMPLEM} retval=$? ;; set-implementation) if [[ -n ${NEW_JAVA_IMPLEM} ]] then set_new_implementation ${NEW_JAVA_IMPLEM} retval=$? else print_usage retval=1 fi ;; version) print_version; retval=0 ;; usage) print_usage; retval=0 ;; error) print_usage; retval=1 ;; *) print_usage; retval=1;; esac exit "${retval}"