default=100000
file=.Data/.Audit
savefile=$HOME/Save.Audit
if ps | grep -v grep | grep -s Sqpe
	then echo "Do not run TRUNC_AUDIT while S is running"; exit 1;
	fi
if test ! -f $file
	then
	file=$HOME/.Data/.Audit
	fi
if test ! -r $file
	then echo "Can not read audit file"; exit 1
	fi
echo "Truncating audit file ($file) to most recent ${1-$default} characters"
if test "$1x" -ne x -a "$1" -eq 0
	then : >$file
	echo "Audit file reset"; exit 0
	fi 
if test -r $savefile
	then echo "Already have a $savefile"; exit 1
	fi
filesize=`wc -c $file|sed -e "s/^ *//" -e "s/ .*//"`
chopat=`expr $filesize - ${1-$default}`
if test $chopat -lt 0
	then echo "Audit file is currently only $filesize characters"; exit 0
	fi
sleep 10
tail +${chopat}c $file |
	sed "1,/^#/d" |
	sed -n '/^[^#]/,$p' >$savefile ||
	( echo "editing failed, audit file not changed"; rm $savefile; exit 1 )

size=`wc -c $savefile|sed -e "s/^ *//" -e "s/ .*//"`
if test `expr ${1-$default} - $size` -gt 2000
	then
		echo "Problem with truncating: you requested ${1-$default}"
		echo "Processing produced $size -- Audit file not changed"
		rm $savefile
		exit 1
	fi
echo "Audit file truncated to $size characters"
mv $savefile $file
