#!/bin/bash
#------------------------------------------------------------------------------
# Copyright (c) Perforce Software, Inc., 2007-2014. All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1  Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE
# SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#------------------------------------------------------------------------------

LOG=$LOGS/p4verify.log
STATUS="OK: All scanned depots verified OK."
EXIT_CODE=0
 
export P4CLIENT=p4admin_verify_client
P4="/p4/${P4INSTANCE}/bin/p4_${P4INSTANCE} -p ${P4PORT} -u $P4USER -c $P4CLIENT"
 
/p4/common/bin/p4login
 
echo If there are errors in this log, contact consulting@perforce.com > $LOG
 
# Verify each depot of type 'local', ignoring remote depots.
for d in `$P4 depots|grep local| grep -v unload | cut -d ' ' -f2`; do
    sed s:EDITME_DEPOT:$d:g /p4/common/bin/p4admin_verify_client.txt > client_${P4INSTANCE}.txt
    $P4 client -i < client_${P4INSTANCE}.txt >> $LOG
    rm -f client_${P4INSTANCE}.txt >> $LOG
    echo === Started verify of //$d/... at $(date). >> $LOG
    echo perl /p4/common/bin/p4verify.pl //$d/... "$P4"  >> $LOG 2>&1
    perl /p4/common/bin/p4verify.pl //$d/... "$P4"  >> $LOG 2>&1
done

for d in `$P4 depots | grep unload | cut -d " " -f 2`; do
   echo === Started verify of //$d/... at $(date). >> $LOG
   echo $P4 -s verify -U -q //$d/... >> $LOG
   if [[ "${P4REPLICA}" == "FALSE" ]]; then
      $P4 -s verify -U -q //$d/... >> $LOG 2>&1
   else
      $P4 -s verify -U -q -t //$d/... >> $LOG 2>&1
   fi

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

done

$P4 client -d $P4CLIENT >> $LOG
 
if [[ -n "$(grep BAD $LOG)" || -n "$(grep MISSING $LOG)" || -n "$(grep -i MAX $LOG)" ]]; then
    STATUS="Warning: Verify errors detected.  Review the log [$LOG]."
    EXIT_CODE=1
fi
 
echo Completed verifications at $(date). >> $LOG
 
mail -s "$HOSTNAME $P4SERVER P4Verify Log ($STATUS)" $MAILTO < $LOG
 
exit $EXIT_CODE
