#!/bin/bash
#------------------------------------------------------------------------------
set -u

function msg () { echo -e "$*"; }
function warnmsg () { msg "\nWARNING: ${1:-Unknown Warning}\n"; WarningCount+=1; }
function errmsg () { msg "\nERROR: ${1:-Unknown Error}\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "${2:-1}"; }

#------------------------------------------------------------------------------
# Functions run($cmd, $desc)
#
# This function is similar to functions defined in SDP core libraries, but we
# need to duplicate them here since this script runs before the SDP is
# available on the machine (and we require no external dependencies for this
# script).
function run {
   cmd="${1:-echo Testing run}"
   desc="${2:-}"
   [[ -n "$desc" ]] && msg "$desc"
   msg "Running: $cmd"
   if [[ "$NoOp" -eq 0 ]]; then
      $cmd
   else
      msg "NO_OP: Would execute: $cmd"
   fi
   return $?
}

#------------------------------------------------------------------------------
# Declarations and Environment

declare ThisScript="${0##*/}"
declare Version=1.1.0
declare -i ErrorCount=0
declare -i WarningCount=0
declare -i ResetNeeded=0
declare -i NoOp=1
declare CustomResetScript="${HMS_CUSTOM_RESET_SCRIPT:-}"
declare ThisUser=

#------------------------------------------------------------------------------
# Command Line Parsing

[[ "$*" == "-y" ]] && NoOp=0

#------------------------------------------------------------------------------
# Main Program

ThisUser=$(whoami)

msg "Started $ThisScript v$Version as $ThisUser@${HOSTNAME%%.*} at $(date)."

msg "\nStopping running hms services (if running)."
for bin in p4d p4broker; do
   svc=${bin}_hms
   initScript=/p4/hms/bin/${bin}_hms_init
   if [[ -x "$initScript" ]]; then
      ResetNeeded=1
      if systemctl cat "$svc" > /dev/null; then
         run "sudo systemctl stop $svc" "Stopping with: sudo systemctl stop $svc" ||\
            warnmsg "Failed to stop with: sudo systemctl stop $svc"
      else
         run "$initScript stop" "Stopping with: $initScript stop." ||\
            warnmsg "Failed to stop with: $initScript stop"
      fi
   fi
done

#msg "\nDisabling and removing systemd services (if any)."
#for systemdService in p4d_hms p4broker_hms p4p_hms; do
#   systemdServiceFile="/etc/systemd/system/${systemdService}.service"
#   if [[ -e "$systemdServiceFile" ]]; then
#      ResetNeeded=1
#      #run "sudo systemctl disable $systemdService" ||\
#      #   errmsg "Failed to disable systemd service $systemdService."
#      #run "sudo rm -f $systemdServiceFile" ||\
#      #   errmsg "Failed to remove systemd service file: $systemdServiceFile"
#   fi
#done

msg "\nRemoving /mnt/p4*/p4/hms and /p4/hms folders if they exist."
for d in /mnt/p4*/hms /p4/hms/; do
   if [[ -d "$d" ]]; then
      ResetNeeded=1
      run "rm -rf $d" "Removing dir: $d" ||\
         errmsg "Failed to remove dir: $d"
   else
      msg "No dir to remove: $d" 
   fi
done

msg "\nRemoving hms-specific files (if any)."
for f in /p4/common/config/p4_hms.* /p4/.p4config.SDP /p4/common/bin/p4*_hms_* /p4/sdp/Server/Unix/setup/mkdirs.hms.*; do
   if [[ -e "$f" ]]; then
      ResetNeeded=1
      run "rm -f $f" "Removing file $f" ||\
         errmsg "Failed to remove file: $f"
   else
      msg "No file to remove: $f" 
   fi
done

if [[ -x "$CustomResetScript" ]]; then
   run "$CustomResetScript" \
      "Executing custom reset script specified with \$HMS_CUSTOM_RESET_SCRIPT." ||\
      errmsg "Custom reset script returned non-zero exit code."
else
   msg "No custom reset script found."
fi

if [[ "$ErrorCount" -eq 0 ]]; then
   if [[ "$WarningCount" -eq 0 ]]; then
      if [[ "$ResetNeeded" -eq 1 ]]; then
         if [[ "$NoOp" -eq 0 ]]; then
            msg "\nSUCCESS:  HMS has been reset."
         else
            msg "\nSUCCESS:  HMS would have been reset (was not without -y)."
         fi
      else
         msg "\nSUCCESS:  HMS reset was not needed."
      fi
   else
      msg "\nSUCCESS:  HMS has been reset, but with $WarningCount warnings."
   fi
else
   msg "\nHMS reset has completed, but with $ErrorCount errors and $WarningCount warnings."
fi

[[ "$NoOp" -eq 1 && "$ResetNeeded" -eq 1 ]] && warnmsg "This was preview mode; reset did not actually occur.  Specify '-y' to perform an actual reset.\n"

exit "$ErrorCount"
