#!/bin/bash
#
# Run a lightweight verify to find missing head revs in //domino and queue retransfer.
# Exits immediately if lockfile exists (prevents overlapping runs).
#
if [[ "$#" -lt 2 ]]; then
echo -e "Two parameters are required for this script."
echo -e "The first is the instance number for the Perforce server."
echo -e "The second is a file with a list of directories to verify in Perforce format"
echo -e "ie:\n//depot/path1/...\n//depot/path2/...\n//depot/path3/...\nand so on."
exit 1
fi
export SDP_ENV=/p4/common/bin/p4_vars
export SDP_INSTANCE=${SDP_INSTANCE:-Unset}
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
function msg () { if [[ $Log != Unset ]]; then echo -e "$*" >> $Log; else echo -e "$*"; fi; }
function bail () { msg "\nError: ${1:-Unknown Error}"; exit ${2:-1}; }
source $SDP_ENV $SDP_INSTANCE ||\
bail "Failed to load SDP environment for instance $SDP_INSTANCE."
LOCKFILE=$LOGS/p4_verify.lock
LOGFILE=$LOGS/p4_verify.log
if [[ ! -f $LOGS/lastcl.txt ]]; then
p4 -ztag -F %change% changes -s submitted -m1 > $LOGS/lastcl.txt
fi
export LASTCL=$(cat $LOGS/lastcl.txt)
# Update lastcl.txt for the next run.
p4 -ztag -F %change% changes -s submitted -m1 > $LOGS/lastcl.txt
# If another run is in progress, bail out quietly.
if [ -e "$LOCKFILE" ]; then
echo "$(date -u +'%Y-%m-%dT%H:%M:%SZ') skipped: lock present ($LOCKFILE)" >>"$LOGFILE"
exit 0
fi
# Create lock and ensure it gets removed on exit.
echo $$ >"$LOCKFILE" || exit 1
trap 'rm -f "$LOCKFILE"' EXIT INT TERM
while IFS="" read -r line || [ -n "$line" ]
do
echo "$(date -u +'%Y-%m-%dT%H:%M:%SZ') running: p4 verify -qt --only MISSING ${line}@${LASTCL},@now" >>"$LOGFILE"
p4 verify -qt --only MISSING ${line}@${LASTCL},@now > /dev/null 2>>$LOGFILE
done < $2
rc=$?
echo "$(date -u +'%Y-%m-%dT%H:%M:%SZ') done rc=$rc" >>"$LOGFILE"
exit $rc