#!/usr/bin/sh
# 
PRG="$0"
# check if the file: startXXX.sh or stopXXX.sh is symbolic link
#	while [ -h "$PRG" ]; do
#	   ls = `ls -ld "$PRG"`
#	   echo $ls
#	   link = `expr "$ls" : '.*-> \(.*\)$'`
#	   if expr "$link" : '/.*' > /dev/null; then
#	      PRG="$link"
#	   else
#	      PRG=`dirname "$PRG"`/"$link"
#	   fi
#	done
# get $MWMS_HOME/bin directory
PRGDIR=`dirname "$PRG"`
# initial MWMS_HOME when MWMS_HOME is not set
[ -z "$MWMS_HOME" ] && MWMS_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
# initial MWMS_BASE when MWMS_BASE is not set
[ -z "$MWMS_BASE" ] && MWMS_BASE="$MWMS_HOME"

ACTION=$1
SVR=$2
SEQNUM=$3
JMXPORT=$4
HTTPPORT=$5
xms=$6
xmx=$7
useTibrv=${10}
useamqp=${11}
scheduler=${12}

CLASSPATH=$CLASSPATH

echo "Using MWMS_HOME:    $MWMS_HOME";
case $MWMS_HOME in
  *:*) echo "Unable to start as MWMS_HOME contains a colon (:) character";
       exit 1;
esac
echo "Using MWMS_BASE:    $MWMS_BASE";
case $MWMS_BASE in
  *:*) echo "Unable to start as MWMS_BASE contains a colon (:) character";
       exit 1;
esac

if [ `hostname` = 'mdlap1' ]
    then
		SEQ="$SVR"1"$SEQNUM"
	elif [ `hostname` = 'mdlap2' ]
	then
		SEQ="$SVR"2"$SEQNUM"
	elif [ `hostname` = 'md1ap3' ]
	then
		SEQ="$SVR"3"$SEQNUM"
	else
		SEQ="$SVR"1"$SEQNUM"
fi


if [ -z "$MWMS_TMPDIR" ]; then
  MWMS_TMPDIR="$MWMS_BASE"/temp
fi

if [ ! -d "$MWMS_TMPDIR/$SEQ" ] ; then
   echo '$SEQ='$SEQ
   mkdir -p "$MWMS_TMPDIR/$SEQ"
fi

svrPrivateDir="$MWMS_TMPDIR/$SEQ"
#echo "svrPrivateDir="$svrPrivateDir
if [ -z "$MWMS_PID" ]; then
  MWMS_PID="$MWMS_TMPDIR"/"$SEQ"/mwms.run
fi

