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