chiark / gitweb /
actually bomb out if the control panel does
[jarrg-owen.git] / jpctb
diff --git a/jpctb b/jpctb
index c50b43baaed84e476373a57e34db8b99fd39cce4..c4f2b6e6a241035ebc7f891765b2e5269676af50 100755 (executable)
--- a/jpctb
+++ b/jpctb
 #!/bin/bash -e
+
+# This is jpctb, a wrapper script for plumbing Ted Pearson's Java PCTB
+# client into a JVM on Linux.
+
+# This program is Free Software.  Copyright (C) 2009 Ian Jackson.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is fur-
+# nished to do so, subject to the following conditions:
+# 
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
+# NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+# XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
+# NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
 usage () { cat <<END
-usage: .../jpctb /path/to/dir/with/PCTB*.jar /path/to/yohoho/yohoho [...]
+usage: .../jpctb /path/to/yohoho/yohoho [...]
 END
 }
 
-jpctb_base="$0"
-jpctb_base="${jpctb_base%/*}"
+fail () {
+       echo >&2 "jpctb: $*"
+       exit 127
+}
+
+nojre () {
+       fail "couldn't find the right jre: $*
+ perhaps you should make ypp's java symlink (normally yohoho/java)
+ point to your jre, eg to /usr/lib/jvm/java-6-sun"
+}
+
+badusage () {
+       fail "bad usage: $*"
+}
+
+while [ $# -ge 1 ]; do
+       case "$1" in
+       -)              shift; break;;
+       --jpctb)
+                       shift
+                       srcjardir="$1"
+                       shift || badusage "--jpctb needs a value"
+                       ;;
+       -*)             badusage "unknown option \`$1'"
+                       ;;
+       *)
+                       break
+       esac
+done
+
+if [ $# -lt 1 ]; then usage; badusage "need path to yohoho"; exit 127; fi
+
+yohoho="$1"; shift
+
+#---------- find ourselves ----------
+
+jpctbdir="$0"
+jpctbdir="${jpctbdir%/*}"
+
+case "$jpctbdir" in
+/*)    ;;
+*)     jpctbdir="$PWD/$jpctbdir" ;;
+esac
+
+if [ x"$srcjardir" = x ]; then
+       srcjardir="$jpctbdir"
+fi
+
+#---------- find YPP client and the Java installation it uses ----------
+
+yppdir="${yohoho%/*}"
+
+# this replicates the java-searching logic from yohoho/yohoho:
+if [ -x "$yppdir/java/bin/java" ]; then
+       yppjava="$yppdir/java/bin/java"
+elif [ -x "$JAVA_HOME/bin/java" ]; then
+       yppjava="$JAVA_HOME/bin/java"
+else
+       set +e
+       yppjava=`type -p java 2>&1`
+       set -e
+fi
+
+cat <<END
+jpctb:
+   jpctb tree:  "$jpctbdir"
+   yohoho:       "$yohoho"
+   ypp dir:      "$yppdir"
+   ypp uses:     "$yppjava"
+END
+
+if ! [ -x "$yppjava" ]; then
+       nojre "bad java ypp"
+fi
+
+javadir="${yppjava%/bin/java}"
+if [ -x "$javadir/jre/bin/java" ]; then
+       echo "   java/ points to the jre, good"
+else
+       echo "   java/ has just the executable bin/java, trying readlink"
+       absjava=`readlink -f "$yppjava"`
+       echo "   abs. java:    \"$absjava\""
+       case "$absjava" in
+       */jre/bin/java)
+               echo "   found jre directory, good"
+               javadir="${absjava%/jre/bin/java}"
+               ;;
+       *)
+               nojre "java binary not in jre dir"
+               ;;
+       esac
+fi
+
+case "$javadir" in
+''|/|/usr)     nojre "javadir is $javadir (and even found $javadir/jre!)" ;;
+/*)            ;;
+*)             javadir="$PWD/$javadir" ;;
+esac
+
+jtmp="$jpctbdir/tmp"
+linkfarm="$jtmp/linkfarm"
+extdir="$jtmp/ext"
 
-if [ $# -lt 2 ]; then usage >&2; exit 127; fi
+export JPCTB_JRE="$javadir/jre"
+
+#---------- confirm for the user which paths we're using ----------
+
+cat <<END
+   java:         "$javadir"
+   JRE:          "$JPCTB_JRE"
+   PCTB jars:    "$srcjardir"
+   jpctb tmpdir: "$jtmp"
+END
+
+#---------- run the control panel ----------
+
+set +e
+"$javadir/bin/java" \
+ -Dcom.tedpearson.ypp.market.controlpanel.exitstatus=12 \
+ -jar "$srcjardir/PCTB-ControlPanel.jar"
+rc=$?
+set -e
+
+case $rc in
+0)     echo "launcher dialogue closed, quitting"; exit 0 ;;
+12)    ;;
+*)     echo >&2 "control panel failed with exit status $rc"; exit "$rc" ;;
+esac
+
+
+#---------- create the temporary are and link farm ----------
+
+rm -rf -- "$jtmp"
+mkdir -- "$jtmp" "$extdir" "$linkfarm"
+cp "$srcjardir"/PCTB*.jar "$extdir"
+
+cp -Rs "$javadir"/. "$linkfarm"/.
+
+#---------- edit the linkfarm to have our jvm wrapper ----------
+
+wrapper="$linkfarm"/jre/bin/java
+rm -- "$wrapper"
+
+export JPCTB_EXTDIR="$extdir"
+
+cat <<'END' >"$wrapper"
+#!/bin/bash
+       set -e$JPCTB_JWRAP_X
+
+       log () {
+               lh=`date +'%Y/%m/%d %H:%M:%S jpctb'`
+               printf >&2 "%s: %s |\f\n" "$lh" "$*"
+       }
+
+       log "invoked as $*"
+
+#echo >&2 "$djava-wrap 
+#exec 4>>/home/ian/u
+#date >&4
+#exec 4>&-
+
+       yppclass=com.threerings.yohoho.client.YoApp
+       atclass=com.tedpearson.ypp.market.MarketUploader
+
+       args=( "$@" )
+       nargs=${#args[*]}
+       lastarg="${args[$(( $nargs - 1 ))]}"
+
+       fail () { echo >&2 "jpctb-java: $*"; exit 127; }
+
+       if [ x"$lastarg" = x"$yppclass" ]; then
+
+               [ x"$JPCTB_EXTDIR" != x ] || fail 'JPCTB_EXTDIR not set'
+
+               set     -e$JPCTB_JWRAP_X -- \
+                       -Djavax.accessibility.assistive_technologies=$atclass \
+                       -Djava.ext.dirs="$JPCTB_EXTDIR:$JPCTB_JRE/lib/ext" \
+                       "$@"
+       fi
+
+       real="$JPCTB_JRE/bin/java"
+       log "running $real $*"
+       exec "$real" "$@"
+END
 
-export JWRAP_EXTDIR="$1"; shift
-export JWRAP_SCRIPT="$jpctb_base/jwrap-script-jpctb"
+chmod +x -- "$wrapper"
 
-if [ x"$LD_PRELOAD" != x ]; then LD_PRELOAD="$LD_PRELOAD:"; fi
-export LD_PRELOAD="${LD_PRELOAD}$jpctb_base/libjwrap.so.1.0"
+#---------- now run it ----------
 
-exec "$@"
+exec "$yohoho" -Djava.home="$linkfarm/jre"