annotate package-files/osx/JavaApplicationStub @ 27:404eb7e57fe6

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