#!/bin/ksh

PATH=$PATH:/usr/openv/netbackup/bin/admincmd:/usr/openv/local
 OUTFILE=/var/tmp/`basename $0`.$$.out
TMPFILE1=/var/tmp/`basename $0`.$$.tmp1
TMPFILE2=/var/tmp/`basename $0`.$$.tmp2
 LOGFILE=/var/tmp/`basename $0`.errlog

MAILADDR="XXXX@XX.com"

##HS hours=19
hours=24

if [ -n "$1" ] 
then
  if [ $1 -gt 0 ]
  then
    hours=$1
  else
    echo "Usage: `basename $0` [hours]"
    exit 1
  fi
fi
echo "## Report Start: `date` for past $hours hours." >$OUTFILE
exec >>$OUTFILE 2>&1

bpimagelist -hoursago $hours | awk '$1=="IMAGE" {print $2,$7,$19}' | sort -u >$TMPFILE1

echo "## General Statistics"

awk 'BEGIN {nj=0;sum=0}
           {nj++ ; sum=sum+($3/1024)}
       END {printf("\tTotal Backup Jobs: %d\n",nj)
           if (sum<1024) { printf("\tTotal Volume: %.1f GB\n",sum/1024)}
           else {printf("\tTotal Volume: %.2f TB\n",sum/1024/1024)}}' $TMPFILE1

echo "## Backup totals by client" 

bpplclients -allunique -noheader|awk '{print $3}' >$TMPFILE2
awk '{print $1}' $TMPFILE1 | sort | uniq >>$TMPFILE2
sort -u -o $TMPFILE2 $TMPFILE2

for client in `cat $TMPFILE2`
do
  awk 'BEGIN {sum=0;OFMT="%8.1f";print}
    {if ($1=="'$client'") {sum=sum+$3}}
    END {  if (sum==0) {printf("%s = null\n","'$client'")} else {
           if (sum<1024) {
            printf("%s = %.1f KB\n","'$client'",sum)
                       } else {
          if (sum<1048576) {
            printf("%s = %.1f MB\n","'$client'",sum/1024)
                           } else {
            printf("%s = %.1f GB\n","'$client'",sum/1024/1024)
                }}}}' $TMPFILE1

  for policy in `awk '$1=="'$client'" {print $2}' $TMPFILE1 | sort -u`
  do
    awk 'BEGIN {nj=0;sum=0;OFMT="%8.1f"}
      {if ($1=="'$client'" && $2=="'$policy'" ) {sum=sum+$3;nj++}}
      END { if (sum<1024) {
              printf ("\t:%24s %6.1f KB (%d jobs)\n","'$policy'",sum,nj)
                         } else {
            if (sum<1048576) {
              printf ("\t:%24s %6.1f MB (%d jobs)\n","'$policy'",sum/1024,nj)
                             } else {
              printf ("\t:%24s %6.1f GB (%d jobs)\n","'$policy'",sum/1024/1024,nj)
                  }}}' $TMPFILE1
  done 
done

echo "## Backup totals by policy"
bppllist >$TMPFILE2
awk '{print $2}' $TMPFILE1 | sort | uniq >>$TMPFILE2
sort -u -o $TMPFILE2 $TMPFILE2

for policy in `cat $TMPFILE2`
do
  awk 'BEGIN {sum=0;OFMT="%8.1f"}
    {if ($2=="'$policy'" ) {sum=sum+$3}}
    END { if (sum<1024) {
            printf ("%24s: %8.1f KB\n","'$policy'",sum)
                       } else {
          if (sum<1048576) {
            printf ("%24s: %8.1f MB\n","'$policy'",sum/1024)
                           } else {
            printf ("%24s: %8.1f GB\n","'$policy'",sum/1024/1024)
                }}}' $TMPFILE1
  done
  


echo "## Report Complete" 
echo "## Report Generated by \"$0\"." 

mailx -s "Net_Backup_Total_Report: ${hours}hr Backup Totals" $MAILADDR <$OUTFILE

[ -f $OUTFILE ]  && rm -f $OUTFILE
[ -f $TMPFILE1 ] && rm -f $TMPFILE1
[ -f $TMPFILE2 ] && rm -f $TMPFILE2
exit
