#!/bin/sh

if [ "$SYBROOT" = "" ]
then
   echo "Error: \$SYBROOT variable not set"
   exit 1
fi  

# ... Environment Settings ........................................

PLATFORM=`uname -s`

# Set JRE directory
if [ "$SYBASE_JRE6" = "" ]
then
   echo "
	Error: 	You need to set \$SYBASE_JRE6 to the directory where your Java JRE is installed."
	exit 1
fi

if [ -f "$SYBASE_JRE6/bin/java" ]
then
    PATH="${SYBASE_JRE6}/bin:$PATH"  # PATH to local JRE first
    export PATH
    export SYBASE_JRE6
else
    echo "
	Error: 	Unable to find the Java Binary.
		Please check that \$SYBROOT has been set correctly and 
		that the file '$SYBASE_JRE6/bin/java exists' exists.\n
		\$SYBROOT currently set to '$SYBROOT'\n"
			exit 1
fi

if [ "$SYBASE_WS" = "" ]
then
	SYBASE_WS=WS-15_0
	export SYBASE_WS
fi

# check native lib dir
if [ ! -d $SCSHARE/lib64 ]
then
    LD_LIBRARY_PATH="$SCSHARE/lib32:$LD_LIBRARY_PATH"
else
    LD_LIBRARY_PATH="$SCSHARE/lib32:$SCSHARE/lib64:$LD_LIBRARY_PATH"
    LD_LIBRARY_PATH_64="$SCSHARE/lib64:$LD_LIBRARY_PATH_64"
    export LD_LIBRARY_PATH_64
fi
export LD_LIBRARY_PATH

if [ $PLATFORM = AIX ]
then
    LIBPATH="$SYBASE_JRE6/bin:$SYBASE_JRE6/bin/classic:$SCSHARE/lib32:$SCSHARE/lib64:$LIBPATH"
	export LIBPATH
fi

ASEP_DIR="$SYBROOT/ASEP"

if [ ! -f "$ASEP_DIR/lib/ASEPlugin.jar" ] 
then
    echo "
        Error:  Unable to find the ASE Plug-in Binary.
                Please check that \$SYBROOT has been set correctly and
                that the file '$ASEP_DIR/lib/ASEPlugin.jar' exists.\n
                \$SYBROOT currently set to '$SYBROOT'\n"
    exit 1
fi

if [ ! -f "$SYBROOT/shared/lib/jconn3.jar" ] 
then
    echo "
        Error:  Unable to find the Connectivity Library.
                Please check that \$SYBROOT has been set correctly and
                that the file '$SYBROOT/shared/lib/jconn3.jar' 
                exists.\n \$SYBROOT currently set to '$SYBROOT'\n"
    exit 1
fi

if [ ! -f "$SYBROOT/shared/lib/dsparser.jar" ]
then
    echo "
        Error:  Unable to find the Directory Service Parser Binary.
                Please check that \$SYBROOT has been set correctly and
                that the file '$SYBROOT/shared/lib/dsparser.jar'
                exists.\n \$SYBROOT currently set to '$SYBROOT'\n"
    exit 1
fi

if [ ! -f "$SYBROOT/shared/java/jsyblib600.jar" ]
then
    echo "
        Error:  Unable to find the Sybase Central Shared Library.
                Please check that \$SYBROOT has been set correctly and
                that the file '$SYBROOT/shared/java/jsyblib600.jar'
                exists.\n \$SYBROOT currently set to '$SYBROOT'\n"
    exit 1
fi

if [ ! -f "$SYBROOT/DBISQL/lib/jlogon.jar" ]
then
    echo "
        Error:  Unable to find the Logon Shared Libary.
                Please check that \$SYBROOT has been set correctly and
                that the file '$SYBROOT/DBISQL/lib/jlogon.jar'
                exists.\n \$SYBROOT currently set to '$SYBROOT'\n"
    exit 1
fi

showUsage()
{
  echo "Usage: aseplugin <options> <arguments>"
  echo 
  echo "options:"
  echo "       -h Displays this help."
  echo "       -v Prints version information."
  echo "       -i Run application in interactive console mode."
  echo "       -s Run application in silent mode."
  echo 
  echo arguments:     [ Applicable only with -i options ]
  echo        file1   [ Location of file to output error/progress {optional} ]
  echo
  echo arguments:     [ Applicable only with -s option ]
  echo        file1   [ Location of resource file in java properties format ]
  echo        file2   [ Location of file to output error/progress {optional} ]
  echo
  echo
  echo Sample usage1: [ Running ASE Plugin in normal mode ]
  echo
  echo aseplugin
  echo
  echo
  echo Sample usage2: [ Running ASE Plugin in interactive console mode ]
  echo
  echo aseplugin -i
  echo
  echo
  echo Sample usage3: [ Running ASE Plugin in silent mode ]
  echo
  echo aseplugin -s $SYBASE/WS-15_0/props/ws.properties $SYBASE/WS-15_0/e.log
  echo
}

if [ $# -ne 0 ]
then
   if [ "$1" = "-v" ]
   then
     "$SYBASE_JRE6/bin/java" -jar $ASEP_DIR/lib/ASEPlugin.jar -v
     exit 0
   elif [ "$1" = "-i" ]
   then
      "$SYBASE_JRE6/bin/java" -classpath $ASEP_DIR/lib/ASEPlugin.jar:$SYBROOT/shared/lib/dsparser.jar:$SYBROOT/shared/lib/jconn3.jar:$SYBROOT/DBISQL/lib/jlogon.jar:$SYBROOT/shared/java/jsyblib600.jar com.sybase.aseplugin.so.WebServicesConfigurationSO $*
      exit 0
   elif [ "$1" = "-s" ]
   then
      if [ "$2" = "" ]
      then
        echo "Resource file name is not specified, Please see the usage below"
        showUsage
        exit 1
      else
        "$SYBASE_JRE6/bin/java" -DSYBASE_JRE6="$SYBASE_JRE6" -DSYBASE_WS="$SYBASE_WS" -classpath $ASEP_DIR/lib/ASEPlugin.jar:$SYBROOT/shared/lib/dsparser.jar:$SYBROOT/shared/lib/jconn3.jar:$SYBROOT/DBISQL/lib/jlogon.jar:$SYBROOT/shared/java/jsyblib600.jar com.sybase.aseplugin.so.WebServicesConfigurationSO $*
        exit 0
      fi
   else
      showUsage
      exit 1
   fi
fi


if [ ! -f "$SYBROOT/shared/sybcentral600/scjview.sh" ] 
then
    echo "
	Error: 	Unable to find Sybase Central.
		Please check that \$SYBROOT has been set correctly and 
		that the file '$SYBROOT/shared/sybcentral600/scjview.sh' exists.\n
		\$SYBROOT currently set to '$SYBROOT'\n"
    exit 1
fi
"$SYBROOT/shared/sybcentral600/scjview.sh"
