#!/bin/bash
export SDP_INSTANCE=${SDP_INSTANCE:-Undefined}
export SDP_INSTANCE=${1:-$SDP_INSTANCE}
export NUM_SHELVES=${2:-Undefined}
if [[ $SDP_INSTANCE == Undefined ]]; then
   echo "Instance parameter not supplied."
   echo "You must supply the Perforce instance as parameter 1 to this script."
   exit 1
fi

if [[ $NUM_SHELVES == Undefined ]]; then
   echo "Number of shelves to verify parameter not supplied."
   echo "You must supply the number of shelves to verify as parameter 2 to this script."
   exit 1
fi

# shellcheck disable=SC1091
source /p4/common/bin/p4_vars "$SDP_INSTANCE"
# shellcheck disable=SC1091
source /p4/common/bin/backup_functions.sh
# shellcheck disable=SC2034
LOGFILE="$LOGS/verify_shelves.log"

rm -f $LOGFILE

######### Start of Script ##########
for change in $(p4 -ztag -F %change% changes -s shelved -m${NUM_SHELVES}); do
  echo "Verifying shelf $change" >> $LOGFILE
  p4 verify -qSt @=$change >> $LOGFILE 2>&1
done
