bin_parabola-java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #! /bin/bash
  2. # Parabola GNU/linux-libre helper script to set/unset/check/fix the enabled Java environment
  3. # This program may be freely redistributed under the terms of the GNU General Public License
  4. #
  5. # Author: Guillaume ALAUX <guillaume@archlinux.org>
  6. # Parabola Rebranding Author: André Silva <emulatorman@parabola.nu>
  7. JVM_DIR=/usr/lib/jvm
  8. DEFAULT_NAME=default
  9. DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME}
  10. DEFAULT_NAME_JRE=default-runtime
  11. DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE}
  12. # Utility functions
  13. check_root() {
  14. if [ $(id -u) -ne 0 ]; then
  15. echo 'This script must be run as root'
  16. exit 1
  17. fi
  18. }
  19. # $1: parameter count given to this script for this option
  20. # $2: expected parameter count for this option
  21. check_param_count() {
  22. if [ $1 -ne $2 ]; then
  23. echo 'Wrong parameter count'
  24. exit 2
  25. fi
  26. }
  27. # Second level functions
  28. get_default_java() {
  29. path=$(readlink -e ${DEFAULT_PATH})
  30. if [ "x${path}" != "x/dev/null" ]; then
  31. echo ${path/${JVM_DIR}\/}
  32. else
  33. echo ""
  34. fi
  35. }
  36. get_installed_javas() {
  37. if [ -d ${JVM_DIR} ]; then
  38. for dir in $(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d | sort); do
  39. if [ -x ${dir}/bin/java ]; then
  40. javas+=(${dir/${JVM_DIR}\/})
  41. else
  42. if [ -x ${dir}/jre/bin/java ]; then
  43. javas+=(${dir/${JVM_DIR}\/}/jre)
  44. fi
  45. fi
  46. done
  47. fi
  48. echo ${javas[@]}
  49. }
  50. # $1: Java environment name to test
  51. is_java_valid() {
  52. test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java
  53. }
  54. # $1: Java environment name to set as default
  55. set_default_link_to() {
  56. new_default=$1
  57. unlink ${DEFAULT_PATH} 2>/dev/null
  58. ln -sf ${new_default} ${DEFAULT_PATH}
  59. unlink ${DEFAULT_PATH_JRE} 2>/dev/null
  60. if [[ -d ${new_default}/jre ]]; then
  61. ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE}
  62. else
  63. ln -sf ${new_default} ${DEFAULT_PATH_JRE}
  64. fi
  65. }
  66. unset_default_link() {
  67. unlink ${DEFAULT_PATH} 2>/dev/null
  68. unlink ${DEFAULT_PATH_JRE} 2>/dev/null
  69. }
  70. # First level functions
  71. do_status() {
  72. installed_java=($(get_installed_javas))
  73. if [ ${#installed_java[@]} -eq 0 ]; then
  74. echo 'No compatible Java environment installed'
  75. else
  76. default_java=$(get_default_java)
  77. echo 'Available Java environments:'
  78. for java in ${installed_java[@]}; do
  79. # We discoverd this Java env but its JRE is actually set as default
  80. if [ "${java}/jre" = "${default_java}" ]; then
  81. echo -e " ${java} (${java}/jre default)"
  82. elif [ ${java} = "${default_java}" ]; then
  83. echo -e " ${java} (default)"
  84. else
  85. echo " ${java}"
  86. fi
  87. done
  88. if [ -z ${default_java} ]; then
  89. echo "No Java environment set as default"
  90. fi
  91. fi
  92. }
  93. do_get() {
  94. get_default_java
  95. }
  96. # $1: Java environment name to set as default
  97. do_set() {
  98. if ! is_java_valid $1; then
  99. echo "'${JVM_DIR}/$1' is not a valid Java environment path"
  100. exit 1
  101. fi
  102. default=$(get_default_java)
  103. if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then
  104. unset_default_link
  105. set_default_link_to $1
  106. fi
  107. #parent_dir=$(dirname $1)
  108. #if is_java_valid ${parent_dir}; then
  109. # echo "Warning: '${parent_dir}' looks like a valid JDK whereas '$1' is set as default"
  110. # echo "Fix this with 'parabola-java set ${parent_dir}'"
  111. #fi
  112. }
  113. # $1: Java environment name to unset
  114. do_unset() {
  115. unset_default_link
  116. }
  117. do_fix() {
  118. default=$(get_default_java)
  119. if is_java_valid ${default}; then
  120. # If its parent is also a valid Java environment
  121. if is_java_valid $(dirname ${default}); then
  122. unset_default_link
  123. set_default_link_to $(dirname ${default})
  124. fi
  125. else
  126. prev=$(readlink ${DEFAULT_PATH})
  127. unset_default_link
  128. potential_fixes=("${prev/\/jre}" "${prev}/jre")
  129. openjdk8=('java-8-openjdk' 'java-8-openjdk/jre')
  130. # List of environments to check by order of preference:
  131. # - first potential fixes of user choices,
  132. # - then OpenJDK8 as it is considered a default in Parabola
  133. # - finally, any installed environment
  134. to_check=(${potential_fixes[@]} ${openjdk8[@]} $(get_installed_javas))
  135. for java in ${to_check[@]}; do
  136. if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then
  137. set_default_link_to ${java}
  138. fi
  139. done
  140. fi
  141. if ! is_java_valid $(get_default_java); then
  142. echo 'No valid Java environment found'
  143. fi
  144. }
  145. usage() {
  146. echo "$(basename $0) <COMMAND>"
  147. echo -e "\nCOMMAND:"
  148. echo -e '\tstatus\t\tList installed Java environments and enabled one'
  149. echo -e '\tget\t\tReturn the short name of the Java environment set as default'
  150. echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default'
  151. echo -e '\tunset\t\tUnset current default Java environment'
  152. echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration'
  153. }
  154. ## Main
  155. case $1 in
  156. 'status') do_status;;
  157. 'get') do_get;;
  158. 'set') check_root; check_param_count $# 2; do_set $2;;
  159. 'unset') check_root; do_unset;;
  160. 'fix') check_root; do_fix;;
  161. 'help' | '--help' | '-h' | '') usage;;
  162. *) echo "$(basename $0): unknown option '$@'"; exit 1;;
  163. esac