chiark / gitweb /
Merge branch 'tidy' into 'main'
[jarrg-ian.git] / jarrg
1 #!/bin/bash -e
2
3 # This is jarrg, a wrapper script for plumbing the Jarrg
4 # client into a JVM on Linux.
5
6 # This program is Free Software.  Copyright (C) 2009 Ian Jackson.
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to deal
10 # in the Software without restriction, including without limitation the rights
11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the Software is fur-
13 # nished to do so, subject to the following conditions:
14
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
17
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
20 # NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21 # XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
23 # NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
26 usage () { cat <<END
27 usage: .../jarrg /path/to/yohoho/yohoho [...]
28 END
29 }
30
31 fail () {
32         echo >&2 "jarrg: $*"
33         exit 127
34 }
35
36 nojre () {
37         fail "couldn't find the right jre: $*
38  perhaps you should make ypp's java symlink (normally yohoho/java)
39  point to your jre, eg to /usr/lib/jvm/java-6-sun"
40 }
41
42 badusage () {
43         fail "bad usage: $*"
44 }
45
46 check_only=false
47 setup_only=false
48 setup_only_simulate=''
49
50 while [ $# -ge 1 ]; do
51         case "$1" in
52         -)              shift; break;;
53         --jarrg)
54                         shift
55                         srcjardir="$1"
56                         shift || badusage "--jarrg needs a value"
57                         ;;
58         --check-only)
59                         check_only=true
60                         shift
61                         ;;
62         --setup-only)
63                         setup_only=true
64                         setup_only_simulate=echo
65                         shift
66                         ;;
67         -*)             badusage "unknown option \`$1'"
68                         ;;
69         *)
70                         break
71         esac
72 done
73
74 if [ $# -lt 1 ]; then usage; badusage "need path to yohoho"; exit 127; fi
75
76 yohoho="$1"; shift
77
78 #---------- find ourselves ----------
79
80 jarrgdir="$0"
81 jarrgdir="${jarrgdir%/*}"
82
83 case "$jarrgdir" in
84 /*)     ;;
85 *)      jarrgdir="$PWD/$jarrgdir" ;;
86 esac
87
88 if [ x"$srcjardir" = x ]; then
89         srcjardir="$jarrgdir"
90 fi
91
92 #---------- find YPP client and the Java installation it uses ----------
93
94 yppdir="${yohoho%/*}"
95
96 # this replicates the java-searching logic from yohoho/yohoho:
97 if [ -x "$yppdir/java/bin/java" ]; then
98         yppjava="$yppdir/java/bin/java"
99 elif [ -x "$JAVA_HOME/bin/java" ]; then
100         yppjava="$JAVA_HOME/bin/java"
101 else
102         set +e
103         yppjava=`type -p java 2>&1`
104         set -e
105 fi
106
107 cat <<END
108 jarrg:
109    jarrg tree:   "$jarrgdir"
110    yohoho:       "$yohoho"
111    ypp dir:      "$yppdir"
112    ypp uses:     "$yppjava"
113 END
114
115 if ! [ -x "$yppjava" ]; then
116         nojre "bad java ypp"
117 fi
118
119 javadir="${yppjava%/bin/java}"
120 if [ -x "$javadir/jre/bin/java" ]; then
121         echo "   java/ points to the jre, good"
122         jreleaf=jre
123 else
124         echo "   java/ has just the executable bin/java, trying readlink"
125         absjava=`readlink -f "$yppjava"`
126         echo "   abs. java:    \"$absjava\""
127         case "$absjava" in
128         */*/bin/java)
129                 javadir="${absjava%/bin/java}"
130                 jreleaf="${javadir##*/}"
131                 javadir="${javadir%/*}"
132                 echo "   jre leaf dir: \"$jreleaf\""
133                 case "$jreleaf" in
134                 jre)    echo "   found jre directory, good";;
135                 java-*) echo "   found java directory, good";;
136                 jre1.*) echo "   found versioned jre directory $jreleaf, ok";;
137                 *)      nojre "java binary not in jre dir ($jreleaf)";;
138                 esac
139                 ;;
140         *)
141                 nojre "real java binary not in ../bin/java dir";;
142         esac
143 fi
144
145 case "$javadir" in
146 ''|/|/usr)      nojre "javadir is $javadir (and even found $javadir/jre!)" ;;
147 /*)             ;;
148 *)              javadir="$PWD/$javadir" ;;
149 esac
150
151 jtmp="$jarrgdir/tmp"
152 linkfarm="$jtmp/linkfarm"
153 extdir="$jtmp/ext"
154
155 export JARRG_JRE="$javadir/$jreleaf"
156 realjava="$JARRG_JRE/bin/java"
157
158 #---------- confirm for the user which paths we're using ----------
159
160 cat <<END
161    java dir.:    "$javadir"
162    jre:          "$JARRG_JRE"
163    primary java: "$realjava"
164    jarrg jars:   "$srcjardir"
165    jarrg tmpdir: "$jtmp"
166 END
167
168 #---------- run the control panel ----------
169
170 if $check_only; then echo "Check successful."; exit 0; fi
171
172 set +e
173 $setup_only_simulate "$realjava" \
174  -Dnet.chiark.yarrg.controlpanel.exitstatus=12 \
175  $JARRG_JAVA_OPTS \
176  -jar "$srcjardir/Jarrg-ControlPanel.jar"
177 rc=$?
178 set -e
179
180 if $setup_only; then rc=12; fi
181
182 case $rc in
183 0)      echo "launcher dialogue closed, quitting"; exit 0 ;;
184 12)     ;;
185 *)      echo >&2 "control panel failed with exit status $rc"; exit "$rc" ;;
186 esac
187
188
189 #---------- create the temporary are and link farm ----------
190
191 rm -rf -- "$jtmp"
192 mkdir -- "$jtmp" "$extdir" "$linkfarm"
193 cp "$srcjardir"/Jarrg*.jar "$extdir"
194
195 cp -Rs "$javadir"/. "$linkfarm"/.
196
197 #---------- edit the linkfarm to have our jvm wrapper ----------
198
199 wrapper="$linkfarm"/$jreleaf/bin/java
200 rm -- "$wrapper"
201
202 export JARRG_EXTDIR="$extdir"
203
204 cat <<'END' >"$wrapper"
205 #!/bin/bash
206         set -e$JARRG_JWRAP_X
207
208         log () {
209                 lh=`date +'%Y/%m/%d %H:%M:%S jarrg'`
210                 printf >&2 "%s: %s |\f\n" "$lh" "$*"
211         }
212
213         log "invoked as $*"
214
215 #echo >&2 "$djava-wrap 
216 #exec 4>>/home/ian/u
217 #date >&4
218 #exec 4>&-
219
220         yppclass=com.threerings.yohoho.client.YoApp
221         atclass=net.chiark.yarrg.MarketUploader
222         # for testing atclass=net.chiark.yarrg.MarketUploader,Monkey
223
224         args=( "$@" )
225         nargs=${#args[*]}
226         lastarg="${args[$(( $nargs - 1 ))]}"
227
228         fail () { echo >&2 "jarrg-java: $*"; exit 127; }
229
230         if [ x"$lastarg" = x"$yppclass" ]; then
231
232                 [ x"$JARRG_EXTDIR" != x ] || fail 'JARRG_EXTDIR not set'
233
234                 set     -e$JARRG_JWRAP_X -- \
235                         -Djavax.accessibility.assistive_technologies=$atclass \
236                         -Djava.ext.dirs="$JARRG_EXTDIR:$JARRG_JRE/lib/ext" \
237                         $JARRG_JAVA_OPTS \
238                         "$@"
239         fi
240
241         real="$JARRG_JRE/bin/java"
242         log "running $real $*"
243         exec "$real" $JARRG_JAVA_OPTS "$@"
244 END
245
246 chmod +x -- "$wrapper"
247
248 #---------- now run it ----------
249
250 exec $setup_only_simulate "$yohoho" -Djava.home="$linkfarm/$jreleaf"