#!/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
#------------------------------------------------------------------------------
export SDP_INSTANCE=${SDP_INSTANCE:-Undefined} 
export SDP_INSTANCE=${1:-$SDP_INSTANCE} 
if [[ $SDP_INSTANCE == Undefined ]]; then 
   echo "Instance parameter not supplied." 
   echo "You must supply the Perforce instance as a parameter to this script." 
   exit 1 
fi 
. /p4/common/bin/p4_vars $SDP_INSTANCE
. /p4/common/bin/backup_functions.sh

LOGFILE=$LOGS/p4verify.log
STATUS="OK: All scanned depots verified OK."
declare -i EXIT_CODE=0
 
P4="$P4BIN -p $P4PORT -u $P4USER"
 
/p4/common/bin/p4login
 
echo If there are errors in this log, contact support@perforce.com > $LOGFILE
 
verifyfailed=0

# Verify all depots of all types except 'remote' and 'archive'.
for d in `$P4 -s depots|grep "^info: Depot " |\
   grep -v --perl-regexp "^info: Depot \S+ \d{4}\/\d{2}\/\d{2} (remote|archive|unload) " |\
   cut -d ' ' -f 3`; do
   echo === Started verify of //$d/... at $(date). >> $LOGFILE
   if [[ "${P4REPLICA}" == "FALSE" || ${SHAREDDATA} == "TRUE" ]]; then
      echo $P4 -s verify -qz //$d/... >> $LOGFILE
      $P4 -s verify -qz //$d/... >> $LOGFILE 2>&1
      if [[ $? -ne 0 ]]; then
         verifyfailed=1
      fi
   else
      echo $P4 -s verify -qz -t //$d/... >> $LOGFILE
      $P4 -s verify -qz -t //$d/... >> $LOGFILE 2>&1
      if [[ $? -ne 0 ]]; then
         verifyfailed=1
      fi
      echo $P4 -s verify -qS //$d/... >> $LOGFILE
      $P4 -s verify -qS //$d/... >> $LOGFILE 2>&1
      if [[ $? -ne 0 ]]; then
         verifyfailed=1
      fi
   fi

   if [[ $verifyfailed -eq 1 ]]; then
      STATUS="Error: Verify attempt failed.  Review the log [$LOGFILE]."
      EXIT_CODE=1
   fi
done

for d in `$P4 -s depots| grep "^info: Depot " | grep --perl-regexp "^info: Depot \S+ \d{4}\/\d{2}\/\d{2} unload" | cut -d " " -f 3 `; do
   echo === Started verify of //$d/... at $(date). >> $LOGFILE
   if [[ "${P4REPLICA}" == "FALSE" || ${SHAREDDATA} == "TRUE" ]]; then
      echo $P4 -s verify -U -q //$d/... >> $LOGFILE
      $P4 -s verify -U -q //$d/... >> $LOGFILE 2>&1
   else
      echo $P4 -s verify -U -q -t //$d/... >> $LOGFILE
      $P4 -s verify -U -q -t //$d/... >> $LOGFILE 2>&1
   fi

   if [[ $? -ne 0 ]]; then
      STATUS="Error: Verify attempt failed.  Review the log [$LOGFILE]."
      EXIT_CODE=1
   fi
done

if [[ $EXIT_CODE -eq 0 ]]; then
   if [[ -n "$(grep BAD! $LOGFILE)" || -n "$(grep MISSING! $LOGFILE)" || -n "$(grep p4\ help\ max $LOGFILE)" ]]; then
      STATUS="Warning: Verify errors detected.  Review the log [$LOGFILE]."
      EXIT_CODE=1
   fi
fi
 
echo Completed verifications at $(date). >> $LOGFILE

mail_log_file "$HOSTNAME $P4SERVER P4Verify Log ($STATUS)"
 
exit $EXIT_CODE
