#!/bin/sh
cd $(dirname $0)
if [ ! "$IMANAGER_HOME" = "" ]
then
  cd $IMANAGER_HOME/bin
fi

CHECK_VERBOSE="false"
REQUIRED_NICI_VERSION=nici-2.7.3-0.01
IMANAGER_BIN_DIR=$PWD/linux
IMANAGER_BIN_NATIVE_DIR=$PWD/../tomcat/webapps/nps/WEB-INF/bin/linux
IMANAGER_BIN_SWT_DIR=$PWD/linux/swt
IMANAGER_BIN_MOZILLA_DIR=$PWD/linux/mozilla
MAIN_CLASS=com.novell.emframe.bootstrap.iManagerBootstrap

function CHECK_PERMISSIONS()
{
   echo "Test permissions" > $IMANAGER_BIN_NATIVE_DIR/perm.txt
   if [ -e $IMANAGER_BIN_NATIVE_DIR/perm.txt ]
   then
      rm $IMANAGER_BIN_NATIVE_DIR/perm.txt
   else
      echo "ERROR:  Insufficient rights to execute iManager"
      exit
   fi
}

function CHECK_NICI()
{
   INSTALLED_NICI_VERSION=`rpm -qa | grep nici`
   NICI_INSTALL_FILE=../NICI/linux/nici.i386.rpm
   if  [ "$INSTALLED_NICI_VERSION" \< "$REQUIRED_NICI_VERSION" ]
   then
      echo "ERROR:    $REQUIRED_NICI_VERSION is required by iManager"
      echo "SOLUTION: Please install/upgrade NICI RPM as the user root"
      echo "SOLUTION: example command: rpm -Uvh $NICI_INSTALL_FILE"
      exit
   fi
}

function CHECK_JAVA_HOME()
{
   export JAVA_HOME_BAK=$JAVA_HOME
   export JAVA_HOME=
   if [ "$JAVA_HOME" = "" -o  ! -e "$JAVA_HOME/lib/tools.jar" ]
   then
      if [ ! -e "$IMANAGER_BIN_DIR/java" ]
      then
         export JAVA_HOME=$JAVA_HOME_BAK
         if [ "$JAVA_HOME" = "" -o  ! -e "$JAVA_HOME/lib/tools.jar" ]
         then
            echo "ERROR:    The JAVA_HOME environment variable is not defined correctly"
            echo "ERROR:    This environment variable is needed to run this program"
            echo "SOLUTION: example command: export JAVA_HOME=/usr/java/j2sdk1.4.2_08"
            exit
         fi
      else
         export JAVA_HOME=$IMANAGER_BIN_DIR/java
      fi
   fi
}


