61
|
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 /usr/libexec/PlistBuddy -c "print $1" Contents/Info.plist 2> /dev/null
|
|
101 }
|
|
102 function must_get {
|
|
103 plist_get "$@" || die "Info.plist key '$1' not found."
|
|
104 }
|
|
105
|
|
106 # Load some parameters from Contents/Info.plist
|
|
107 estat=0
|
|
108 CFBundleName=$(must_get ':CFBundleName')
|
|
109 CFBundleIconFile=$(must_get ':CFBundleIconFile')
|
|
110 JVMMainClassName=$(must_get ':JVMMainClassName')
|
|
111 JVMClassPath=$(must_get ':JVMClassPath')
|
|
112 JVMVersion=$(must_get ':JVMVersion')
|
|
113
|
|
114 # Sanity check our JVM version
|
|
115 if [[ "$JVMVersion" == *+ ]]
|
|
116 then
|
|
117 op='>'
|
|
118 JVMVersion="${JVMVersion%+}"
|
|
119 else
|
|
120 op='='
|
|
121 fi
|
|
122 version=$("$java" -version 2>&1 | awk '/version/{print $3}' | sed -E -e 's/"//g' -e 's/-ea//g')
|
|
123 if [ ! "$version" "$op" "$JVMVersion" ]
|
|
124 then
|
|
125 die "'$java' too old"
|
|
126 fi
|
|
127
|
|
128 # Load the JVMOptions array (if found)
|
|
129 JVMOptions=()
|
|
130 let i=0
|
|
131 while true
|
|
132 do
|
|
133 j="$(plist_get :JVMOptions:$i)"
|
|
134 if [ -z "$j" ]
|
|
135 then
|
|
136 break
|
|
137 fi
|
|
138 JVMOptions+=("$j")
|
|
139 let i+=1
|
|
140 done
|
|
141
|
|
142 # Fatal error if we cannot change to HOME or HOME not defined
|
|
143 approot="$(pwd)"
|
|
144 if [ -z "$HOME" ]
|
|
145 then
|
|
146 die "HOME not defined!"
|
|
147 fi
|
|
148 cd "$HOME" || die "Unable to cd '$HOME'"
|
|
149
|
|
150 # And off we go!
|
|
151 jcmd=( "$JAVA_HOME/bin/java" -cp "$approot/$JVMClassPath"
|
|
152 -Xdock:icon="$approot/Contents/Resources/$CFBundleIconFile"
|
|
153 -Xdock:name="$CFBundleName" "${JVMOptions[@]}" "$JVMMainClassName" )
|
|
154
|
|
155 stub_logger "Executing: ${jcmd[*]}"
|
|
156 exec "${jcmd[@]}"
|
|
157
|
|
158 # This shouldn't happen...
|
|
159 die "Could not launch Java."
|