1#!/bin/sh
2
3# This script runs Processing, using the JDK in the Processing
4# installation directory if possible.
5
6# If no JDK was installed with Processing then the script tries to use
7# the preferred Java version of the machine, i.e. what is executed
8# by the "java" console command. This must be a Sun JDK (for details, see
9# http://processing.org/reference/environment/platforms.html#java).
10
11# In order to run Processing with an already installed JDK that is *not*
12# the preferred Java version of the machine, create a symlink named "java"
13# in the Processing installation directory that points to the JDK home
14# directory.
15
16# Thanks to Ferdinand Kasper for this build script. [fry]
17
18
19# JARs required from JDK (anywhere in/below the JDK home directory)
20JDKLIBS="rt.jar tools.jar"
21
22# Set this to non-zero for logging
23LOGGING=0
24
25# Logs name and value of a variable to stdout if LOGGING is non-zero.
26# Expects the variable name as parameter $1.
27log() {
28  if [ $LOGGING -ne 0 ]; then
29    eval echo $1=\$$1
30  fi
31}
32
33
34# Locates JDKLIBS in a directory and its subdirectories and saves their
35# absolute paths as list to JDKCP. Expects the directory as parameter $1.
36# Sets SUCCESS to 1 if all libraries were found, to 0 otherwise.
37make_jdkcp() {
38  # Back out of JRE directory if apparently located inside a JDK
39  if [ -f "$1/../bin/java" ]; then
40    DIR="$1/.."
41  else
42    DIR="$1"
43  fi
44  log DIR
45
46  JDKCP=
47  SUCCESS=1
48
49  # Locate JDKLIBS
50  for L in $JDKLIBS; do
51    # Locate only the first library with a matching name
52    LIB=`find "$DIR" -name $L 2>/dev/null | head -n 1`
53    log L
54    log LIB
55
56    # Library found?
57    if [ -n "$LIB" ]; then
58      JDKCP="$JDKCP"${JDKCP:+:}"$LIB"
59    else
60      SUCCESS=0
61    fi
62  done
63
64  log JDKCP
65}
66
67
68# Get absolute path of directory where this script is located
69APPDIR=`readlink -f "$0"`
70APPDIR=`dirname "$APPDIR"`
71log APPDIR
72
73# Try using a local JDK from the same directory as this script
74JDKDIR=`readlink -f "$APPDIR/java"`
75make_jdkcp "$JDKDIR"
76log SUCCESS
77
78# Local JDK found?
79if [ $SUCCESS -ne 1 ]; then
80  # No, try using the preferred system JRE/JDK (if any)
81  JDKDIR=`which java` && JDKDIR=`readlink -e "$JDKDIR"` && JDKDIR=`dirname "$JDKDIR"`/..
82  make_jdkcp "$JDKDIR"
83  log SUCCESS
84fi
85
86# Add all required JARs to CLASSPATH
87CLASSPATH="$CLASSPATH"${CLASSPATH:+:}"$JDKCP"
88for LIB in "$APPDIR"/lib/*.jar; do
89  CLASSPATH="$CLASSPATH"${CLASSPATH:+:}"$LIB"
90done
91export CLASSPATH
92log CLASSPATH
93
94# Make all JDK binaries available in PATH
95export PATH="$JDKDIR/bin":"$PATH"
96log PATH
97
98# Start Processing in the same directory as this script
99cd "$APPDIR"
100if [ "$1" ]; then
101  SKETCH=`readlink -f $1`
102else
103  SKETCH=
104fi
105java processing.app.Base "$SKETCH" &
106