#!/bin/sh
############################################################ {COPYRIGHT-TOP} ###
# Licensed materials - Property of IBM
# IBM Tivoli Storage Manager
#
# 5698-ISX (C) Copyright IBM Corporation 2013
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with
# IBM Corp.
############################################################ {COPYRIGHT-END} ##
#
### BEGIN INIT INFO
# Provides: wlp start and stop
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 3 5
# Default-Stop: 0 6
# chkconfig: 35 20 80
# Description: starts and stops the liberty profile for IBM Spectrum Protect Backup-Archive Web UI
### END INIT INFO
#
WLP_INST_DIR="/opt/tivoli/tsm/tdpvmware/common/webserver";
WLP_START="${WLP_INST_DIR}/bin/server";
WLP_STOP="${WLP_INST_DIR}/bin/server";

export JAVA_HOME=/opt/tivoli/tsm/tdpvmware/common/jre/jre

#######################################################################
# function isWlpRunning
# check if wlp is up and running
#######################################################################
isWlpRunning() {
WLP_PID_FILE="${WLP_INST_DIR}/usr/servers/.pid/veProfile.pid";
# check if file with pid for wlp exists
if test -f "${WLP_PID_FILE}"; then
  ## ok looks like wlp is running, lets check for the process
  PID=`cat ${WLP_PID_FILE}`;
  if test `ps -ef | grep java | grep ${PID} | wc -l` -eq 1;then
    # wlp up and running
    return 0;
  else
    return 2;  
  fi
else
  # wlp not runnning
  return 1; 
fi
}

# get current userid
USER=`id -u -n`;

if test -d ${WLP_INST_DIR}; then 
  # get the userid which owns the wlp directory
  USER_WLP=`ls -dl ${WLP_INST_DIR} | awk '{print $3}'`;
else
  echo "The wlp install dir ${WLP_INST_DIR} does not exist";
  exit 2;
fi
if test "${USER}" = "root"; then
  CURRENT_DIR=`dirname $0`;
  if test "${USER_WLP}" = "root"; then
    echo "The correct wlp user cannot be determined. Check the correct ownership of ${WLP_INST_DIR}, your eWas install directory.";
    exit 2;
  fi
  
  if test "${CURRENT_DIR}" = "."; then
    CURRENT_DIR=`pwd`;
  fi
  
  # we are running as root user, switch to wlp user and invoke the script again
  su - ${USER_WLP} -c "cd ${CURRENT_DIR};$0 $1";
  exit $?;
else
  if test "${USER}" != ${USER_WLP}; then
    # the user which invoked this script was not root and not the user which owns the wlp directory
    echo "The script was started as the wrong user. Login as user ${USER_WLP} and retry the command.";
    exit 2;
  fi
fi

isWlpRunning;
WLP_RUNS=$?
case "$1" in
start)
# check if start script exists
if ! test -x ${WLP_START}; then
  echo "The WebSphere Liberty Profile start script ${WLP_START} does not exist.";
  exit 2;
fi

if test ${WLP_RUNS} -ne 0; then
  echo "Starting the WebSphere Liberty Profile ..."
  # start the wlp server 
  if test -x /usr/bin/sudo; then
    if test -f /opt/tivoli/tsm/client/ba/bin/dsmutillnx; then
       sudo /opt/tivoli/tsm/client/ba/bin/dsmutillnx setupFirewall 0 2>&1 >/dev/null
    else
       echo "Pre-start check: continuing."
    fi
  else
     echo "Pre-start check: continuing."
  fi

  ${WLP_START} start veProfile --clean
else
  # wlp already started
  echo "The WebSphere Liberty Profile is already started.";
fi
;;
stop)
# check if stop script exists
if ! test -x ${WLP_STOP}; then
  echo "The WebSphere Liberty Profile stop script ${WLP_STOP} does not exist.";
  exit 2;
fi

if test ${WLP_RUNS} -eq 0; then
  echo "Stopping the WebSphere Liberty Profile ..."
  # stop the wlp server
  ${WLP_STOP} stop veProfile
else
  # wlp is already stopped
  echo "The WebSphere Liberty Profile is already stopped.";
fi
;;
restart)
# check if start script exists
if ! test -x ${WLP_START}; then
  echo "The WebSphere Liberty Profile start script ${WLP_START} does not exist.";
  exit 2;
fi
# check if stop script exists
if ! test -x ${WLP_STOP}; then
  echo "The WebSphere Liberty Profile stop script ${WLP_STOP} does not exist.";
  exit 2;
fi
echo "Restarting  the WebSphere Liberty Profile ...";
if test ${WLP_RUNS} -eq 0; then
  # Change the location to your specific location
  ${WLP_STOP} stop veProfile
  RC_STOP=$?
else
  RC_STOP=0;
fi
if test $RC_STOP -eq 0; then
  sleep 5;
  ${WLP_START} start veProfile --clean
fi
;;
status)
if test ${WLP_RUNS} -eq 0; then
  echo "WebSphere Liberty Profile (pid $PID) is running...";  
else
  echo "WebSphere Liberty Profile is stopped.";
fi
;;
*)
echo "Usage: '$0' {start|stop|restart|status}" 
exit 64
;;
esac
exit $?
