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