# /bin/csh
# $Header: dailymain,v 4.300 91/06/09 00:59:26 root Rel41 $
#	Copyright 1990 Sony Corp.
#	Copyright 1990 Performance Systems International Inc.
#       copyright 1988 NYSERNet, Inc.
#	All Rights Reserved.
#       wpc
#
source ../../config/report-env
if (-e $ROOT/config/daily-env) then
  source $ROOT/config/daily-env
else
  echo "$0 : daily-env missing. Can't continue. Exit."
  exit(1)
endif

set 	crntdir=`pwd`

if ($#argv >= 1) then
  set ext=$argv[1]
else
  echo "Usage: $0 mmddyy"
  exit(1)
endif

set nonomatch
if (! $?DAILYREPORTS) then
  cd $ROOT/daily/report
else 
  cd $DAILYREPORTS
endif
if (-e $ext) then
    /bin/rm -rf $ext
endif
mkdir $ext

cd	$DATA 
if (-e $ext) then
    /bin/rm -rf $ext
endif
mkdir $ext
unset nonomatch

# stop trap daemons
# to be uncommented
ps -ax | grep $SNMP/snmptrapd |\
  awk '/grep/ {next;}\
       {printf "kill -9 %s\n\n", $1; exit;}' | /bin/csh

mv $TRAPLOG $ext/$TRAPLOG:t

# create empty files
cat	/dev/null	> $TRAPLOG

# to be uncommented
if ($?SNMPTRAPDCF) then
($SNMP/snmptrapd $SNMPTRAPDCF/snmptrapd.cf & ;echo -n 'snmptrapd restarted')> \
	/dev/console
endif


# check to see if there is no raw data at all
set nonomatch
if (! $?FLATDIR) then
  set wordcount = (`ls $TODAY/* | wc`)
  if ($wordcount[1] == 0) then
    if ($?OPERATER) then
      (echo " in the $TODAY directory.") | \
  	/usr/ucb/mail -s " On $HOST, no polling data is generated " $OPERATER
     endif
     exit
  endif
else
  set wordcount = (`ls $TODAY | wc`)
  if ($wordcount[1] == 0) then
    if ($?OPERATER) then
      (echo " in the $TODAY directory.") | \
  	/usr/ucb/mail -s " On $HOST, no polling data is generated " $OPERATER
     endif
     exit
  endif
endif
unset nonomatch

# move all today's data to $ext subdirectory
if (! $?FLATDIR) then
  mv	$TODAY/*/* $DATA/$ext/
else
  mv	$TODAY/* $DATA/$ext/
endif

cd $BIN
# to generate Daily.Stats, Summary, Tops, LinksDowns, EgpLoss and Reboots files
$BIN/dailystatsmain $ext

# to generate Errors.new and Error.list files
$BIN/newerrs	$ext

# to generate errs file
$BIN/dailyerrs $ext

#prepare graph's of day's data.
if ($?GRAPHDAILY) then
  $ROOT/plot/bin/graphdaily     $ext
endif

# compress the data
$BIN/compressdata $ext
compress	$DATA/$ext/*-*

# the rawdata subdirectory carries only the data that are less than 7 days old.
$BIN/killolddir

# to see how much space available
cd $DATA
if ($?PARTITION && $?OPERATER) then
(df $PARTITION ; echo ""; echo "Kbytes  Directory";du -s ?????? today ) | \
	/usr/ucb/mail -s "Disk used by SNMP report generater on $HOST" $OPERATER
endif
cd $BIN

if (! $?DAILYREPORTS) then
  set reports = $ROOT/daily/report/$ext
else 
  set reports = $DAILYREPORTS/$ext
endif

# copy or move the reports as user requested
if ($?MVDAILYRPT && $?DAILYREPORTDIR) then
  foreach file ($reports/*)
    mv $file $DAILYREPORTDIR/$file:t.$ext
  end
  rmdir $reports
endif

if ($?CPDAILYRPT && $?DAILYREPORTDIR) then
  foreach file ($reports/*)
    cp $file $DAILYREPORTDIR/$file:t.$ext
  end
endif

# tell us where the reports are
if ($?OPERATER) then
  if ($?DAILYREPORTDIR) then
    set where=$DAILYREPORTDIR
  else
    set where=$reports
  endif
  (echo " Daily reports were generated and located in $where directory.") | \
	/usr/ucb/mail -s "$ext SNMP daily reports from $HOST" $OPERATER
endif