if [ $ACTION = 'start' ] || [ $ACTION = 'run' ] ; then
  if [ -r "$MWMS_BASE"/bin/isProcessRunning.sh ]; then
  . "$MWMS_BASE"/bin/isProcessRunning.sh $SVR $SEQ $HTTPPORT
  fi
	if [ -r "$MWMS_BASE"/bin/setenv.sh ]; then
	  . "$MWMS_BASE"/bin/setenv.sh $SVR $SEQNUM $6 $7 $8 $9
	fi
	if [ -r "$MWMS_HOME"/bin/setclasspath.sh ]; then
	  . "$MWMS_HOME"/bin/setclasspath.sh
	else
	  echo "cann't find $MWMS_HOME/bin/setclasspath.sh"
	  echo "This file is needed to run this project"
	  exit 1
	
	fi
	if [ ! -z "$CLASSPATH" ] ; then
	  CLASSPATH="$CLASSPATH":.:
	fi
	
	if [ -z "$MWMS_OUT" ] ; then
	  MWMS_OUT="$MWMS_BASE"/logs/"$SEQ".out
	fi
	
	if [ -z "$MAINCLASS" ] ; then
	  MAINCLASS="$svrPrivateDir"/bootstrap.jar
	fi
	#echo "MAINCLASS="$MAINCLASS
	if [ -a "$MAINCLASS" ] ; then
	  rm -f "$MAINCLASS"
	  echo "delete $MAINCLASS"
	fi
	cp "$MWMS_BASE"/lib/bootstrap.jar "$svrPrivateDir"/
	echo "copy $MWMS_BASE/lib/bootstrap.jar to $svrPrivateDir/"
	
	if [ -z "$UMASK" ]; then
	  UMASK="0027"
	fi
	umask $UMASK
	
	if [ -z "$USE_NOHUP" ] ; then
	  USE_NOHUP="true"
	fi
	
	unset _NOHUP
	if [ "$USE_NOHUP" = "true" ] ; then
	  _NOHUP="nohup"
	fi 
	
	have_tty=0
	if [ -t 0 ]; then
	  have_tty=1
	fi
	
	if [ $have_tty -eq 1 ]; then
	  echo "Using MWMS_HOME:    $MWMS_HOME"
	  echo "Using MWMS_BASE:    $MWMS_BASE"
	  echo "Using MWMS_TMPDIR:  $MWMS_TMPDIR"
	  echo "Using JAVA_HOME:   $JAVA_HOME"
	  echo "Using CLASSPATH:   $CLASSPATH"
	  echo "Using MWMS_OUT:     $MWMS_OUT"
	  if [ ! -z "$MWMS_PID" ]; then
	    echo "Using MWMS_PID:   $MWMS_PID"
	  fi 
	fi
	
	if [ $ACTION = "run" ] ; then
	  shift
	  # shellcheck disable=SC2145
	  echo "$_RUNJAVA -jar $MWMS_OPTS $TIBCO_OPTS $JAVA_OPTS -Dcom.sun.management.jmxremote.port=$JMXPORT -Dhttp.port=$HTTPPORT  -Dmwms.home=$MWMS_HOME -Djava.io.tmpdir=$MWMS_TMPDIR $MAINCLASS $@ start"
	  eval exec "\"$_RUNJAVA\"" " -jar " $MWMS_OPTS  \
	  -Dcom.sun.management.jmxremote \
	  -Dcom.sun.management.jmxremote.port=$JMXPORT \
	  -Dcom.sun.management.jmxremote.ssl=false \
	  -Dcom.sun.management.jmxremote.authenticate=false \
	  -Djmx.port=$JMXPORT \
	  -Dhttp.port=$HTTPPORT \
	   $JAVA_OPTS \
	  -Dmwms.home="\"$MWMS_HOME\"" \
	  -Djava.io.tmpdir="\"$MWMS_TMPDIR\"" \
	  "$MAINCLASS" "$@" start
	elif [ $ACTION = "start" ] ; then
	  
	  if [ -f "$MWMS_PID" ]; then
	    if [ -s "$MWMS_PID" ]; then
	      echo "Existing PID file found during start."
	      if [ -r "$MWMS_PID" ]; then
	        PID=`cat "$MWMS_PID"`
	        ps -p $PID >/dev/null 2>&1
	        if [ $? -eq 0 ] ; then
	          echo "MWMS appears to still be running with PID $PID. Start aborted."
	          echo "If the following process is not a MWMS process, remove the PID file and try again:"
	          ps -f -p $PID
	          exit 1
	        fi
	      fi
	    fi
	  fi
	  shift
	  # touch "$MWMS_OUT" 
	  # shellcheck disable=SC2145
	  echo "$_NOHUP $_RUNJAVA -jar  $MWMS_OPTS  $JAVA_OPTS -Dscheduler=$scheduler -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.ssl=false -Dhttp.port=$HTTPPORT -Dmwms.home=$MWMS_HOME -Djava.io.tmpdir=$MWMS_TMPDIR -DuseTibrv=$useTibrv -Duseamqp=$useamqp -Dclient=OIC -Dspring.jdbc.getParameterType.ignore=true $MAINCLASS $@ start >> /dev/null 2>&1 &"
	  eval $_NOHUP "\"$_RUNJAVA\"" " -jar " \
	  "$MWMS_OPTS" \
	  -Dcom.sun.management.jmxremote \
	  -Dcom.sun.management.jmxremote.port=$JMXPORT \
	  -Dcom.sun.management.jmxremote.ssl=false \
	  -Dcom.sun.management.jmxremote.authenticate=false \
	  -Djmx.port=$JMXPORT \
	  -Dhttp.port=$HTTPPORT \
	  $JAVA_OPTS \
	  -Dmwms.base="\"$MWMS_BASE\"" \
	  -Dmwms.home="\"$MWMS_HOME\"" \
	  -Djava.io.tmpdir="\"$MWMS_TMPDIR\"" \
	  -Dscheduler=$scheduler \
	  -DuseTibrv=$useTibrv \
	  -Duseamqp=$useamqp \
	  -Dclient=OIC \
	  -Dspring.jdbc.getParameterType.ignore=true \
	  "$MAINCLASS" "$@" start \
	  >> /dev/null 2>&1 "&"
	fi
elif [ $ACTION = "version" ] ; then
	  echo "version"
else
	  echo "Usage:  zidmboot.sh ( commands ... )"
	  echo "commands:"
	  echo "  run           Start MWMS AP Server in the current window"
	  echo "  start         Start MWMS AP Server in a separate window"
	  echo "  stop          Stop MWMS AP Server, waiting up to 5 seconds for the process to end"
	  echo "  stop n        Stop MWMS AP Server, waiting up to n seconds for the process to end"
	  echo "  stop -force   Stop MWMS AP Server, wait up to 5 seconds and then use kill -KILL if still running"
	  echo "  stop n -force Stop MWMS AP Server, wait up to n seconds and then use kill -KILL if still running"
	  echo "  version       What version of MWMS are you runing?"
	  echo "Note: Waiting for the process to end and use of the -force option require that \$MWMS_PID is defined"
	  exit 1
fi
