#!/bin/sh
#
# (C) Michael Meskes <meskes@debian.org>
# Placed under GPL
#

#
# some constants
#
PIPE=/etc/tripwire/tw.fifo
DATABASEGZIP="/usr/lib/tripwire/databases/tw.db_`hostname`.gz"
NEWDATABASE="./databases/tw.db_`hostname`"
old=no
pipe=no

#
# parse arguments
#
oldcommandline=$*
newcommandline=
while [ ! -z $1 ]
do
    case $1 in
	-init|-initialize)	# not much to do for init
		old=yes;
		shift 1;;
	-dfd)	# file already open, no need to zcat it
		old=yes;
		shift 2;;
	-d)	# use given file for zcat and remove option
		DATABASEGZIP=$2;
		shift 2;;
	-pipe)  # use a non-default pipe
		pipe=yes;
		PIPE=$2;
		shift 2;
		[ -e $PIPE ] && /bin/rm -f $PIPE;
		mkfifo $PIPE;;		
	*)	# just use it
		newcommandline="$newcommandline $1";
		shift 1;;
    esac
done

#
# do we have to acces the gzipped database?
#
if [ $old != "yes" ]
then
	if [ $DATABASEGZIP != "-" -a ! -f $DATABASEGZIP ]
	then
	 echo "couldn't open $DATABASEGZIP" > 2
	 exit 1
	fi

	#
	# pipe database to tripwire
	#
	zcat $DATABASEGZIP > $PIPE &

	#
	# and start it
	#
	/usr/lib/tripwire/tripwire -dfd 147 $newcommandline 147<$PIPE
else
	/usr/lib/tripwire/tripwire $oldcommandline;
fi

#
# store the original return value
#
return=$?

#
# compress new database
#
[ -f $NEWDATABASE ] && gzip -f $NEWDATABASE;

#
# remove pipe if we created it
#
[ $pipe = "yes" ] && /bin/rm -f $PIPE

#
# and return the value we got from tripwire
#
exit $return
