#! /usr/bin/sh

end() {
  exit
}

finish () {
  echo
  echo "========= End of building $DIRECTORY EJBean example ==========="
  echo
  end
}

completed() {
  echo
  echo "  The EJBean has been saved in"
  echo
  echo "   $WL_HOME/myserver/$JAR"
  echo
  echo "  To deploy the EJBean, either use the hot deploy feature of"
  echo "  the WebLogic Console to deploy the EJBean or add to the"
  echo "  \"weblogic.ejb.deploy\" property in the \"weblogic.properties\"" 
  echo "  file an entry to deploy the .jar."
  echo
  echo "  To run the client, follow the instructions in the"
  echo "  documentation for the example."
  echo
  finish
}

error() {
  echo
  echo "  An error has occurred. Error level: $1"
  cd $WORK_DIR
  finish
}

cleanup() {
  cd $WORK_DIR
  rm -rf $TEMP
}

build () {

  if [ ! -d $TEMP ]
  then
    mkdir $TEMP
  fi
  if [ ! -d $EJB_CLIENT_CLASSES ]
  then
    mkdir $EJB_CLIENT_CLASSES
  fi
  if [ ! -d $SERVLET_CLASSES ]
  then
    mkdir $SERVLET_CLASSES
  fi

  # Compile classes
  javac -d $TEMP $DIRECTORY/*.java
  retval=$?
  if [ "$retval" -ne 0 ]
  then
    error $retval
  fi

  # Copy classes to the clientclasses and servletclasses directories
  cp $EJB_TEMP/* $EJB_CLIENT_CLASSES
  if [ -f $EJB_TEMP/*Servlet.class ]
  then
    cp $EJB_TEMP/*Servlet.class $EJB_SERVLET_CLASSES
  fi

  # Delete the Client code from the temp directory
  # and the Bean or Servlet code from the client directory

  rm -rf $EJB_TEMP$/*Client*.class
  rm -rf $EJB_TEMP$/*Servlet.class
  rm -rf $EJB_CLIENT_CLASSES/*Bean*.class
  rm -rf $EJB_CLIENT_CLASSES/*Servlet.class
 
  # Create a .ser file from the deployment descriptor

  java weblogic.ejb.utils.DDCreator -d $EJB_TEMP -outputfile DD.ser $DIRECTORY/DeploymentDescriptor.txt
  retval=$?
  if [ "$retval" -ne 0 ]
  then
    error $retval
  fi

  # Run ejbc on the DD.ser file, creating generated files

  java weblogic.ejbc -d $TEMP $EJB_TEMP/DD.ser
  retval=$?
  if [ "$retval" -ne 0 ]
  then
    error $retval
  fi

  # Jar the EJBean using the manifest file

  cd $TEMP
  if [ "$EXAMPLE" = "child" ]
  then
    if [ -f ../myserver/ejb_subclass_parent.jar ]
    then
      jar xf ../myserver/ejb_subclass_parent.jar
      jar cmf ../$EJB_DIRECTORY/manifest ../myserver/ejb_subclass.jar examples/ejb/subclass
      rm -rf ../myserver/ejb_subclass_*.jar
    else
      cleanup
      error 
    fi
  else
    jar cmf ../$EJB_DIRECTORY/manifest ../myserver/$JAR $EJB_DIRECTORY
  fi
  retval=$?
  if [ "$retval" -ne 0 ]
  then
    error $retval
  fi
}

dependcheck() {
  if [ ! $CLIENT_CLASSES ]
  then
    echo
    echo   "Building the examples is dependent on setting certain variables"
    echo   "in your environment. Please source or dot command the setEnv.sh script"
    echo   "(found in the /weblogic directory) before running this script."
    finish
  elif [ "$GROUP" = "extensions" ]
  then
    if [ ! -f ../../myserver/ejb_basic_containerManaged.jar ]
    then 
      echo
      echo   "Building the extensions examples is dependent on the"
      echo   "basic/containerManaged example having been built first. Please"
      echo   "build the basic/containerManaged example (\"build basic containerManaged\")"
      echo   "before building either of the extensions examples."
      finish
    fi
  elif [ "$GROUP" = "subclass" ]
  then
    if [ "$EXAMPLE" = "child" ]
    then
      CLASSPATH=$CLASSPATH:../../myserver/ejb_subclass_parent.jar;export CLASSPATH
      if [ ! -f ../../myserver/ejb_subclass_parent.jar ]
      then
        echo
        echo   "Building the subclass child example is dependent on the"
        echo   "subclass/parent example having been built first. Please"
        echo   "build the subclass/parent example (\"build subclass parent\")"
        echo   "before building the subclass/child example."
        finish
      fi
    fi
  fi
}

begin() {
  DIRECTORY=$GROUP/$EXAMPLE
  echo
  echo "========= Start of building $DIRECTORY EJBean example ========="
  echo
  JAR=ejb_"$GROUP"_"$EXAMPLE".jar
  EJB_DIRECTORY=examples/ejb/$DIRECTORY
  TEMP=../../temp
  EJB_TEMP=$TEMP/$EJB_DIRECTORY
  EJB_CLIENT_CLASSES=$CLIENT_CLASSES/$EJB_DIRECTORY
  EJB_SERVLET_CLASSES=$SERVLET_CLASSES/EJB_DIRECTORY
  CLASSPATH=$CLASSPATH:$TEMP:;export CLASSPATH

  # check dependencies
  dependcheck
  # build EJBean
  build
  # cleanup temp files
  cleanup
  completed
}

if [ "$2" != "" ]
then
  GROUP=$1
  EXAMPLE=$2
  WORK_DIR=`pwd`
  begin
else
# -- Usage --
  echo "Usage: build group example"
  echo "       where group is the example group and"
  echo "       where example is the example directory, such as"
  echo "         basic beanManaged"
  echo "         sequences oracleSeq"
  echo "       the EJBean will be saved in ejb_{group}_{example}.jar"
  echo "       You must source or dot command the setEnv script"
  echo "       before using this script."
  end
fi
exit
