#!/usr/bin/env bash
#
# Copyright (C) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
#  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 of the License, 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.
#

# $Id: runtest,v 1.5 2004/04/02 02:34:28 taru Exp $

set -e

INCLUDE_TEST=${INCLUDE_TEST:-/}

eval `ultrapossum-config init`

. $TESTDIR/test.cf

TMPFILE=`tempfile`
export TMPFILE
trap "/bin/rm -f $TMPFILE; eval `ultrapossum-config term`" 0

OK=0
NG=10
IGNORE=11
 
export OK NG IGNORE
 
### Export Variables
export ORGANIZATION
export SUFFIX
export ROOTDN
export ROOTPW
export DIRECTORY
export UPDATEDN
export MASTER
export BACKUP
export VIRTUAL
export SLAVES
export CREDENTIALS
export LOADBALANCER
export NFSDIR
export RPLDIR
export RPLOCK
export RPLOCKDIR

export LDAPMASTER
 
export HOST

export ULTRAPOSSUMCF

test_init() {

  echo $SUFFIX
  if test `$MODULEDIR/server/startup status` != "running"; then
    addcmd="slapadd -b $SUFFIX -f $SLAPDMASTERCONF"
  else
    addcmd="ldapadd -x -D $ROOTDN -w $ROOTPW"
  fi
  echo "
dn: $SUFFIX
objectClass: top
objectClass: organization
o: $TESTORGANIZATION
" | $addcmd || true
}
 
test_term() {
  if test `$MODULEDIR/server/startup status` != "running"; then
    $MODULEDIR/server/startup start
    tmpstart=1
  fi
  ldapdelete -r -x -D $ROOTDN -w $ROOTPW $SUFFIX
  if test "$tmpstart" = "1"; then
    $MODULEDIR/server/startup stop
  fi
}

ldapdelete -r -x -D $ROOTDN -w $ROOTPW $SUFFIX 2>&1 /dev/null || true
test_init

tests=`find $TESTSCRIPTSDIR -type f | sort | grep $INCLUDE_TEST`
for e in $tests
do
  echo -n "Testing $e... "
  if $e 1>&2; then
    echo "ok"
    test_term
    test_init
  else
    e=$?
    case $e in
      $IGNORE)
        echo "ignored"
       ;;
      $NG)
        echo "failed"
        exit 1
       ;;
      *)
        echo "error: $e"
        exit $e
       ;;
    esac
  fi
done
test_term
