altosui: open /Library/AltusMetrum on Mac OS X after install
[fw/altos] / telegps / TeleGPS.app / Contents / MacOS / JavaApplicationStub
1 #!/bin/bash
2 #
3 # Fix fonts. I don't know why the getting the
4 # basename of the app set to . matters, but it does
5 #
6 case "$0" in
7     /*)
8         cd `dirname "$0"`
9         ./`basename "$0"` "$@"
10         exit $?
11         ;;
12 esac
13 export FREETYPE_PROPERTIES=truetype:interpreter-version=35
14 ##################################################################################
15 #                                                                                #
16 # universalJavaApplicationStub                                                   #
17 #                                                                                #
18 # A BASH based JavaApplicationStub for Java Apps on Mac OS X                     #
19 # that works with both Apple's and Oracle's plist format.                        #
20 #                                                                                #
21 # Inspired by Ian Roberts stackoverflow answer                                   #
22 # at http://stackoverflow.com/a/17546508/1128689                                 #
23 #                                                                                #
24 # @author    Tobias Fischer                                                      #
25 # @url       https://github.com/tofi86/universalJavaApplicationStub              #
26 # @date      2018-08-24                                                          #
27 # @version   3.0.4                                                               #
28 #                                                                                #
29 ##################################################################################
30 #                                                                                #
31 # The MIT License (MIT)                                                          #
32 #                                                                                #
33 # Copyright (c) 2014-2018 Tobias Fischer                                         #
34 #                                                                                #
35 # Permission is hereby granted, free of charge, to any person obtaining a copy   #
36 # of this software and associated documentation files (the "Software"), to deal  #
37 # in the Software without restriction, including without limitation the rights   #
38 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      #
39 # copies of the Software, and to permit persons to whom the Software is          #
40 # furnished to do so, subject to the following conditions:                       #
41 #                                                                                #
42 # The above copyright notice and this permission notice shall be included in all #
43 # copies or substantial portions of the Software.                                #
44 #                                                                                #
45 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     #
46 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       #
47 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    #
48 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         #
49 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  #
50 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  #
51 # SOFTWARE.                                                                      #
52 #                                                                                #
53 ##################################################################################
54
55
56
57 # function 'stub_logger()'
58 #
59 # A logger which logs to the macOS Console.app using the 'syslog' command
60 #
61 # @param1  the log message
62 # @return  void
63 ################################################################################
64 function stub_logger() {
65         syslog -s -k \
66                 Facility com.apple.console \
67                 Level Notice \
68                 Sender "$(basename "$0")" \
69                 Message "[$$][${CFBundleName:-$(basename "$0")}] $1"
70 }
71
72
73
74 # set the directory abspath of the current
75 # shell script with symlinks being resolved
76 ############################################
77
78 PRG=$0
79 while [ -h "$PRG" ]; do
80         ls=$(ls -ld "$PRG")
81         link=$(expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null)
82         if expr "$link" : '^/' 2> /dev/null >/dev/null; then
83                 PRG="$link"
84         else
85                 PRG="$(dirname "$PRG")/$link"
86         fi
87 done
88 PROGDIR=$(dirname "$PRG")
89 stub_logger "[StubDir] $PROGDIR"
90
91
92
93 # set files and folders
94 ############################################
95
96 # the absolute path of the app package
97 cd "$PROGDIR"/../../ || exit 11
98 AppPackageFolder=$(pwd)
99
100 # the base path of the app package
101 cd .. || exit 12
102 AppPackageRoot=$(pwd)
103
104 # set Apple's Java folder
105 AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
106
107 # set Apple's Resources folder
108 AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
109
110 # set Oracle's Java folder
111 OracleJavaFolder="${AppPackageFolder}"/Contents/Java
112
113 # set Oracle's Resources folder
114 OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
115
116 # set path to Info.plist in bundle
117 InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
118
119 # set the default JVM Version to a null string
120 JVMVersion=""
121 JVMMaxVersion=""
122
123
124
125 # function 'plist_get()'
126 #
127 # read a specific Plist key with 'PlistBuddy' utility
128 #
129 # @param1  the Plist key with leading colon ':'
130 # @return  the value as String or Array
131 ################################################################################
132 plist_get(){
133         /usr/libexec/PlistBuddy -c "print $1" "${InfoPlistFile}" 2> /dev/null
134 }
135
136 # function 'plist_get_java()'
137 #
138 # read a specific Plist key with 'PlistBuddy' utility
139 # in the 'Java' or 'JavaX' dictionary (<dict>)
140 #
141 # @param1  the Plist :Java(X):Key with leading colon ':'
142 # @return  the value as String or Array
143 ################################################################################
144 plist_get_java(){
145         plist_get ${JavaKey:-":Java"}$1
146 }
147
148
149
150 # read Info.plist and extract JVM options
151 ############################################
152
153 # read the program name from CFBundleName
154 CFBundleName=$(plist_get ':CFBundleName')
155
156 # read the icon file name
157 CFBundleIconFile=$(plist_get ':CFBundleIconFile')
158
159
160 # check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
161 /usr/libexec/PlistBuddy -c "print :Java" "${InfoPlistFile}" > /dev/null 2>&1
162 exitcode=$?
163 JavaKey=":Java"
164
165 # if no :Java key is present, check Info.plist for universalJavaApplication style JavaX keys -> if key :JavaX is present, parse in apple mode
166 if [ $exitcode -ne 0 ]; then
167         /usr/libexec/PlistBuddy -c "print :JavaX" "${InfoPlistFile}" > /dev/null 2>&1
168         exitcode=$?
169         JavaKey=":JavaX"
170 fi
171
172
173 # read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)
174 if [ $exitcode -eq 0 ]; then
175         stub_logger "[PlistStyle] Apple"
176
177         # set Java and Resources folder
178         JavaFolder="${AppleJavaFolder}"
179         ResourcesFolder="${AppleResourcesFolder}"
180
181         APP_PACKAGE="${AppPackageFolder}"
182         JAVAROOT="${AppleJavaFolder}"
183         USER_HOME="$HOME"
184
185
186         # read the Java WorkingDirectory
187         JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
188         # set Working Directory based upon PList value
189         if [[ ! -z ${JVMWorkDir} ]]; then
190                 WorkingDirectory="${JVMWorkDir}"
191         else
192                 # AppPackageRoot is the standard WorkingDirectory when the script is started
193                 WorkingDirectory="${AppPackageRoot}"
194         fi
195         # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
196         WorkingDirectory=$(eval echo "${WorkingDirectory}")
197
198
199         # read the MainClass name
200         JVMMainClass="$(plist_get_java ':MainClass')"
201
202         # read the SplashFile name
203         JVMSplashFile=$(plist_get_java ':SplashFile')
204
205         # read the JVM Properties as an array and retain spaces
206         IFS=$'\t\n'
207         JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
208         unset IFS
209         # post processing of the array follows further below...
210
211         # read the ClassPath in either Array or String style
212         JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
213         if [[ $JVMClassPath_RAW == *Array* ]] ; then
214                 JVMClassPath=.$(plist_get_java ':ClassPath' | grep "    " | sed 's/^ */:/g' | tr -d '\n' | xargs)
215         else
216                 JVMClassPath=${JVMClassPath_RAW}
217         fi
218         # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
219         JVMClassPath=$(eval echo "${JVMClassPath}")
220
221         # read the JVM Options in either Array or String style
222         JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
223         if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
224                 JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep "    " | sed 's/^ */ /g' | tr -d '\n' | xargs)
225         else
226                 JVMDefaultOptions=${JVMDefaultOptions_RAW}
227         fi
228
229         # read StartOnMainThread and add as -XstartOnFirstThread
230         JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
231         if [ "${JVMStartOnMainThread}" == "true" ]; then
232                 JVMDefaultOptions+=" -XstartOnFirstThread"
233         fi
234
235         # read the JVM Arguments as an array and retain spaces
236         IFS=$'\t\n'
237         MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
238         unset IFS
239         # post processing of the array follows further below...
240
241         # read the Java version we want to find
242         JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
243         # post processing of the version string follows below...
244
245
246 # read 'Info.plist' file in Oracle style
247 else
248         stub_logger "[PlistStyle] Oracle"
249
250         # set Working Directory and Java and Resources folder
251         JavaFolder="${OracleJavaFolder}"
252         ResourcesFolder="${OracleResourcesFolder}"
253         WorkingDirectory="${OracleJavaFolder}"
254
255         APP_ROOT="${AppPackageFolder}"
256
257         # read the MainClass name
258         JVMMainClass="$(plist_get ':JVMMainClassName')"
259
260         # read the SplashFile name
261         JVMSplashFile=$(plist_get ':JVMSplashFile')
262
263         # read the JVM Options as an array and retain spaces
264         IFS=$'\t\n'
265         JVMOptions=($(plist_get ':JVMOptions' | grep "    " | sed 's/^ *//g'))
266         unset IFS
267         # post processing of the array follows further below...
268
269         # read the ClassPath in either Array or String style
270         JVMClassPath_RAW=$(plist_get ':JVMClassPath')
271         if [[ $JVMClassPath_RAW == *Array* ]] ; then
272                 JVMClassPath=.$(plist_get ':JVMClassPath' | grep "    " | sed 's/^ */:/g' | tr -d '\n' | xargs)
273                 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
274                 JVMClassPath=$(eval echo "${JVMClassPath}")
275
276         elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
277                 JVMClassPath=${JVMClassPath_RAW}
278                 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
279                 JVMClassPath=$(eval echo "${JVMClassPath}")
280
281         else
282                 #default: fallback to OracleJavaFolder
283                 JVMClassPath="${JavaFolder}/*"
284                 # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
285         fi
286
287         # read the JVM Default Options
288         JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
289
290         # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
291         IFS=$'\t\n'
292         MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/  */ /g')))
293         unset IFS
294         # post processing of the array follows further below...
295
296         # read the Java version we want to find
297         JVMVersion=$(plist_get ':JVMVersion' | xargs)
298         # post processing of the version string follows below...
299 fi
300
301
302
303 # JVMVersion: post processing and optional splitting
304 if [[ ${JVMVersion} == *";"* ]]; then
305         minMaxArray=(${JVMVersion//;/ })
306         JVMVersion=${minMaxArray[0]//+}
307         JVMMaxVersion=${minMaxArray[1]//+}
308 fi
309 stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
310 stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
311
312 # MainArgs: replace occurences of $APP_ROOT with its content
313 MainArgsArr=()
314 for i in "${MainArgs[@]}"
315 do
316         MainArgsArr+=("$(eval echo "$i")")
317 done
318
319 # JVMOptions: replace occurences of $APP_ROOT with its content
320 JVMOptionsArr=()
321 for i in "${JVMOptions[@]}"
322 do
323         JVMOptionsArr+=("$(eval echo "$i")")
324 done
325
326
327 # internationalized messages
328 ############################################
329
330 LANG=$(defaults read -g AppleLocale)
331 stub_logger "[Language] $LANG"
332
333 # French localization
334 if [[ $LANG == fr* ]] ; then
335         MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
336         MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
337         MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
338         MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
339         MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
340         MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
341         MSG_JAVA_VERSION_MAX="à %s"
342         MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
343         MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
344         MSG_LATER="Plus tard"
345         MSG_VISIT_JAVA_DOT_COM="Visiter java.com"
346
347 # German localization
348 elif [[ $LANG == de* ]] ; then
349         MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
350         MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
351         MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
352         MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
353         MSG_JAVA_VERSION_OR_LATER="oder neuer"
354         MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
355         MSG_JAVA_VERSION_MAX="bis %s"
356         MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
357         MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
358         MSG_LATER="Später"
359         MSG_VISIT_JAVA_DOT_COM="java.com öffnen"
360
361 # Simplifyed Chinese localization
362 elif [[ $LANG == zh* ]] ; then
363         MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
364         MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
365         MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
366         MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
367         MSG_JAVA_VERSION_OR_LATER="及以上版本"
368         MSG_JAVA_VERSION_LATEST="(最新版本)"
369         MSG_JAVA_VERSION_MAX="最高为 %s"
370         MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
371         MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
372         MSG_LATER="稍后"
373         MSG_VISIT_JAVA_DOT_COM="访问 java.com"
374
375 # English default localization
376 else
377         MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
378         MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
379         MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
380         MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
381         MSG_JAVA_VERSION_OR_LATER="or later"
382         MSG_JAVA_VERSION_LATEST="(latest update)"
383         MSG_JAVA_VERSION_MAX="up to %s"
384         MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
385         MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
386         MSG_LATER="Later"
387         MSG_VISIT_JAVA_DOT_COM="Visit java.com"
388 fi
389
390
391
392 # function 'get_java_version_from_cmd()'
393 #
394 # returns Java version string from 'java -version' command
395 # works for both old (1.8) and new (9) version schema
396 #
397 # @param1  path to a java JVM executable
398 # @return  the Java version number as displayed in 'java -version' command
399 ################################################################################
400 function get_java_version_from_cmd() {
401         # second sed command strips " and -ea from the version string
402         echo $("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')
403 }
404
405
406 # function 'extract_java_major_version()'
407 #
408 # extract Java major version from a version string
409 #
410 # @param1  a Java version number ('1.8.0_45') or requirement string ('1.8+')
411 # @return  the major version (e.g. '7', '8' or '9', etc.)
412 ################################################################################
413 function extract_java_major_version() {
414         echo $(echo "$1" | sed -E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
415 }
416
417
418 # function 'get_comparable_java_version()'
419 #
420 # return comparable version for a Java version number or requirement string
421 #
422 # @param1  a Java version number ('1.8.0_45') or requirement string ('1.8+')
423 # @return  an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
424 ################################################################################
425 function get_comparable_java_version() {
426         # cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
427         local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
428         # splitting at '.' into an array
429         local arr=( ${cleaned//./ } )
430         # echo a string with left padded version numbers
431         echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
432 }
433
434
435 # function 'is_valid_requirement_pattern()'
436 #
437 # check whether the Java requirement is a valid requirement pattern
438 #
439 # supported requirements are for example:
440 # - 1.6       requires Java 6 (any update)      [1.6, 1.6.0_45, 1.6.0_88]
441 # - 1.6*      requires Java 6 (any update)      [1.6, 1.6.0_45, 1.6.0_88]
442 # - 1.6+      requires Java 6 or higher         [1.6, 1.6.0_45, 1.8, 9, etc.]
443 # - 1.6.0     requires Java 6 (any update)      [1.6, 1.6.0_45, 1.6.0_88]
444 # - 1.6.0_45  requires Java 6u45                [1.6.0_45]
445 # - 1.6.0_45+ requires Java 6u45 or higher      [1.6.0_45, 1.6.0_88, 1.8, etc.]
446 # - 9         requires Java 9 (any update)      [9.0.*, 9.1, 9.3, etc.]
447 # - 9*        requires Java 9 (any update)      [9.0.*, 9.1, 9.3, etc.]
448 # - 9+        requires Java 9 or higher         [9.0, 9.1, 10, etc.]
449 # - 9.1       requires Java 9.1 (any update)    [9.1.*, 9.1.2, 9.1.13, etc.]
450 # - 9.1*      requires Java 9.1 (any update)    [9.1.*, 9.1.2, 9.1.13, etc.]
451 # - 9.1+      requires Java 9.1 or higher       [9.1, 9.2, 10, etc.]
452 # - 9.1.3     requires Java 9.1.3               [9.1.3]
453 # - 9.1.3*    requires Java 9.1.3 (any update)  [9.1.3]
454 # - 9.1.3+    requires Java 9.1.3 or higher     [9.1.3, 9.1.4, 9.2.*, 10, etc.]
455 # - 10-ea     requires Java 10 (early access release)
456 #
457 # unsupported requirement patterns are for example:
458 # - 1.2, 1.3, 1.9       Java 2, 3 are not supported
459 # - 1.9                 Java 9 introduced a new versioning scheme
460 # - 6u45                known versioning syntax, but unsupported
461 # - 9-ea*, 9-ea+        early access releases paired with */+
462 # - 9., 9.*, 9.+        version ending with a .
463 # - 9.1., 9.1.*, 9.1.+  version ending with a .
464 # - 9.3.5.6             4 part version number is unsupported
465 #
466 # @param1  a Java requirement string ('1.8+')
467 # @return  boolean exit code: 0 (is valid), 1 (is not valid)
468 ################################################################################
469 function is_valid_requirement_pattern() {
470         local java_req=$1
471         java8pattern='1\.[4-8](\.0)?(\.0_[0-9]+)?[*+]?'
472         java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
473         # test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
474         if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
475                 return 0
476         else
477                 return 1
478         fi
479 }
480
481
482
483 # determine which JVM to use
484 ############################################
485
486 # default Apple JRE plugin path (< 1.6)
487 apple_jre_plugin="/Library/Java/Home/bin/java"
488 apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
489 # default Oracle JRE plugin path (>= 1.7)
490 oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
491 oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
492
493
494 # first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
495 stub_logger '[JavaSearch] Checking for $JAVA_HOME ...'
496 if [ -n "$JAVA_HOME" ] ; then
497         stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
498
499         # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
500         # which allows for bundling a custom version of Java inside your app!
501         if [[ $JAVA_HOME == /* ]] ; then
502                 # if "$JAVA_HOME" starts with a Slash it's an absolute path
503                 JAVACMD="$JAVA_HOME/bin/java"
504         else
505                 # otherwise it's a relative path to "$AppPackageFolder"
506                 JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
507         fi
508         JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
509 else
510         stub_logger "[JavaSearch] ... didn't found JAVA_HOME"
511 fi
512
513
514 # check for any other or a specific Java version
515 # also if $JAVA_HOME exists but isn't executable
516 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
517         stub_logger "[JavaSearch] Checking for JavaVirtualMachines on the system ..."
518         # reset variables
519         JAVACMD=""
520         JAVACMD_version=""
521
522         # first check whether JVMVersion string is a valid requirement string
523         if [ ! -z "${JVMVersion}" ] && ! is_valid_requirement_pattern ${JVMVersion} ; then
524                 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMVersion}")
525                 # log exit cause
526                 stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
527                 # display error message with AppleScript
528                 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
529                 # exit with error
530                 exit 4
531         fi
532         # then check whether JVMMaxVersion string is a valid requirement string
533         if [ ! -z "${JVMMaxVersion}" ] && ! is_valid_requirement_pattern ${JVMMaxVersion} ; then
534                 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMMaxVersion}")
535                 # log exit cause
536                 stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
537                 # display error message with AppleScript
538                 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
539                 # exit with error
540                 exit 5
541         fi
542
543
544         # find installed JavaVirtualMachines (JDK + JRE)
545         allJVMs=()
546         # read JDK's from '/usr/libexec/java_home -V' command
547         while read -r line; do
548                 version=$(echo $line | awk -F $',' '{print $1;}')
549                 path=$(echo $line | awk -F $'" ' '{print $2;}')
550                 path+="/bin/java"
551                 allJVMs+=("$version:$path")
552         done < <(/usr/libexec/java_home -V 2>&1 | grep '^[[:space:]]')
553         # unset while loop variables
554         unset version path
555
556         # add Apple JRE if available
557         if [ -x "${apple_jre_plugin}" ] ; then
558                 allJVMs+=("$apple_jre_version:$apple_jre_plugin")
559         fi
560
561         # add Oracle JRE if available
562         if [ -x "${oracle_jre_plugin}" ] ; then
563                 allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
564         fi
565
566         # debug output
567         for i in "${allJVMs[@]}"
568         do
569                 stub_logger "[JavaSearch] ... found JVM: $i"
570         done
571
572
573         # determine JVMs matching the min/max version requirement
574         minC=$(get_comparable_java_version ${JVMVersion})
575         maxC=$(get_comparable_java_version ${JVMMaxVersion})
576         matchingJVMs=()
577
578         for i in "${allJVMs[@]}"
579         do
580                 # split JVM string at ':' delimiter to retain spaces in $path substring
581                 IFS=: arr=($i) ; unset IFS
582                 # [0] JVM version number
583                 ver=${arr[0]}
584                 # comparable JVM version number
585                 comp=$(get_comparable_java_version $ver)
586                 # [1] JVM path
587                 path="${arr[1]}"
588                 # construct string item for adding to the "matchingJVMs" array
589                 item="$comp:$ver:$path"
590
591                 # pre-requisite: current version number needs to be greater than min version number
592                 if [ "$comp" -ge "$minC" ] ; then
593
594                         # perform max version checks if max version requirement is present
595                         if [ ! -z ${JVMMaxVersion} ] ; then
596
597                                 # max version requirement ends with '*' modifier
598                                 if [[ ${JVMMaxVersion} == *\* ]] ; then
599
600                                         # use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
601                                         # and check whether the current version number starts with the max version wildcard string
602                                         if [[ ${ver} == ${JVMMaxVersion} ]]; then
603                                                 matchingJVMs+=("$item")
604
605                                         # or whether the current comparable version is lower than the comparable max version
606                                         elif [ "$comp" -le "$maxC" ] ; then
607                                                 matchingJVMs+=("$item")
608                                         fi
609
610                                 # max version requirement ends with '+' modifier -> always add this version if it's greater than $min
611                                 # because a max requirement with + modifier doesn't make sense
612                                 elif [[ ${JVMMaxVersion} == *+ ]] ; then
613                                         matchingJVMs+=("$item")
614
615                                 # matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
616                                 # -> then the max version string should be treated like with a '*' modifier at the end
617                                 #elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} + 999 )) ] ; then
618                                 #       matchingJVMs+=("$item")
619
620                                 # matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
621                                 # -> then the max version string should be treated like with a '*' modifier at the end
622                                 #elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
623                                 #       matchingJVMs+=("$item")
624
625                                 # matches standard requirements without modifier
626                                 elif [ "$comp" -le "$maxC" ]; then
627                                         matchingJVMs+=("$item")
628                                 fi
629
630                         # no max version requirement:
631
632                         # min version requirement ends with '+' modifier
633                         # -> always add the current version because it's greater than $min
634                         elif [[ ${JVMVersion} == *+ ]] ; then
635                                 matchingJVMs+=("$item")
636
637                         # min version requirement ends with '*' modifier
638                         # -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
639                         #    and check whether the current version number starts with the min version wildcard string
640                         elif [[ ${JVMVersion} == *\* ]] ; then
641                                 if [[ ${ver} == ${JVMVersion} ]] ; then
642                                         matchingJVMs+=("$item")
643                                 fi
644
645                         # compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
646                         # -> e.g. add 1.8.0_44 when the requirement is 1.8
647                         elif [[ ${ver} == ${JVMVersion}* ]] ; then
648                                         matchingJVMs+=("$item")
649                         fi
650                 fi
651         done
652         # unset for loop variables
653         unset arr ver comp path item
654
655         # debug output
656         for i in "${matchingJVMs[@]}"
657         do
658                 stub_logger "[JavaSearch] ... ... matches all requirements: $i"
659         done
660
661
662         # sort the matching JavaVirtualMachines by version number
663         # https://stackoverflow.com/a/11789688/1128689
664         IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
665         unset IFS
666
667
668         # get the highest matching JVM
669         for ((i = 0; i < ${#matchingJVMs[@]}; i++));
670         do
671                 # split JVM string at ':' delimiter to retain spaces in $path substring
672                 IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
673                 # [0] comparable JVM version number
674                 comp=${arr[0]}
675                 # [1] JVM version number
676                 ver=${arr[1]}
677                 # [2] JVM path
678                 path="${arr[2]}"
679
680                 # use current value as JAVACMD if it's executable
681                 if [ -x "$path" ] ; then
682                         JAVACMD="$path"
683                         JAVACMD_version=$comp
684                         break
685                 fi
686         done
687         # unset for loop variables
688         unset arr comp ver path
689 fi
690
691 # log the Java Command and the extracted version number
692 stub_logger "[JavaCommand] '$JAVACMD'"
693 stub_logger "[JavaVersion] $(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+ / $JAVACMD_version}"
694
695
696
697 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
698
699         # different error messages when a specific JVM was required
700         if [ ! -z "${JVMVersion}" ] ; then
701                 # display human readable java version (#28)
702                 java_version_hr=$(echo ${JVMVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+/ ${MSG_JAVA_VERSION_OR_LATER}/;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
703                 MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}").
704
705                 if [ ! -z "${JVMMaxVersion}" ] ; then
706                         java_version_hr=$(extract_java_major_version ${JVMVersion})
707                         java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
708                         MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}") $(printf "${MSG_JAVA_VERSION_MAX}" "${java_version_max_hr}")"
709                 fi
710
711                 # log exit cause
712                 stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
713
714                 # display error message with AppleScript
715                 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_NO_SUITABLE_JAVA_EXPANDED}\n${MSG_NO_SUITABLE_JAVA_CHECK}\" with title \"${CFBundleName}\"  buttons {\" OK \", \"${MSG_VISIT_JAVA_DOT_COM}\"} default button \"${MSG_VISIT_JAVA_DOT_COM}\" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)" \
716                                 -e "set response to button returned of the result" \
717                                 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"http://java.com\""
718                 # exit with error
719                 exit 3
720
721         else
722                 # log exit cause
723                 stub_logger "[EXIT 1] ${MSG_ERROR_LAUNCHING}"
724                 # display error message with AppleScript
725                 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_INSTALL_JAVA}\" with title \"${CFBundleName}\" buttons {\"${MSG_LATER}\", \"${MSG_VISIT_JAVA_DOT_COM}\"} default button \"${MSG_VISIT_JAVA_DOT_COM}\" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)" \
726                                         -e "set response to button returned of the result" \
727                                         -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"http://java.com\""
728                 # exit with error
729                 exit 1
730         fi
731 fi
732
733
734
735 # MainClass check
736 ############################################
737
738 if [ -z "${JVMMainClass}" ]; then
739         # log exit cause
740         stub_logger "[EXIT 2] ${MSG_MISSING_MAINCLASS}"
741         # display error message with AppleScript
742         osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_MISSING_MAINCLASS}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
743         # exit with error
744         exit 2
745 fi
746
747
748
749 # execute $JAVACMD and do some preparation
750 ############################################
751
752 # enable drag&drop to the dock icon
753 export CFProcessPath="$0"
754
755 # remove Apples ProcessSerialNumber from passthru arguments (#39)
756 if [[ "$*" == -psn* ]] ; then
757         ArgsPassthru=()
758 else
759         ArgsPassthru=("$@")
760 fi
761
762 # change to Working Directory based upon Apple/Oracle Plist info
763 cd "${WorkingDirectory}" || exit 13
764 stub_logger "[WorkingDirectory] ${WorkingDirectory}"
765
766 # execute Java and set
767 # - classpath
768 # - splash image
769 # - dock icon
770 # - app name
771 # - JVM options / properties (-D)
772 # - JVM default options (-X)
773 # - main class
774 # - main class arguments
775 # - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
776 stub_logger "[Exec] \"$JAVACMD\" -cp \"${JVMClassPath}\" -splash:\"${ResourcesFolder}/${JVMSplashFile}\" -Xdock:icon=\"${ResourcesFolder}/${CFBundleIconFile}\" -Xdock:name=\"${CFBundleName}\" ${JVMOptionsArr:+$(printf "'%s' " "${JVMOptionsArr[@]}") }${JVMDefaultOptions:+$JVMDefaultOptions }${JVMMainClass}${MainArgsArr:+ $(printf "'%s' " "${MainArgsArr[@]}")}${ArgsPassthru:+ $(printf "'%s' " "${ArgsPassthru[@]}")}"
777 exec "${JAVACMD}" \
778                 -Djava.library.path="${AppleJavaFolder}" \
779                 -cp "${JVMClassPath}" \
780                 -splash:"${ResourcesFolder}/${JVMSplashFile}" \
781                 -Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
782                 -Xdock:name="${CFBundleName}" \
783                 ${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
784                 ${JVMDefaultOptions:+$JVMDefaultOptions }\
785                 "${JVMMainClass}"\
786                 ${MainArgsArr:+ "${MainArgsArr[@]}"}\
787                 ${ArgsPassthru:+ "${ArgsPassthru[@]}"}