#!/bin/bash
# This script will remove all by the latest file in your /p4/1/depots/ tree.
# It is designed to work with servers running with lbr.autocompress=1 set in the configurables.
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

if [[ -f /p4/${SDP_INSTANCE}/bin/skip_cache_clean.txt ]]; then
  exit 0
fi


IFS=$'\n' depots=($(ls "/p4/${SDP_INSTANCE}/cache/"))
for depot in "${depots[@]}"
do
  if [[ !($depot =~ pdb.monitor) ]]; then
    find /p4/${SDP_INSTANCE}/cache/$depot/ -atime +10 -type f -exec rm {} \; & 
  fi
done
