#!/bin/ksh
PROGRAM_NAME=$0
CLASS_NAME=TxnClient
INTF_NAME=TxnServer
PKG0=examples
PKG1=txn

JDK_BASE=jdk1.2
JINI_BASE=files/jini1_0

# -----------------------------------------------------------------------------
# begin parseArgs
USAGE="usage: $PROGRAM_NAME [-usage] [-help] [-src src] [-jdk jdk] [-jini jini]"

ARG_LIST="-src -jdk -jini"

SRC_HOME=$HOME
JDK_HOME=$SRC_HOME/$JDK_BASE
JINI_HOME=$SRC_HOME/$JINI_BASE

for arg in $*
do
  if [ "$arg" = -usage ]
  then
    echo
    echo $USAGE
    echo
    exit 
  fi
done

for arg in $*
do
  if [ "$arg" = -help ]
  then
    echo 
    echo $USAGE
    echo "where options include:"
    echo 
    echo " -src  <base directory of example source, class and jar files>"
    echo "       Example: enter /export/home, then"
    echo "                source files assumed in /export/home/src,"
    echo "                class files assumed in /export/home/classes,"
    echo "                jar files assumed in /export/home/jars"
    echo "       (Default = $SRC_HOME)."
    echo " -jdk  <base directory of JDK bin>"
    echo "       Example: enter /java/jdk1.2, bin assumed in /java/jdk1.2/bin"
    echo "       (Default = $JDK_HOME)."
    echo " -jini <base directory of jini jar files>"
    echo "       Example: enter /jini/src, jars assumed in /jini/src/lib"
    echo "       (Default = $JINI_HOME)."
    echo 
    exit
  fi
done

while [ "$#" -gt 0 ]
do
  ARG=$1
  for f in $ARG_LIST
  do
    if [ "$ARG" = "$f" ]
    then
      shift
      break
    fi
  done

  if [ "$#" -eq 0 ]
  then
    break
  fi
  if [ "$ARG" = -src ]
  then
    SRC_HOME=$1
  fi
  if [ "$ARG" = -jdk ]
  then
    JDK_HOME=$1
  fi
  if [ "$ARG" = -jini ]
  then
    JINI_HOME=$1
  fi

  shift
done
#end parseArgs
# -----------------------------------------------------------------------------
echo --- $PROGRAM_NAME ---
# -----------------------------------------------------------------------------
CLASSPATH=
export CLASSPATH
if [ "$JDK_HOME" != "" ]
then
  PATH="$JDK_HOME"/bin:$PATH
  export PATH
fi
echo 
java -version
echo
# -----------------------------------------------------------------------------
# Set variables that are dependent on the arguments input to this script
SRC_DIR=$SRC_HOME/src
CLASS_DIR=$SRC_HOME/classes
PKG_DIR=$PKG0/$PKG1
JAR_DIR=$SRC_HOME/jars

####################################################################
# Compile *.java files
####################################################################
echo
echo ========= Compile .java source files 

# Compile remote interfaces
echo Compiling $INTF_NAME.java ...
javac -classpath $JINI_HOME/lib/jini-core.jar:$JINI_HOME/lib/jini-ext.jar:$CLASS_DIR -d $CLASS_DIR $SRC_DIR/$PKG0/$PKG1/$INTF_NAME.java

# Compile the example program that will register the test service
echo Compiling $CLASS_NAME.java ...
javac -classpath $JINI_HOME/lib/jini-core.jar:$JINI_HOME/lib/jini-ext.jar:$JINI_HOME/lib/sun-util.jar:$CLASS_DIR -d $CLASS_DIR $SRC_DIR/$PKG0/$PKG1/$CLASS_NAME.java

####################################################################
# Get ready to build jar files
####################################################################

# Because security restrictions do not allow the use of
# absolute paths, relative paths must be used to point to
# Class-Path download extension files in a manifest. Because
# of this fact, any Jini JAR files needed by this program
# are copied to this program's JAR directory
cp -f $JINI_HOME/lib/jini-core.jar $SRC_HOME/jars
cp -f $JINI_HOME/lib/jini-ext.jar $SRC_HOME/jars
cp -f $JINI_HOME/lib/sun-util.jar $SRC_HOME/jars

####################################################################
# Build the executable jar file 
####################################################################
echo
echo ========= Build the executable jar file  

# (1) Create the manifest for the executable jar file SANG
echo Building Executable JAR Manifest ...
echo Manifest-Version: 1.0>$SRC_DIR/$PKG_DIR/$CLASS_NAME.mf
echo Main-Class: $PKG0.$PKG1.$CLASS_NAME>>$SRC_DIR/$PKG_DIR/$CLASS_NAME.mf
echo Class-Path: jini-core.jar jini-ext.jar sun-util.jar TxnServer.jar>>$SRC_DIR/$PKG_DIR/$CLASS_NAME.mf

# (2) Set the variables needed to create the executable jar file
JAR_CLASS_0="$PKG_DIR/"$CLASS_NAME".class"
JAR_CLASS_1="$PKG_DIR/"$CLASS_NAME"\$LookupDiscoveryListener.class"
JAR_CLASS_2="$PKG_DIR/"$CLASS_NAME"\$LookupInfo.class"
JAR_CLASS_3="$PKG_DIR/"$CLASS_NAME"\$StoreLookupInfoThread.class" 

# (3) Set the variables for remote interfaces used
JAR_CLASS_4="$PKG_DIR/"$INTF_NAME".class"

# (4) Create the executable jar file
echo Building Executable JAR File ...
jar cfm $JAR_DIR/$CLASS_NAME.jar $SRC_DIR/$PKG_DIR/$CLASS_NAME.mf -C $CLASS_DIR $JAR_CLASS_0 
jar uvf $JAR_DIR/$CLASS_NAME.jar -C $CLASS_DIR $JAR_CLASS_1
jar uvf $JAR_DIR/$CLASS_NAME.jar -C $CLASS_DIR $JAR_CLASS_2
jar uvf $JAR_DIR/$CLASS_NAME.jar -C $CLASS_DIR $JAR_CLASS_3
jar uvf $JAR_DIR/$CLASS_NAME.jar -C $CLASS_DIR $JAR_CLASS_4

# -----------------------------------------------------------------------------
echo --- end $PROGRAM_NAME ---