#!/bin/ksh
################################################################
# Set variables
################################################################

PROGRAM_NAME=$0
INTF_NAME=TxnServer
IMPL_NAME="$INTF_NAME"Impl

PKG0=examples
PKG1=txn

JDK_BASE=jdk1.2
JINI_BASE=files/jini1_0

################################################################
# Usage
################################################################

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

ARG_LIST="-src -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
# -----------------------------------------------------------------------------
CLASSPATH=
export CLASSPATH
if [ "$JDK_HOME" != "" ]
then
  PATH="$JDK_HOME"/bin:$PATH
  export PATH
fi
echo 
java -version
echo
# -----------------------------------------------------------------------------
echo ============== $PROGRAM_NAME ==============
# -----------------------------------------------------------------------------
# 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

cd $SRC_DIR

# Compile the service's remote interface class
echo Compiling $INTF_NAME.java ...
javac -classpath $JINI_HOME/lib/jini-core.jar:$JINI_HOME/lib/jini-ext.jar:$CLASS_DIR -d $CLASS_DIR $PKG0/$PKG1/$INTF_NAME.java

# Compile the service's implementation class
echo Compiling $IMPL_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 $PKG0/$PKG1/$IMPL_NAME.java

#################################################################### 
# Execute rmic to create the stub and skeleton of the remote service
####################################################################
echo
echo ========= Now create Stub file 

# Setting -classpath for rmic over-writes the SYSTEM classpath;
# so you'll get errors saying it can't find the most basic 
# classes in java (ex. java.lang.Object). This is because
# the system classpath includes only your application classpath.
# Set the classpath as an evironment variable and the system
# classpath will be left intact.
CLASSPATH=$JINI_HOME/lib/jini-core.jar:$JINI_HOME/lib/jini-ext.jar:$CLASS_DIR
export CLASSPATH
echo Building "$IMPL_NAME"_Stub ...
rmic -keep -d $CLASS_DIR $PKG0.$PKG1.$IMPL_NAME

# Unset the CLASSPATH so that it doesn't corrupt the work below
CLASSPATH=
export CLASSPATH

# 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
cp -f $JINI_HOME/lib/mahalo.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
echo Building Executable JAR Manifest ...
echo Manifest-Version: 1.0>$SRC_DIR/$PKG_DIR/$INTF_NAME.mf
echo Main-Class: $PKG0.$PKG1.$IMPL_NAME>>$SRC_DIR/$PKG_DIR/$INTF_NAME.mf
echo Class-Path: jini-core.jar jini-ext.jar sun-util.jar>>$SRC_DIR/$PKG_DIR/$INTF_NAME.mf

# (2) Set the variables needed to create the executable jar file
JAR_CLASS_0="$PKG_DIR/"$INTF_NAME".class"
JAR_CLASS_1="$PKG_DIR/"$IMPL_NAME".class"
JAR_CLASS_2="$PKG_DIR/"$IMPL_NAME"_Skel.class"
JAR_CLASS_3="$PKG_DIR/"$IMPL_NAME"_Stub.class"
JAR_CLASS_4="$PKG_DIR/"$IMPL_NAME"\$LookupDiscoveryListener.class"
JAR_CLASS_5="$PKG_DIR/"$IMPL_NAME"\$RegisterThread.class"
JAR_CLASS_6="$PKG_DIR/"$IMPL_NAME"\$UnDoReg.class"

# (3) Create the executable jar file
echo Building Executable JAR File ...
jar cvfm $JAR_DIR/$INTF_NAME.jar $SRC_DIR/$PKG_DIR/$INTF_NAME.mf -C $CLASS_DIR $JAR_CLASS_0 
jar uvf $JAR_DIR/$INTF_NAME.jar -C $CLASS_DIR $JAR_CLASS_1
jar uvf $JAR_DIR/$INTF_NAME.jar -C $CLASS_DIR $JAR_CLASS_2
jar uvf $JAR_DIR/$INTF_NAME.jar -C $CLASS_DIR $JAR_CLASS_3
jar uvf $JAR_DIR/$INTF_NAME.jar -C $CLASS_DIR $JAR_CLASS_4
jar uvf $JAR_DIR/$INTF_NAME.jar -C $CLASS_DIR $JAR_CLASS_5
jar uvf $JAR_DIR/$INTF_NAME.jar -C $CLASS_DIR $JAR_CLASS_6

####################################################################
# Build the service downloadable jar file
####################################################################
echo 
echo ========= Build the service downloadable jar file  

# (1) Create the manifest for the service downloadable jar file
echo Building Service Downloadable JAR Manifest ...
echo Manifest-Version: 1.0>$SRC_DIR/$PKG_DIR/$INTF_NAME-srvc-dl.mf

# (2) Set the variables needed to create the service downloadable jar file
SRVC_DL_CLASS_0="$PKG_DIR/"$INTF_NAME".class"
SRVC_DL_CLASS_1="$PKG_DIR/"$IMPL_NAME"_Stub.class"

# (3) Create the service downloadable jar file
echo Building Service Downloadable JAR File ...
jar cvf $JAR_DIR/$INTF_NAME-srvc-dl.jar -C $CLASS_DIR $SRVC_DL_CLASS_0
jar uvf $JAR_DIR/$INTF_NAME-srvc-dl.jar -C $CLASS_DIR $SRVC_DL_CLASS_1

####################################################################
# Build the attribute downloadable jar file
####################################################################
echo
echo =========  Build the attribute downloadable jar file  

# (1) Create the manifest for the attribute downloadable jar file
echo Building Attribute Downloadable JAR Manifest ...
echo Manifest-Version: 1.0>$SRC_DIR/$PKG_DIR/$INTF_NAME-attr-dl.mf

# (2) Set the variables needed to create the attribute downloadable jar file
ATTR_DL_CLASS_0="net/jini/lookup/entry/ServiceInfo.class"
ATTR_DL_CLASS_1="com/sun/jini/lookup/entry/BasicServiceType.class"

# (3) Extract the Jini-specific attribute classes from the appropriate jar file
echo Extracting the Jini Attribute Class Files ...
cd $CLASS_DIR
jar xvf $JINI_HOME/lib/jini-ext.jar net/jini/lookup/entry/ServiceInfo.class
jar xvf $JINI_HOME/lib/sun-util.jar com/sun/jini/lookup/entry/BasicServiceType.class

# (4) Create the attribute downloadable jar file
echo Building Attribute Downloadable JAR File ...
jar cvf $JAR_DIR/$INTF_NAME-attr-dl.jar -C $CLASS_DIR $ATTR_DL_CLASS_0
jar uvf $JAR_DIR/$INTF_NAME-attr-dl.jar -C $CLASS_DIR $ATTR_DL_CLASS_1


####################################################################
echo
echo ========= end $PROGRAM_NAME 