#!/bin/sh
#
# Copyright (c) 2001-2002 by Leni Mayo
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License, version 2, as
#	published by the Free Software Foundation.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
# srcdir may get passed in by autoconf
#
if [ "$srcdir" != "" ]; then
	srcdir=$srcdir"/"
else
	srcdir="/"
fi

srcdir="`pwd`/$srcdir"

# Test the locally built versions, in preference to the installed ones.
# Also, there might not be anything installed as yet (eg. during "make check")
export PATH=../src:../scripts:$PATH

# Source some functions
. $srcdir"testfunctions"

echo -n "Test that dependencies upon non-existent services are removed ... "
directory=$srcdir"5"

tmpfile=`mktemp -q /tmp/XXXXXX`
tmpconfigfile=`mktemp -q /tmp/XXXXXX`
tmpfiles="$tmpfile $tmpconfigfile"

if [ ! -w $tmpfile -o ! -w $tmpconfigfile ]; then
	echo Unable to create temporary file.
	failure
fi

# build the serel config file on the fly.
# We only go though this gynastic because relative pathnames
# are meaningless under autoconf's "make distcheck"
#
cat <<EOF > $tmpconfigfile
# Test #5
#
rdfpath = $srcdir/../data/redhat-serel-default.rdf
dbignore = yes
EOF

startServer -c $tmpconfigfile
[ $? != "0" ] && failure
# exit

# always stop the server and remove the tmp files, no matter what
trap "serelc --control stop > /dev/null 2>&1; rm $tmpfiles > /dev/null 2>&1" EXIT

for i in apmd atd autofs crond gpm ipchains iptables keytable local lpd netfs network nfslock portmap pppoe random rawdevices sendmail sshd syslog xfs xinetd; do
	if [ ! -e "$directory/$i" ]; then
		# echo ln -s "$directory"/anacron "$directory"/$i
		ln -s "$directory"/anacron "$directory"/$i
	fi
done

SEREL_PAIRS=
for i in "$directory"/*; do
	if [ ! -d $i ]; then
		service=`basename $i`
		SEREL_PAIRS="$SEREL_PAIRS $service $i" 
	fi
done

serelhistory --populate << END
--pairs
$SEREL_PAIRS
END
[ $? != "0" ] && failure

serelhistory --query verify | grep "success" 2>&1 > /dev/null
[ $? != "0" ] && failure

for i in "$directory"/*; do
	[ ! -d $i ] && serel-wrap $i start &
done

wait

testStatus $directory "eStateIn"

serelc --query graph > $tmpfile
[ $? != "0" ] && failure

# Test that there's no dependency upon kudzu
#
grep -q edge-kudzu-keytable $tmpfile
[ $? == "0" ] && failure

# Test that local depends on keytable
#
grep -q edge-keytable-local $tmpfile
[ $? != "0" ] && failure

# the trap stops the server and removes the tmp files

success
