#
#   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. 
#

. /raid5/senga/htdocs/webbase/distrib/webbase/../config
. $srcdir/params

init_sql() {
$mysql -e "drop database $testdb" 
$mysql -e "create database $testdb" 
$crawler -no_hook -base $testdb -create
rm -fr $WLROOT
mkdir -p $WLROOT
rm -f $FULLTEXTDB*
rm -fr db
}

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 [ -n "$debug" ]
      then
        $name
	tmp_error=$?
	if [ $tmp_error -gt 0 ]
	then
		error=$tmp_error
	fi
      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 $srcdir/t/$name.expect
	tmp_error=$?
	if [ $tmp_error -gt 0 ]
	then
		error=$tmp_error
	fi
	if [ -f core ]
	then
		echo "core file"
		exit 1
	fi
      fi
    fi
  done
  if [ -f $srcdir/logs/httpd.pid ] ; then kill -15 `cat $srcdir/logs/httpd.pid` ; fi
  exit $error
}

#
# Check configuration
#
if $mysql -e 'show databases' > /dev/null
then :
else
	echo "fix the ~/.my.cnf 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

mifluz=
langrec=

#
# Check for Apache
#
httpd=/usr/sbin/httpd

if test -z "$httpd"
then
	echo "no apache server provided. Run configure with --with-apache=<httpd path>"
	exit 77
fi

#
# Prepar mysql database
#
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 $srcdir/conf ; make user="loic" modules="lib/apache" testdir="$testdir" all > /dev/null )
if [ ! -d $srcdir/logs ] 
then 
  mkdir -p $srcdir/logs
else
  if [ -f $srcdir/logs/httpd.pid ] ; then kill -15 `cat $srcdir/logs/httpd.pid` ; sleep 2 ; fi
fi

$httpd -f $testdir/conf/httpd.conf

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

rm -f core
