function usage {
    echo "$0 {UAT|DEV|PROD} -r";
	exit 1;
}
 		
function killApp {	
    pid=`cat ../logs/$APP_NAME.pid`;
	kill $pid;
}
		
#check deployment environment of application
function checkDeploymentEnvironment() {
    
	if [ -z $ALLOWED_HOSTS ]; then
	  echo "ERROR: Define ALLOWED_HOSTS is $ENV.properties"
	  exit 1;	
	fi
	
	for ALLOWED_HOST in  ${ALLOWED_HOSTS[@]}
	do
		if [ "$THISHOST" == "$ALLOWED_HOST" ]; then
			echo "################HostName matches for $THISHOST#########";
			return 0;		
		else 
			echo "Hostname $ALLOWED_HOST not a match"
		fi
		
	done;
	
	return 1;
}

# pattern

function killApp {	
    pid=`cat ../logs/$APP_NAME.pid`;
	kill $pid;
}

function usage {
    echo "$0 {UAT|DEV|PROD} -r";
	exit 1;
}

# pattern
findPids()
{
	pids=`ps -ef | grep $1 | grep -v grep | awk '{print $2 }'`
	echo $pids
}
	
	
# name pattern
status()
{
	echo $2
	pids=`findPids $2`
	if [ -n "$pids" ]; then
		echo "$1 is running"
		echo "PIDS -- $pids"
		RAVL=0;
	else
		echo "$1 is not running"
		RAVL=1;
	fi
}

# name, pattern , commandline
start()
{
#	echo  "start $1 "
	pids=`findPids $2`
	if [ -n "$pids" ]; then
		echo "$1 is running"
		echo "PIDS -- $pids"
	else 
		if [ "${PRINT_CONFIG_DETAILS}" == "true" ]; then
			echo "commandline: $3 "
		else
			echo "starting $1 "
			echo "$3"
		fi
		STDOUT_LOG=$STD_OUT
		if [ "${STDOUT_LOG}" == "" ]; then
				STDOUT_LOG=/dev/null
		fi
		nohup $3 >> ${STDOUT_LOG} 2>&1 &
		## now make sure it is running
		sleep 5
		pids=`findPids $2`
		if [ -n "$pids" ]
		  then
			echo "$1 started pid: $pids"
			exit 0
		  else 
			echo "ERROR: failed to start $1"
			exit 1
		
		fi
	fi
}

startBatch()
{
#	echo  "startBatch $1 "
	pids=`findPids $2`
	if [ -n "$pids" ]; then
		echo "$1 is running"
		echo "PIDS -- $pids"
	else 
		if [ "${PRINT_CONFIG_DETAILS}" == "true" ]; then
			echo "commandline: $3 "
		else
			echo "starting $1 "
			echo "$3"
		fi
		STDOUT_LOG=$STD_OUT
		if [ "${STDOUT_LOG}" == "" ]; then
				STDOUT_LOG=/dev/null
		fi
		$3 >> ${STDOUT_LOG} 2>&1
		exitStatus=$?
		echo "Batch complete, status - $exitStatus"
		exit $exitStatus
	fi
}

stop()
{
	echo  "Stopping $1 ...."
	pids=`findPids $2`
	if [ -n "$pids" ]; then
		echo  "pids: $pids"
		kill $pids;
	fi

	## make sure it is killed
	for i in {1..10}
	do
	  pids=`findPids $2`
          if [ -z "$pids" ]
          then 
		 echo "$1 stopped"
                exit 0
           else
		echo "$1 still running check again after 10 seconds"
                sleep 10
	  fi	
  		
        done
	echo "force kill is issued to stop $1, $pids"
	kill -9 $pids

	pids=`findPids $2`
          if [ -z "$pids" ]
          then
                 echo "$1 stopped"
                exit 0
           else
                echo "WARNING: $1 still running after force kill"
                exit 1
          fi

}


