#!/bin/bash
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
#
# P4DTG server init script

# p4dtg Init Script.
# chkconfig: - 90 30
# description: P4DTG Start Up Script

export TMP=$P4TMP

# Load SDP controlled shell environment.
source /p4/common/bin/p4_vars REPL_SDP_INSTANCE ||\
   { echo -e "\nError: Failed to load SDP environment for instance REPL_SDP_INSTANCE.\n"; exit 1; }

source $P4CBIN/ps_functions.sh ||\
   { echo -e "\nError: Failed to load SDP ps_functions.sh lib.\n"; exit 1; }

if [[ $(id -u) -eq 0 ]]; then
   exec su - $OSUSER -c "$0 $*"
elif [[ $(id -u -n) != $OSUSER ]]; then
   echo "$0 can only be run by root or $OSUSER"
   exit 1
fi

cd $P4DTG_ROOT
if [[ $? -ne 0 ]]; then
   echo "The P4DTG_ROOT directory [$P4DTG_ROOT] does not exist."
   exit 1
fi

declare StopFile="$P4DTG_ROOT/repl/stop-$P4DTG_CFG"
declare ErrorFile="$P4DTG_ROOT/repl/err-$P4DTG_CFG"
declare MovedErrorFile=

# See how we were called.
case "${1:-status}" in
   start)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -n "$p4dtg_pids" ]]; then
         echo -e "Error: ${P4DTGBIN} is already running as pids: $p4dtg_pids."
         exit 1
      fi

      [[ -e "$StopFile" ]] && rm -f "$StopFile"

      if [[ -e "$ErrorFile" ]]; then
         if [[ "${2:-Unset}" == "-f" ]]; then
            MovedFile="$P4DTG_ROOT/repl/MOVED_FILE_err-$(date +'%Y%m%d-%H%M')"
            mv -f "$ErrorFile" "$MovedFile"
            echo -e "\nWarning: P4DTG Error File detected and moved aside to: $MovedFile\nStarting anyway due to -f.\n"
         else
            echo -e "\Error: Refusing to start because a P4DTG error file was found:\n\t$ErrorFile\nReview this file.\nTo start P4DTG after reviewing this file, try again with -f which will the error file aside first, i.e.: $0 start -f\n"
            exit 1
         fi
      fi

      echo -e "\nStarting P4DTG with: $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT"
      $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT &
   ;;

   stop)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -z "$p4dtg_pids" ]]; then
         echo -e "Error: ${P4DTGBIN} is NOT running.\n"
         exit 1
      fi

      echo -e -n "\nShutting down P4DTG instance P4DTG_CFG ..."
      cd $P4DTG_ROOT/repl
      touch stop-$P4DTG_CFG
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      while [[ -n "$p4dtg_pids" ]]; do
         sleep 2
         echo -n "."
         p4dtg_pids=$(get_pids "$P4DTGBIN")
      done
      echo -e "\n\nShutdown of P4DTG confirmed.\n"
   ;;

   restart)
      $0 stop
      sleep 1
      $0 start
   ;;

   status)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -n "$p4dtg_pids" ]]; then
         echo "${P4DTGBIN} is running as pids: $p4dtg_pids."
         exit 0
      else
         echo "${P4DTGBIN} is NOT running."
         exit 1
      fi
   ;;

   *)
      echo -e "\nUsage: $0 {start [-f] | stop | status | restart}\n"
      exit 1
   ;;
esac

exit 0