#!/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 overlapping service defns, unknown services, serel:parse, X-serel-late and X-serel-last ... "
directory=$srcdir"4"

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

if [ ! -w $tmpfile -o ! -w $tmpconfigfile -o ! -w $tmpfileFirst -o ! -w $tmpfileLast ]; 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 #4
#
rdfpath = $tmpfileFirst;$directory/conf/local.rdf;$directory/conf/default.rdf;$tmpfileLast
dbdirectory = $directory/data/
mangledollar = strip
EOF

cat <<EOF > $tmpfileFirst
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:serel="http://www.fastboot.org/2002/03/serel-schema#" 
>
<rdf:Description about="file:/$directory/syslog" serel:parse="true" />
</rdf:RDF>
EOF

cat <<EOF > $tmpfileLast
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:serel="http://www.fastboot.org/2002/03/serel-schema#" 
>
<rdf:Description about="file:/$directory/syslog"  serel:parse="false" />
<rdf:Description about="file:/$directory/anacron" serel:parse="false"  />
</rdf:RDF>
EOF

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

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

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
--started
hwclock
--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"

# The expected dependency graph is:
#
#                       /-->  apmd   ---\ /-->  xdm       --\
#  start -->  syslog ---                                     --> foo --> local
#        \              \--> anacron ---/ \-->  rmnologin --/
#         \
#          \--> hwclock
#

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

# foo depends on xdm and rmnologin
#
grep -q edge-xdm-foo $tmpfile
[ $? != "0" ] && failure

grep -q edge-rmnologin-foo $tmpfile
[ $? != "0" ] && failure

# xdm depends on apmd and anacron
#
grep -q edge-apmd-xdm $tmpfile
[ $? != "0" ] && failure

grep -q edge-anacron-xdm $tmpfile
[ $? != "0" ] && failure

# rmnologin depends on apmd and anacron
#
grep -q edge-apmd-rmnologin $tmpfile
[ $? != "0" ] && failure

grep -q edge-anacron-rmnologin $tmpfile
[ $? != "0" ] && failure

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

# no-one depends on hwclock
#
grep -q edge-hwclock- $tmpfile
[ $? == "0" ] && failure

# the trap stops the server and removes the tmp files

success
