home *** CD-ROM | disk | FTP | other *** search
- #! /bin/bash
-
- shopt -s dotglob
-
- prog=$(basename $0)
-
- usage()
- {
- rv=$1
- cat >&2 <<-EOF
- usage: $prog [--jre] [--plugin] [ -t|--test|-v|--verbose]
- -l|--list [<jname>]
- -s|--set <jname>
- -a|--auto
- -h|-?|--help
- EOF
- exit $rv
- }
-
- action=
- uaopts='--quiet'
-
- while [ "$#" -gt 0 ]; do
- case "$1" in
- -a|--auto)
- [ -z "$action" ] || usage 1
- action=auto;;
- -j|--jre)
- jreonly=yes;;
- --plugin)
- pluginonly=yes;;
- -l|--list)
- [ -z "$action" ] || usage 1
- action=list
- if [ "$#" -gt 0 ]; then
- shift
- jname=$1
- fi
- ;;
- -s|--set)
- [ -z "$action" ] || usage 1
- action=set
- shift
- [ "$#" -gt 0 ] || usage 1
- jname=$1
- ;;
- -t|--test)
- dryrun=yes
- uaopts="$uaopts --test";;
- -v|--verbose)
- verbose=yes
- uaopts="${uaopts/--quiet/}";;
- -h|-?|--help)
- usage 0;;
- -*)
- usage 1;;
- *)
- break;;
- esac
- shift
- done
-
- [ "$#" -eq 0 ] || usage 1
- [ -n "$action" ] || usage 1
-
- which='^(jre|jdk|plugin|DUMMY) '
- [ -n "$jreonly$pluginonly" ] && which=${which/jdk|/}
- [ -n "$pluginonly" ] && [ -z "$jreonly" ] && which=${which/jre|/}
- [ -z "$pluginonly" ] && [ -n "$jreonly" ] && which=${which/plugin|/}
-
- top=/usr/lib/jvm
-
- if [ -n "$jname" ]; then
- case "$jname" in
- /*) jdir=$jname; jname=$(basename $jdir);;
- *) jdir=$top/$jname
- esac
- if [ ! -d $jdir ]; then
- echo >&2 "$prog: directory does not exist: $jdir"
- exit 1
- fi
- if [ -f $top/.$jname.jinfo ]; then
- jinfo=$top/.$jname.jinfo
- elif [ -f $top/$jname.jinfo ]; then
- jinfo=$top/$jname.jinfo
- else
- echo >&2 "$prog: file does not exist: $top/.$jname.jinfo"
- exit 1
- fi
- fi
-
- vecho()
- {
- [ -z "$verbose" ] || echo >&2 "$@"
- }
-
- jinfo_files=
-
- do_auto()
- {
- vecho "resetting java alternatives"
- awk "/$which/ {print \$2}" $top/*.jinfo | sort -u \
- | \
- while read name; do
- update-alternatives $uaopts --auto $name
- done
- }
-
- do_list()
- {
- vecho "listing java alternatives:"
- for i in $top/${jname:-*}.jinfo; do
- alias=$(basename ${i%*.jinfo})
- alias=${alias#.*}
- prio=$(awk -F= '/priority=/ {print $2}' $i)
- echo $alias $prio $top/$alias
- [ -n "$verbose" ] && egrep "$which" $i
- done
- }
-
- do_set()
- {
- do_auto
-
- awk "/$which/ {print}" $jinfo | sort -u \
- | \
- while read type name location; do
- if [ $type = jdk -a -z "$jreonly$pluginonly" -a ! -f $location ]; then
- echo >&2 "$prog: jdk alternative does not exist: $location"
- continue
- fi
- if [ $type = plugin -a -z "$jreonly" -a ! -f $location ]; then
- echo >&2 "$prog: plugin alternative does not exist: $location"
- continue
- fi
- update-alternatives $uaopts --set $name $location
- done
- }
-
- if [ "$action" != list ] && [ "$(id -u)" -ne 0 ]; then
- echo >&2 "$prog: no root privileges"
- exit 1
- fi
-
- do_$action
-