|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2018 Joni Van Roost |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +if [ `id -u` -ne 0 ] |
| 26 | +then |
| 27 | + echo "Permission denied. Please start with sudo." |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +echo "Select a Java version you want to use:" |
| 32 | + |
| 33 | +# Collect the folders in the array $folders |
| 34 | +folders=( /Library/Java/JavaVirtualMachines/* ) |
| 35 | + |
| 36 | +# Enable extended globbing. This lets us use @(foo|bar) to |
| 37 | +# match either 'foo' or 'bar'. |
| 38 | +shopt -s extglob |
| 39 | + |
| 40 | +# Start building the string to match against. |
| 41 | +string="@(${folders[0]}" |
| 42 | + |
| 43 | +# Add the rest of the folders to the string |
| 44 | +for((i=1;i<${#folders[@]};i++)) |
| 45 | +do |
| 46 | + string+="|${folders[$i]}" |
| 47 | +done |
| 48 | + |
| 49 | +# Close the parenthesis. $string is now @(folder1|folder2|...|folderN) |
| 50 | +string+=")" |
| 51 | + |
| 52 | +# Show the menu. This will list all folders and the string "quit" |
| 53 | +select selected_java_version in "${folders[@]}" "quit" |
| 54 | +do |
| 55 | + case $selected_java_version in |
| 56 | + # If the choice is one of the folders (if it matches $string) |
| 57 | + $string) |
| 58 | + echo "Setting Java version from \"$selected_java_version\" as current version." |
| 59 | + |
| 60 | + # Loop through java versions again |
| 61 | + for java_version in ${folders[@]} |
| 62 | + do |
| 63 | + # If there's no info.plist or info.plist.disabled file, notify the user. |
| 64 | + if [ ! -f "$java_version/Contents/Info.plist" ] && |
| 65 | + [ ! -f "$java_version/Contents/Info.plist.disabled" ] |
| 66 | + then |
| 67 | + echo "No Info.plist or Info.plist.disabled file found for $java_version. \n Please check this installation." |
| 68 | + break |
| 69 | + fi |
| 70 | + |
| 71 | + # When a java version does not match the selected version |
| 72 | + # if there is an Info.plist file, rename -> Info.plist.disabled |
| 73 | + if [ "$java_version" != "$selected_java_version" ] && |
| 74 | + [ -f "$java_version/Contents/Info.plist" ] |
| 75 | + then |
| 76 | + `mv $java_version/Contents/Info.plist $java_version/Contents/Info.plist.disabled` |
| 77 | + |
| 78 | + # When a java version matches the selected version |
| 79 | + # if there is no Info.plist file but there is an Info.plist.disabled, rename -> Info.plist |
| 80 | + elif [ "$java_version" == "$selected_java_version" ] && |
| 81 | + [ ! -f "$selected_java_version/Contents/Info.plist" ] && |
| 82 | + [ -f "$selected_java_version/Contents/Info.plist.disabled" ] |
| 83 | + then |
| 84 | + `mv $java_version/Contents/Info.plist.disabled $java_version/Contents/Info.plist` |
| 85 | + fi |
| 86 | + done |
| 87 | + |
| 88 | + break; |
| 89 | + ;; |
| 90 | + |
| 91 | + "quit") |
| 92 | + # Exit |
| 93 | + exit;; |
| 94 | + *) |
| 95 | + selected_java_version="" |
| 96 | + echo "Please choose a number from 1 to $((${#folders[@]}+1))";; |
| 97 | + esac |
| 98 | +done |
| 99 | + |
| 100 | +echo "Done!" |
| 101 | + |
| 102 | +exit 0 |
0 commit comments