#!/bin/bash
# ==========
# $File: //guest/amo/scripts/proxyload.sh $
# $Revision: #1 $
# $Change: 24265 $
#
# $Author: amo $
# $DateTimeTZ: 2018/06/27 08:42:40 -0700 PDT $
# ----------
# Bash script called by a p4d bgtask, intended to update the proxy cache.
#
# Takes three parameters.
# user: a user with protections necessary to sync files from the specified area
# port: the P4PORT value where the proxy can be found
# client: a client workspace that maps the files to be sync'd
# path: the top-level depot path to be sync'd
#
# The script runs 'p4 -Zproxyload sync @n' using the above details, where 'n' is the value of a counter.
# The counter is incremented, and next run the next revision should be sync'd.
#
# ----------
# Example trigger entry:
# proxyload bgtask bgtask "%//scripts/triggers/proxyload.sh% amorriss myServer:3666 proxyload //..."
#
# Example bgtask command:
# p4 bgtask -d -i5 -m100 -t proxyload
#
# ----------
# Next steps:
# - better (or just 'some') error handling
# - logging; what's the script actually doing, what's the current counter value, timestamps, etc.
#
# ==========
user=$1
port=$2
cli=$3
path=$4
# messages
usage="USAGE: $0 user port client path"
nocli="ERROR: 'p4' client not found"
nocon="ERROR: 'p4' not logged in or no connection available"
# checking parameters provided
if [ "$#" -eq 0 ]; then printf "%s\n" "$usage"; exit 1; fi
# check we have 'p4' available
p4cli=`which p4`
if [ "$?" -ne 0 ]; then printf "%s\n%s\n" "$usage" "$nocli"; exit 2; fi
# client exists, create the 'connection string' and test for a connection.
p4con="$p4cli -u $user -p $port -c $cli"
$p4con login -s > /dev/null 2>&1
if [ $? -ne 0 ]; then printf "%s\n%s\n" "$usage" "$nocon"; exit 3; fi
# define the counter update command
count=`$p4con counter -i proxyload`
# proxyload-sync, at counter
$p4con -Zproxyload sync -q $path@$count