home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / sbin / update-java-alternatives < prev    next >
Encoding:
Text File  |  2006-06-15  |  2.7 KB  |  146 lines

  1. #! /bin/bash
  2.  
  3. shopt -s dotglob
  4.  
  5. prog=$(basename $0)
  6.  
  7. usage()
  8. {
  9.     rv=$1
  10.     cat >&2 <<-EOF
  11.     usage: $prog [--jre] [--plugin] [ -t|--test|-v|--verbose]
  12.                -l|--list [<jname>]
  13.                -s|--set <jname>
  14.                -a|--auto
  15.                -h|-?|--help
  16.     EOF
  17.     exit $rv
  18. }
  19.  
  20. action=
  21. uaopts='--quiet'
  22.  
  23. while [ "$#" -gt 0 ]; do
  24.     case "$1" in
  25.       -a|--auto)
  26.     [ -z "$action" ] || usage 1
  27.     action=auto;;
  28.       -j|--jre)
  29.     jreonly=yes;;
  30.       --plugin)
  31.     pluginonly=yes;;
  32.       -l|--list)
  33.     [ -z "$action" ] || usage 1
  34.     action=list
  35.     if [ "$#" -gt 0 ]; then
  36.         shift
  37.         jname=$1
  38.     fi
  39.     ;;
  40.       -s|--set)
  41.     [ -z "$action" ] || usage 1
  42.     action=set
  43.     shift
  44.     [ "$#" -gt 0 ] || usage 1
  45.     jname=$1
  46.     ;;
  47.       -t|--test)
  48.     dryrun=yes
  49.     uaopts="$uaopts --test";;
  50.       -v|--verbose)
  51.     verbose=yes
  52.     uaopts="${uaopts/--quiet/}";;
  53.       -h|-?|--help)
  54.     usage 0;;
  55.       -*)
  56.     usage 1;;
  57.       *)
  58.     break;;
  59.     esac
  60.     shift
  61. done
  62.  
  63. [ "$#" -eq 0 ] || usage 1
  64. [ -n "$action" ] || usage 1
  65.  
  66. which='^(jre|jdk|plugin|DUMMY) '
  67. [ -n "$jreonly$pluginonly" ] && which=${which/jdk|/}
  68. [ -n "$pluginonly" ] && [ -z "$jreonly" ] && which=${which/jre|/}
  69. [ -z "$pluginonly" ] && [ -n "$jreonly" ] && which=${which/plugin|/}
  70.  
  71. top=/usr/lib/jvm
  72.  
  73. if [ -n "$jname" ]; then
  74.     case "$jname" in
  75.     /*) jdir=$jname; jname=$(basename $jdir);;
  76.     *)  jdir=$top/$jname
  77.     esac
  78.     if [ ! -d $jdir ]; then
  79.     echo >&2 "$prog: directory does not exist: $jdir"
  80.     exit 1
  81.     fi
  82.     if [ -f $top/.$jname.jinfo ]; then
  83.     jinfo=$top/.$jname.jinfo
  84.     elif [ -f $top/$jname.jinfo ]; then
  85.     jinfo=$top/$jname.jinfo
  86.     else
  87.     echo >&2 "$prog: file does not exist: $top/.$jname.jinfo"
  88.     exit 1
  89.     fi
  90. fi
  91.  
  92. vecho()
  93. {
  94.     [ -z "$verbose" ]  || echo >&2 "$@"
  95. }
  96.  
  97. jinfo_files=
  98.  
  99. do_auto()
  100. {
  101.     vecho "resetting java alternatives"
  102.     awk "/$which/ {print \$2}" $top/*.jinfo | sort -u \
  103.     | \
  104.     while read name; do
  105.     update-alternatives $uaopts --auto $name
  106.     done
  107. }
  108.  
  109. do_list()
  110. {
  111.     vecho "listing java alternatives:"
  112.     for i in $top/${jname:-*}.jinfo; do
  113.     alias=$(basename ${i%*.jinfo})
  114.     alias=${alias#.*}
  115.     prio=$(awk -F= '/priority=/ {print $2}' $i)
  116.     echo $alias $prio $top/$alias
  117.     [ -n "$verbose" ] && egrep "$which" $i
  118.     done
  119. }
  120.  
  121. do_set()
  122. {
  123.     do_auto
  124.  
  125.     awk "/$which/ {print}" $jinfo | sort -u \
  126.     | \
  127.     while read type name location; do
  128.     if [ $type = jdk -a -z "$jreonly$pluginonly" -a ! -f $location ]; then
  129.         echo >&2 "$prog: jdk alternative does not exist: $location"
  130.         continue
  131.     fi
  132.     if [ $type = plugin -a -z "$jreonly" -a ! -f $location ]; then
  133.         echo >&2 "$prog: plugin alternative does not exist: $location"
  134.         continue
  135.     fi
  136.     update-alternatives $uaopts --set $name $location
  137.     done
  138. }
  139.  
  140. if [ "$action" != list ] && [ "$(id -u)" -ne 0 ]; then
  141.     echo >&2 "$prog: no root privileges"
  142.     exit 1
  143. fi
  144.  
  145. do_$action
  146.