#
# Script to compress and move Helix Server structured audit logs
#
# (C) 2015 Perforce Software
#
# Implementation assumptions and suggestions:
# - Assumes the rotated log files are named audit-nnn.csv
# - Do NOT configure your log files to be placed in $P4ROOT
# - Set TARGETDIR below
#
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

. /p4/common/bin/p4_vars $SDP_INSTANCE

HTDLOG=${P4LOGS}/htd_move.log
TARGETDIR=/data1/ingest

$P4BIN logrotate

numfiles=$(ls ${P4LOGS}/audit-*.csv 2> /dev/null | wc -l)
echo "$numfiles log files found" > $HTDLOG
if [ "$numfiles" -gt 0 ];then
  for EACH in `ls ${P4LOGS}/audit-*.csv`
  do
    echo -n "${EACH}: Compressing... " >> $HTDLOG
    gzip -9 ${EACH}
    echo -n "Moving..." >> $HTDLOG
    mv ${EACH}.gz ${TARGETDIR}
  done
fi

echo "Done!" >> $HTDLOG

