#!/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.3 2004/10/11 11:13:57 taru Exp $

set -e

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

export OK=0
export NG=10
export IGNORE=11

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

test_term || true
test_init

if test "x$1" != "x"; then
  tests="$@"
else
  tests="`find $TESTSCRIPTSDIR -type f | sort`"
fi

for e in $tests
do
  echo -n "Testing $e... "
  echo "Running $e" 1>&2
  if $SHAREDIR/envexec $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
