comparison package-files/osx/JavaApplicationStub @ 39:89d7f4d91f67

Got a working, non-bloated Apple Mac bundle!
author David Barts <n5jrn@me.com>
date Fri, 01 May 2020 23:06:04 -0700
parents
children a65a8f5a9647
comparison
equal deleted inserted replaced
38:d794ef80f9b0 39:89d7f4d91f67
1 #!/bin/bash
2
3 # This is based on Tobias Fischer's universalJavaApplicationStub:
4 # https://github.com/tofi86/universalJavaApplicationStub
5 #
6 # Therefore this part of JpegWasher is covered by the same MIT license
7 # that his code is:
8 #
9 # The MIT License (MIT)
10 #
11 # Copyright (c) 2014-2020 Tobias Fischer
12 #
13 # Permission is hereby granted, free of charge, to any person obtaining a copy
14 # of this software and associated documentation files (the "Software"), to deal
15 # in the Software without restriction, including without limitation the rights
16 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 # copies of the Software, and to permit persons to whom the Software is
18 # furnished to do so, subject to the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be included in all
21 # copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 # SOFTWARE.
30
31 # Establish our application names
32 appname="@app.name@"
33 appdom="@app.domain@"
34
35 # Make a place for our logs to go
36 appsupp="$HOME/Library/Application Support/$appdom"
37 logfile="$appsupp/launcher.log"
38
39 function die {
40 osascript -e "tell application \"System Events\" to display dialog \"${1}\" with title \"Fatal Error\" buttons {\" OK \"} default button 1"
41 exit ${2:-1}
42 }
43
44 # Initialize files and dirs, bail if we can't.
45 if [ ! -d "$appsupp" ]
46 then
47 if ! mkdir -p "$appsupp"
48 then
49 die "Unable to create folder '$appsupp'" 2
50 fi
51 fi
52 > "$logfile"
53 if [ $? -ne 0 ]
54 then
55 die "Unable to create file '$logfile'" 2
56 fi
57
58 function stub_logger {
59 echo "$(date +%c) - $1" >> "$logfile"
60 }
61
62 # Was originally just for debugging, but I think this could be useful
63 # for later troubleshooting "in the wild."
64 stub_logger "execution begins"
65 {
66 echo "****** BEGIN ARGUMENT AND ENVIRONMENT DUMP *******"
67 echo "Arguments:" "$0" "$@"
68 echo "Directory:" $(pwd)
69 env
70 echo "******* END ARGUMENT AND ENVIRONMENT DUMP ********"
71 } >> "$logfile"
72
73 # Change to the application root directory, two up from where this script
74 # (which should contain an absolute path) is.
75 mydir="${0%/*}"
76 cd "$mydir/../.." || die "Unable to cd '$mydir/../..'"
77
78 # Determine where Java is, bail if we can't.
79 if [ -n "$JAVA_HOME" ]
80 then
81 if [ ! -d "$JAVA_HOME" ]
82 then
83 die "'$JAVA_HOME' does not exist"
84 fi
85 else
86 export JAVA_HOME="$(/usr/libexec/java_home)"
87 if [ $? -ne 0 -o ! -d "$JAVA_HOME" ]
88 then
89 die "Unable to locate Java."
90 fi
91 fi
92
93 java="$JAVA_HOME/bin/java"
94 if [ ! -x "$java" ]
95 then
96 die "'$java' not found or not executable"
97 fi
98
99 # function 'plist_get()'
100 #
101 # read a specific Plist key with 'PlistBuddy' utility
102 #
103 # @param1 the Plist key with leading colon ':'
104 # @return the value as String or Array
105 ################################################################################
106 function plist_get {
107 /usr/libexec/PlistBuddy -c "print $1" Contents/Info.plist 2> /dev/null
108 }
109 function must_get {
110 plist_get "$@" || die "Info.plist key '$1' not found."
111 }
112
113 # Load some parameters from Contents/Info.plist
114 estat=0
115 CFBundleName=$(must_get ':CFBundleName')
116 CFBundleIconFile=$(must_get ':CFBundleIconFile')
117 JVMMainClassName=$(must_get ':JVMMainClassName')
118 JVMClassPath=$(must_get ':JVMClassPath')
119 JVMVersion=$(must_get ':JVMVersion')
120
121 # Sanity check our JVM version
122 if [[ "$JVMVersion" == *+ ]]
123 then
124 op='>'
125 JVMVersion="${JVMVersion%+}"
126 else
127 op='='
128 fi
129 version=$("$java" -version 2>&1 | awk '/version/{print $3}' | sed -E -e 's/"//g' -e 's/-ea//g')
130 if [ ! "$version" "$op" "$JVMVersion" ]
131 then
132 die "'$java' too old"
133 fi
134
135 # Load the JVMOptions array (if found)
136 JVMOptions=()
137 let i=0
138 while true
139 do
140 j="$(plist_get :JVMOptions:$i)"
141 if [ -z "$j" ]
142 then
143 break
144 fi
145 JVMOptions+=("$j")
146 let i+=1
147 done
148
149 # Fatal error if we cannot change to HOME or HOME not defined
150 approot="$(pwd)"
151 if [ -z "$HOME" ]
152 then
153 die "HOME not defined!"
154 fi
155 cd "$HOME" || die "Unable to cd '$HOME'"
156
157 # And off we go!
158 jcmd=( "$JAVA_HOME/bin/java" -cp "$approot/$JVMClassPath"
159 -Xdock:icon="$approot/Contents/Resources/$CFBundleIconFile"
160 -Xdock:name="$CFBundleName" "${JVMOptions[@]}" "$JVMMainClassName" )
161
162 stub_logger "Executing: ${jcmd[@]}"
163 exec "${jcmd[@]}"
164
165 # This shouldn't happen...
166 die "Could not launch Java."