function CHECK_SYMBOLIC_LINKS ()
{
   WORKING_DIR=$PWD
   cd $IMANAGER_BIN_NATIVE_DIR
   if [ ! -e libJClient.so ] && [ -e libJClient.so.1.0.0 ];
   then
      ln -sf libJClient.so.1.0.0 libJClient.so
      ln -sf libJClient.so.1.0.0 libJclient_g.so
      ln -sf libJClient.so.1.0.0 libJClient.so.1
   fi
   if [ ! -e libndssdk.so ] && [ -e libndssdk.so.1.0.0 ];
   then
      ln -sf libndssdk.so.1.0.0 libn4u.so.0
      ln -sf libndssdk.so.1.0.0 libpdksa.so.0
      ln -sf libndssdk.so.1.0.0 libdclient.so.0
      ln -sf libndssdk.so.1.0.0 libndssdk.so
      ln -sf libndssdk.so.1.0.0 libndssdk.so.1
   fi
   if [ ! -e libsal.so ] && [ -e libsal.so.1.0.0 ];
   then
      ln -sf libsal.so.1.0.0 libsal.so
      ln -sf libsal.so.1.0.0 libsal.so.1
   fi
   if [ ! -e libsch.so ] && [ -e libsch.so.1.0.0 ];
   then
      ln -sf libsch.so.1.0.0 libsch.so
      ln -sf libsch.so.1.0.0 libsch.so.1
   fi
   if [ ! -e libntls.so.2 ] && [ -e libntls.so ];
   then
      ln -sf libntls.so libntls.so.2.0
      ln -sf libntls.so libntls.so.2
   fi
   #
   # ??? libnmasclnt.so ??? does it need links
   # ??? libspmclnt.so ??? does it need links
   #

   # removed when placed in eDirExtended.npm
   #   if [ ! -e libldapsdk.so ] && [ -e libldapsdk.so.0.0.0 ];
   #   then
   #      ln -sf libldapsdk.so.0.0.0 libldapsdk.so
   #      ln -sf libldapsdk.so.0.0.0 libldapsdk.so.0
   #   fi
   #   if [ ! -e libnpkiapi.so.3.0.0 ] && [ -e libnpkiapi.so ];
   #   then
   #      mv libnpkiapi.so libnpkiapi.so.3.0.0
   #      ln -sf libnpkiapi.so.3.0.0 libnpkiapi.so
   #      ln -sf libnpkiapi.so.3.0.0 libnpkiapi.so.2
   #      ln -sf libnpkiapi.so.3.0.0 libnpkiapi.so.3
   #   fi
   #   if [ ! -e libnpkit.so.3.0.0 ] && [ -e libnpkit.so ];
   #   then
   #      mv libnpkit.so libnpkit.so.3.0.0
   #      ln -sf libnpkit.so.3.0.0 libnpkit.so
   #      ln -sf libnpkit.so.3.0.0 libnpkit.so.2
   #      ln -sf libnpkit.so.3.0.0 libnpkit.so.3
   #   fi
   cd $WORKING_DIR
}

function BUILD_CLASSPATH ()
{
   CPATH=.
   CPATH=$CPATH:$JAVA_HOME/lib/tools.jar
   CPATH=$CPATH:bootstrap.jar
   CPATH=$CPATH:iManBootstrap.jar
   CPATH=$CPATH:$IMANAGER_BIN_SWT_DIR/swt.jar
   CPATH=$CPATH:$PWD/../tomcat/bin/commons-logging-api.jar
   CPATH=$CPATH:$PWD/../tomcat/bin/jmx.jar
}

function BUILD_BOOT_CLASSPATH()
{
	BPATH=$PWD/../tomcat/common/endorsed/xercesImpl.jar
	BPATH=$BPATH:$PWD/../tomcat/common/endorsed/xml-apis.jar
}

function BUILD_LD_LIBRARY_PATH ()
{
   LPATH=$IMANAGER_BIN_NATIVE_DIR
   LPATH=$LPATH:$IMANAGER_BIN_SWT_DIR
   LPATH=$LPATH:$IMANAGER_BIN_MOZILLA_DIR

   export LD_LIBRARY_PATH=$LPATH:$LD_LIBRARY_PATH
   export MOZILLA_FIVE_HOME=$IMANAGER_BIN_MOZILLA_DIR
}

function START_IMANAGER ()
{
   CHECK_PERMISSIONS;
   CHECK_NICI;
   CHECK_SYMBOLIC_LINKS;
   CHECK_JAVA_HOME;
   BUILD_CLASSPATH;
   BUILD_LD_LIBRARY_PATH;
   BUILD_BOOT_CLASSPATH;
   EPATH=$PWD/../tomcat/common/endorsed
}

START_IMANAGER;
linux/mozilla/xulrunner --register-user;
$JAVA_HOME/bin/java -server -Xms256m -Xmx512m -Xbootclasspath/a:$BPATH -cp $CPATH -Djava.endorsed.dir=$EPATH $DEBUG_PARAMS $MAIN_CLASS $1 $2 $3 $4 $5 $6 $7 $8 $9
linux/mozilla/xulrunner --unregister-user;
