# hms_load_and_verify.sh
Version=1.0.3

#==============================================================================
# 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
#------------------------------------------------------------------------------

#==============================================================================
# HMS Library Functions.

#------------------------------------------------------------------------------
# Function: load_helix_topology
#
# Input:
# $1 - Helix Topology file path
# $2 - Helix Topology file required format version.
#
# The following global arrays are updated:
#
# * InstanceName array
# * InstanceUserPorts array
# * InstanceMasterHost array
# * InstanceServerPort array
# * InstanceBrokerPort array
# * InstanceManaged array
# * InstanceCount integer
#
# Instance definition:
# Form: INSTANCE|Name|UserPorts|MasterHost|ServerPort|BrokerPort|Managed|Desc
# Example:
# INSTANCE|fgs|perforce:1666|helix-01|1999|1666|Friendly Greeting Systems
#
# Return:
# 0 - Instances loaded, syntax verified.
# 1 - Errors detected.
#------------------------------------------------------------------------------
function load_helix_topology {
   vvmsg "CALL: load_helix_topology($*)"

   declare cfgFile=$1
   declare reqVersion=$2
   declare cfgVersion=
   declare instanceName=
   declare instanceUserPorts=
   declare instanceMasterHost=
   declare instanceServerPort=
   declare instanceBrokerPort=
   declare instanceManaged=
   declare instanceDesc=
   declare failoverName=
   declare failoverType=
   declare failoverMasterHost=
   declare failoverBackupHost=
   declare failoverInstances=
   declare failoverActive=
   declare line=
   declare -a errors
   declare -i errorCount=0
   declare -i i=0
   declare -i lineCount=0

   msg "Loading instances from [$cfgFile]."

   if [[ ! -r "$cfgFile" ]]; then
      errmsg "Cannot read Helix Topology file [$cfgFile]."
      return 1
   fi

   # The first action is artifically hard-coded to START_IMPORT.

   while read line; do
      lineCount+=1
      case $line in
         (\#*|NAME*) continue;;
         (GLOBAL_OPTIONS*)
            GlobalOptions=$(echo $line|cut -d '=' -f 2)
         ;;
         (VERSION*)
            cfgVersion=$(echo $line | cut -d '=' -f 2)
            if [[ "$cfgVersion" > "$reqVersion" ]]; then
               msg "Verified:  Helix Topology file version [$cfgVersion] meets minimum requirement."
            else
               errors[$errorCount]="Helix Topology file version [$cfgVersion] is too old.  Must be at least ${reqVersion}.0."
               errorCount+=1
            fi
         ;;
         (INSTANCE*)
            # INSTANCE|Name|UserPorts|MasterHost|ServerPort|BrokerPort|Managed|Desc
            instanceName="$(echo $line | cut -d '|' -f 2)"
            instanceUserPorts="$(echo $line | cut -d '|' -f 3)"
            instanceMasterHost="$(echo $line | cut -d '|' -f 4)"
            instanceServerPort="$(echo $line | cut -d '|' -f 5)"
            instanceBrokerPort="$(echo $line | cut -d '|' -f 6)"
            instanceManaged="$(echo $line | cut -d '|' -f 7)"
            instanceDesc="$(echo $line | cut -d '|' -f 8)"
            InstanceName[$InstanceCount]=$instanceName
            InstanceUserPorts[$InstanceCount]=$instanceUserPorts
            InstanceMasterHost[$InstanceCount]=$instanceMasterHost
            InstanceServerPort[$InstanceCount]=$instanceServerPort
            InstanceBrokerPort[$InstanceCount]=$instanceBrokerPort
            InstanceManaged[$InstanceCount]=$instanceManaged
            InstanceDesc[$InstanceCount]=$instanceDesc
            InstanceCount+=1
         ;;
         (FAILOVER*)
            # FAILOVER|Name|Type|MasterHost|BackupHost|InstanceList|Active|Desc
            failoverName="$(echo $line | cut -d '|' -f 2)"
            failoverType="$(echo $line | cut -d '|' -f 3)"
            failoverMasterHost="$(echo $line | cut -d '|' -f 4)"
            failoverBackupHost="$(echo $line | cut -d '|' -f 5)"
            failoverInstanceList="$(echo $line | cut -d '|' -f 6)"
            failoverActive="$(echo $line | cut -d '|' -f 7)"
            failoverDesc="$(echo $line | cut -d '|' -f 8)"
            FailoverName[$FailoverCount]=$failoverName
            FailoverType[$FailoverCount]=$failoverType
            FailoverMasterHost[$FailoverCount]=$failoverMasterHost
            FailoverBackupHost[$FailoverCount]=$failoverBackupHost
            FailoverInstanceList[$FailoverCount]=$failoverInstanceList
            FailoverActive[$FailoverCount]=$failoverActive
            FailoverDesc[$FailoverCount]=$failoverDesc
            FailoverCount+=1
         ;;
      esac
   done < $cfgFile

   msg "${H}\nLoaded ${#InstanceName[*]} instances:\n"

   printf "%-3s %-13s %-16s %-4s %-4s %-1s %-40s %-s\n" "#" "Instance" "Master Host" "P4D" "P4B" "On" "User P4PORTs" "Description"
   printf "%-3s %-13s %-16s %-4s %-4s %-1s %-40s %-s\n" "---" "------------" "---------------" "----" "----" "--" "----------------------------------------" "------------------------------------------------"
   i=0; while [[ $i -lt ${#InstanceName[*]} ]]; do
      printf "%-3d %-13s %-16s %-4s %-4s %-1s %-40s %-s\n" "$((i+1))" "${InstanceName[$i]}" "${InstanceMasterHost[$i]}" "${InstanceServerPort[$i]}" "${InstanceBrokerPort[$i]}" "${InstanceManaged[$i]}" "${InstanceUserPorts[$i]}" "${InstanceDesc[$i]}"
      i+=1
   done

   if [[ $errorCount -eq 0 ]]; then
      msg "\nVerified:  All instances passed sanity checks."
   else
      msg "\nThe following problems were identified:"
      i=0; while [[ $i -lt $errorCount ]]; do
         errmsg "${errors[$i]}"
         i+=1
      done
   fi

   msg "${H}\nLoaded ${#InstanceName[*]} failover options:\n"

   printf "%-3s %-8s %-5s %-16s %-16s %-36s %-1s %-s\n" "#" "Name" "Type" "Master Host" "Backup Host" "Instance List" "On" "Description"
   printf "%-3s %-8s %-5s %-16s %-16s %-36s %-1s %-s\n" "---" "--------" "-----" "----------------" "----------------" "------------------------------------" "--" "----------------------------------------"
   i=0; while [[ $i -lt ${#FailoverName[*]} ]]; do
      printf "%-3s %-8s %-5s %-16s %-16s %-36s %-1s  %-s\n" "$((i+1))" "${FailoverName[$i]}" "${FailoverType[$i]}" "${FailoverMasterHost[$i]}" "${FailoverBackupHost[$i]}" "${FailoverInstanceList[$i]}" "${FailoverActive[$i]}" "${FailoverDesc[$i]}"
      i+=1
   done


   if [[ $errorCount -eq 0 ]]; then
      return 0
   else
      return 1
   fi
}
