chiark / gitweb /
UI fixes: jpctb does not run ypp if user closes the control panel; remove option...
[jarrg-owen.git] / jpctb
1 #!/bin/bash -e
2
3 # This is jpctb, a wrapper script for plumbing Ted Pearson's Java PCTB
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: .../jpctb /path/to/yohoho/yohoho [...]
28 END
29 }
30
31 fail () {
32         echo >&2 "jpctb: $*"
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 while [ $# -ge 1 ]; do
47         case "$1" in
48         -)              shift; break;;
49         --jpctb)
50                         shift
51                         srcjardir="$1"
52                         shift || badusage "--jpctb needs a value"
53                         ;;
54         -*)             badusage "unknown option \`$1'"
55                         ;;
56         *)
57                         break
58         esac
59 done
60
61 if [ $# -lt 1 ]; then usage; badusage "need path to yohoho"; exit 127; fi
62
63 yohoho="$1"; shift
64
65 #---------- find ourselves ----------
66
67 jpctbdir="$0"
68 jpctbdir="${jpctbdir%/*}"
69
70 case "$jpctbdir" in
71 /*)     ;;
72 *)      jpctbdir="$PWD/$jpctbdir" ;;
73 esac
74
75 if [ x"$srcjardir" = x ]; then
76         srcjardir="$jpctbdir"
77 fi
78
79 #---------- find YPP client and the Java installation it uses ----------
80
81 yppdir="${yohoho%/*}"
82
83 # this replicates the java-searching logic from yohoho/yohoho:
84 if [ -x "$yppdir/java/bin/java" ]; then
85         yppjava="$yppdir/java/bin/java"
86 elif [ -x "$JAVA_HOME/bin/java" ]; then
87         yppjava="$JAVA_HOME/bin/java"
88 else
89         set +e
90         yppjava=`type -p java 2>&1`
91         set -e
92 fi
93
94 cat <<END
95 jpctb:
96    jpctb tree:   "$jpctbdir"
97    yohoho:       "$yohoho"
98    ypp dir:      "$yppdir"
99    ypp uses:     "$yppjava"
100 END
101
102 if ! [ -x "$yppjava" ]; then
103         nojre "bad java ypp"
104 fi
105
106 javadir="${yppjava%/bin/java}"
107 if [ -x "$javadir/jre/bin/java" ]; then
108         echo "   java/ points to the jre, good"
109 else
110         echo "   java/ has just the executable bin/java, trying readlink"
111         absjava=`readlink -f "$yppjava"`
112         echo "   abs. java:    \"$absjava\""
113         case "$absjava" in
114         */jre/bin/java)
115                 echo "   found jre directory, good"
116                 javadir="${absjava%/jre/bin/java}"
117                 ;;
118         *)
119                 nojre "java binary not in jre dir"
120                 ;;
121         esac
122 fi
123
124 case "$javadir" in
125 ''|/|/usr)      nojre "javadir is $javadir (and even found $javadir/jre!)" ;;
126 /*)             ;;
127 *)              javadir="$PWD/$javadir" ;;
128 esac
129
130 jtmp="$jpctbdir/tmp"
131 linkfarm="$jtmp/linkfarm"
132 extdir="$jtmp/ext"
133
134 export JPCTB_JRE="$javadir/jre"
135
136 #---------- confirm for the user which paths we're using ----------
137
138 cat <<END
139    java:         "$javadir"
140    JRE:          "$JPCTB_JRE"
141    PCTB jars:    "$srcjardir"
142    jpctb tmpdir: "$jtmp"
143 END
144
145 #---------- run the control panel ----------
146
147 set +e
148 "$javadir/bin/java" \
149  -Dcom.tedpearson.ypp.market.controlpanel.exitstatus=12 \
150  -jar "$srcjardir/PCTB-ControlPanel.jar"
151 rc=$?
152 set -e
153
154 case $rc in
155 0)      echo "launcher dialogue closed, quitting"; exit 0 ;;
156 12)     ;;
157 *)      echo >&2 "control panel failed with exit status $rc" ;;
158 esac
159
160
161 #---------- create the temporary are and link farm ----------
162
163 rm -rf -- "$jtmp"
164 mkdir -- "$jtmp" "$extdir" "$linkfarm"
165 cp "$srcjardir"/PCTB*.jar "$extdir"
166
167 cp -Rs "$javadir"/. "$linkfarm"/.
168
169 #---------- edit the linkfarm to have our jvm wrapper ----------
170
171 wrapper="$linkfarm"/jre/bin/java
172 rm -- "$wrapper"
173
174 export JPCTB_EXTDIR="$extdir"
175
176 cat <<'END' >"$wrapper"
177 #!/bin/bash
178         set -e$JPCTB_JWRAP_X
179
180         log () {
181                 lh=`date +'%Y/%m/%d %H:%M:%S jpctb'`
182                 printf >&2 "%s: %s |\f\n" "$lh" "$*"
183         }
184
185         log "invoked as $*"
186
187 #echo >&2 "$djava-wrap 
188 #exec 4>>/home/ian/u
189 #date >&4
190 #exec 4>&-
191
192         yppclass=com.threerings.yohoho.client.YoApp
193         atclass=com.tedpearson.ypp.market.MarketUploader
194
195         args=( "$@" )
196         nargs=${#args[*]}
197         lastarg="${args[$(( $nargs - 1 ))]}"
198
199         fail () { echo >&2 "jpctb-java: $*"; exit 127; }
200
201         if [ x"$lastarg" = x"$yppclass" ]; then
202
203                 [ x"$JPCTB_EXTDIR" != x ] || fail 'JPCTB_EXTDIR not set'
204
205                 set     -e$JPCTB_JWRAP_X -- \
206                         -Djavax.accessibility.assistive_technologies=$atclass \
207                         -Djava.ext.dirs="$JPCTB_EXTDIR:$JPCTB_JRE/lib/ext" \
208                         "$@"
209         fi
210
211         real="$JPCTB_JRE/bin/java"
212         log "running $real $*"
213         exec "$real" "$@"
214 END
215
216 chmod +x -- "$wrapper"
217
218 #---------- now run it ----------
219
220 exec "$yohoho" -Djava.home="$linkfarm/jre"