chiark / gitweb /
remove obsolete "elevate" code
[jarrg-ian.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 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         --jpctb)
54                         shift
55                         srcjardir="$1"
56                         shift || badusage "--jpctb 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 jpctbdir="$0"
81 jpctbdir="${jpctbdir%/*}"
82
83 case "$jpctbdir" in
84 /*)     ;;
85 *)      jpctbdir="$PWD/$jpctbdir" ;;
86 esac
87
88 if [ x"$srcjardir" = x ]; then
89         srcjardir="$jpctbdir"
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 jpctb:
109    jpctb tree:   "$jpctbdir"
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 else
123         echo "   java/ has just the executable bin/java, trying readlink"
124         absjava=`readlink -f "$yppjava"`
125         echo "   abs. java:    \"$absjava\""
126         case "$absjava" in
127         */*/bin/java)
128                 javadir="${absjava%/bin/java}"
129                 jreleaf="${javadir##*/}"
130                 javadir="${javadir%/*}"
131                 echo "   jre leaf dir: \"$jreleaf\""
132                 case "$jreleaf" in
133                 jre)    echo "   found jre directory, good";;
134                 jre1.*) echo "   found versioned jre directory $jreleaf, ok";;
135                 *)      nojre "java binary not in jre dir ($jreleaf)";;
136                 esac
137                 ;;
138         *)
139                 nojre "real java binary not in ../bin/java dir";;
140         esac
141 fi
142
143 case "$javadir" in
144 ''|/|/usr)      nojre "javadir is $javadir (and even found $javadir/jre!)" ;;
145 /*)             ;;
146 *)              javadir="$PWD/$javadir" ;;
147 esac
148
149 jtmp="$jpctbdir/tmp"
150 linkfarm="$jtmp/linkfarm"
151 extdir="$jtmp/ext"
152
153 export JPCTB_JRE="$javadir/$jreleaf"
154
155 #---------- confirm for the user which paths we're using ----------
156
157 cat <<END
158    java:         "$javadir"
159    JRE:          "$JPCTB_JRE"
160    PCTB jars:    "$srcjardir"
161    jpctb tmpdir: "$jtmp"
162 END
163
164 #---------- run the control panel ----------
165
166 if $check_only; then echo "Check successful."; exit 0; fi
167
168 set +e
169 $setup_only_simulate "$javadir/bin/java" \
170  -Dcom.tedpearson.ypp.market.controlpanel.exitstatus=12 \
171  -jar "$srcjardir/PCTB-ControlPanel.jar"
172 rc=$?
173 set -e
174
175 if $setup_only; then rc=12; fi
176
177 case $rc in
178 0)      echo "launcher dialogue closed, quitting"; exit 0 ;;
179 12)     ;;
180 *)      echo >&2 "control panel failed with exit status $rc"; exit "$rc" ;;
181 esac
182
183
184 #---------- create the temporary are and link farm ----------
185
186 rm -rf -- "$jtmp"
187 mkdir -- "$jtmp" "$extdir" "$linkfarm"
188 cp "$srcjardir"/PCTB*.jar "$extdir"
189
190 cp -Rs "$javadir"/. "$linkfarm"/.
191
192 #---------- edit the linkfarm to have our jvm wrapper ----------
193
194 wrapper="$linkfarm"/$jreleaf/bin/java
195 rm -- "$wrapper"
196
197 export JPCTB_EXTDIR="$extdir"
198
199 cat <<'END' >"$wrapper"
200 #!/bin/bash
201         set -e$JPCTB_JWRAP_X
202
203         log () {
204                 lh=`date +'%Y/%m/%d %H:%M:%S jpctb'`
205                 printf >&2 "%s: %s |\f\n" "$lh" "$*"
206         }
207
208         log "invoked as $*"
209
210 #echo >&2 "$djava-wrap 
211 #exec 4>>/home/ian/u
212 #date >&4
213 #exec 4>&-
214
215         yppclass=com.threerings.yohoho.client.YoApp
216         atclass=com.tedpearson.ypp.market.MarketUploader
217
218         args=( "$@" )
219         nargs=${#args[*]}
220         lastarg="${args[$(( $nargs - 1 ))]}"
221
222         fail () { echo >&2 "jpctb-java: $*"; exit 127; }
223
224         if [ x"$lastarg" = x"$yppclass" ]; then
225
226                 [ x"$JPCTB_EXTDIR" != x ] || fail 'JPCTB_EXTDIR not set'
227
228                 set     -e$JPCTB_JWRAP_X -- \
229                         -Djavax.accessibility.assistive_technologies=$atclass \
230                         -Djava.ext.dirs="$JPCTB_EXTDIR:$JPCTB_JRE/lib/ext" \
231                         "$@"
232         fi
233
234         real="$JPCTB_JRE/bin/java"
235         log "running $real $*"
236         exec "$real" "$@"
237 END
238
239 chmod +x -- "$wrapper"
240
241 #---------- now run it ----------
242
243 exec $setup_only_simulate "$yohoho" -Djava.home="$linkfarm/$jreleaf"