#!/bin/sh
#
# amcleanup.sh - clean up and generate a report after a crash.
#
# @configure_input@
#

# include "config.h"
# include "options.h"
# include "version.h"

# try to hit all the possibilities here
PATH=@libexecdir@:/usr/ucb:/usr/bin:/usr/sbin:/bin:/sbin
export PATH
confdir=@confdir@
execdir=@libexecdir@


suf=@SUF@

if [ $# -ne 1 ]
then
        echo "Usage: amcleanup conf"
        exit 1
fi

conf=$1
if [ ! -d $confdir/$conf ]; then
        echo "amcleanup: could not cd into $confdir/$conf"
        exit 1
fi

cd $confdir/$conf
logfile=`getconf$suf logfile`
errfile=`dirname $logfile`/amdump
tapecycle=`getconf$suf tapecycle`

if [ -f $logfile ]; then
	echo "amcleanup: processing outstanding log file."
	exec </dev/null >/dev/null 2>&1
	$execdir/reporter$suf

	# keep debug log through the tapecycle plus a couple days, up to 50
	days=`expr $tapecycle + 2`

	if [ $days -gt 50 ]; then
		days=50
	fi

	while [ $days -ge 2 ]; do
		ndays=`expr $days - 1`
		mv $errfile.$ndays $errfile.$days
		days=$ndays
	done
	mv $errfile $errfile.1

else
	echo "amcleanup: no unprocessed logfile to clean up."
	exit 1
fi

exit 0
