archrelease: copy trunk to extra-x86_64
[arch-packages.git] / java-common / repos / extra-any / bin_archlinux-java
blob5c5937bfe9169d2fc46aa60e82b9198d4f3e8048
1 #! /bin/bash
3 # Arch Linux helper script to set/unset/check/fix the enabled Java environment
4 # This program may be freely redistributed under the terms of the GNU General Public License
6 # Author: Guillaume ALAUX <guillaume@archlinux.org>
8 JVM_DIR=/usr/lib/jvm
9 DEFAULT_NAME=default
10 DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME}
11 DEFAULT_NAME_JRE=default-runtime
12 DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE}
15 # Utility functions
17 check_root() {
18 if [ $(id -u) -ne 0 ]; then
19 echo 'This script must be run as root'
20 exit 1
24 # $1: parameter count given to this script for this option
25 # $2: expected parameter count for this option
26 check_param_count() {
27 if [ $1 -ne $2 ]; then
28 echo 'Wrong parameter count'
29 exit 2
33 # Second level functions
35 get_default_java() {
36 path=$(readlink -e ${DEFAULT_PATH})
37 if [ "x${path}" != "x/dev/null" ]; then
38 echo ${path/${JVM_DIR}\/}
39 else
40 echo ""
44 get_installed_javas() {
45 if [ -d ${JVM_DIR} ]; then
46 for dir in $(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d | sort); do
47 if [ -x ${dir}/bin/java ]; then
48 javas+=(${dir/${JVM_DIR}\/})
49 else
50 if [ -x ${dir}/jre/bin/java ]; then
51 javas+=(${dir/${JVM_DIR}\/}/jre)
54 done
56 echo ${javas[@]}
59 # $1: Java environment name to test
60 is_java_valid() {
61 test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java
64 # $1: Java environment name to set as default
65 set_default_link_to() {
66 new_default=$1
67 unlink ${DEFAULT_PATH} 2>/dev/null
68 ln -sf ${new_default} ${DEFAULT_PATH}
70 unlink ${DEFAULT_PATH_JRE} 2>/dev/null
71 if [[ -d ${new_default}/jre ]]; then
72 ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE}
73 else
74 ln -sf ${new_default} ${DEFAULT_PATH_JRE}
78 unset_default_link() {
79 unlink ${DEFAULT_PATH} 2>/dev/null
80 unlink ${DEFAULT_PATH_JRE} 2>/dev/null
83 # First level functions
85 do_status() {
86 installed_java=($(get_installed_javas))
87 if [ ${#installed_java[@]} -eq 0 ]; then
88 echo 'No compatible Java environment installed'
89 else
90 default_java=$(get_default_java)
91 echo 'Available Java environments:'
92 for java in ${installed_java[@]}; do
93 # We discoverd this Java env but its JRE is actually set as default
94 if [ "${java}/jre" = "${default_java}" ]; then
95 echo -e " ${java} (${java}/jre default)"
96 elif [ ${java} = "${default_java}" ]; then
97 echo -e " ${java} (default)"
98 else
99 echo " ${java}"
101 done
102 if [ -z ${default_java} ]; then
103 echo "No Java environment set as default"
108 do_get() {
109 get_default_java
112 # $1: Java environment name to set as default
113 do_set() {
114 if ! is_java_valid $1; then
115 echo "'${JVM_DIR}/$1' is not a valid Java environment path"
116 exit 1
118 default=$(get_default_java)
119 if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then
120 unset_default_link
121 set_default_link_to $1
124 #parent_dir=$(dirname $1)
125 #if is_java_valid ${parent_dir}; then
126 # echo "Warning: '${parent_dir}' looks like a valid JDK whereas '$1' is set as default"
127 # echo "Fix this with 'archlinux-java set ${parent_dir}'"
131 # $1: Java environment name to unset
132 do_unset() {
133 unset_default_link
136 do_fix() {
137 default=$(get_default_java)
138 if is_java_valid ${default}; then
139 # If its parent is also a valid Java environment
140 if is_java_valid $(dirname ${default}); then
141 unset_default_link
142 set_default_link_to $(dirname ${default})
144 else
145 prev=$(readlink ${DEFAULT_PATH})
146 unset_default_link
147 potential_fixes=("${prev/\/jre}" "${prev}/jre")
148 openjdk8=('java-8-openjdk' 'java-8-openjdk/jre')
149 # List of environments to check by order of preference:
150 # - first potential fixes of user choices,
151 # - then OpenJDK8 as it is considered a default in Arch Linux
152 # - finally, any installed environment
153 to_check=(${potential_fixes[@]} ${openjdk8[@]} $(get_installed_javas))
154 for java in ${to_check[@]}; do
155 if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then
156 set_default_link_to ${java}
158 done
160 if ! is_java_valid $(get_default_java); then
161 echo 'No valid Java environment found'
165 usage() {
166 echo "$(basename $0) <COMMAND>"
167 echo -e "\nCOMMAND:"
168 echo -e '\tstatus\t\tList installed Java environments and enabled one'
169 echo -e '\tget\t\tReturn the short name of the Java environment set as default'
170 echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default'
171 echo -e '\tunset\t\tUnset current default Java environment'
172 echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration'
175 ## Main
176 case $1 in
177 'status') do_status;;
178 'get') do_get;;
179 'set') check_root; check_param_count $# 2; do_set $2;;
180 'unset') check_root; do_unset;;
181 'fix') check_root; do_fix;;
182 'help' | '--help' | '-h' | '') usage;;
183 *) echo "$(basename $0): unknown option '$@'"; exit 1;;
184 esac