#
#   Copyright (C) 1997, 1998
#   	Free Software Foundation, Inc.
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the
#   Free Software Foundation; either version 2, or (at your option) any
#   later version.
#
#   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.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
#
init_sql() {
$mysql -e "drop database $testdb" 
$mysql -e "create database $testdb" 
$crawler -base $testdb -create
rm -fr $WLROOT
mkdir -p $WLROOT
}

testing() {
  if [ ! -d t ] ; then mkdir t ; fi
  error=0
  for name in $*
  do
    echo "testing $name"
    if [ "$init" ]
    then 
      eval $name 2>&1 | perl -p -e "s/sleep [0-9][0-9]*s/sleep Ns/; s/\(0h:.*\)//; s'$ftphost'FTPHOST'g;" > t/$name.expect
    else
      if [ "$debug" ]
      then
        $name
      else
        eval $name 2>&1 | perl -p -e "s/sleep [0-9][0-9]*s/sleep Ns/; s/\(0h:.*\)//; s'$ftphost'FTPHOST'g;" > t/$name.out
        diff t/$name.out t/$name.expect
	if [ -f core ]
	then
		echo "core file"
		exit 1
	fi
	if [ $? -gt 0 ]
	then
		error=$?
	fi
      fi
    fi
  done
  if [ -f logs/httpd.pid ] ; then kill -15 `cat logs/httpd.pid` ; fi
  exit $error
}

#
# Check configuration
#
if $mysql -e 'show databases' > /dev/null
then :
else
	echo "fix the check/config file: Cannot connect to database"
	exit 1
fi
if [ ! -f ../bin/consistentc ]
then
	echo "../bin/consistentc does not exist: make all before check"
	exit 1
fi
if $httpd -v | grep Apache > /dev/null
then :
else 
	echo "fix the check/config file: $httpd is not an Apache server"
	exit 1
fi
init_sql
if $mysql -e 'show tables' $testdb | grep url_complete > /dev/null
then :
else
	echo "fix the check/config file: Cannot create and fill $testdb"
	exit 1
fi

#
# Prepare http server
#
( cd conf ; make user="$user" testdir="$testdir" all > /dev/null )
if [ ! -d logs ] 
then 
  mkdir -p logs
else
  kill -15 `cat logs/httpd.pid`
  sleep 2
fi
$httpd -d `pwd`

#
# Prepare data
#
perl -pi -e "s|^AuthUserFile.*|AuthUserFile $testdir/htdocs/protected/htpasswd|" htdocs/protected/.htaccess

