#!/usr/bin/env bash
#
# Sample Test Script for UltraPossum
#
# 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.
#

set -e

if test "x$MASTER" != "x$HOST" || test "x$BACKUP$SLAVES" = "x"; then
  echo "Ignoring $0"
  exit $IGNORE
fi

if test "x$DEBUG" != "x"; then
  set -x
fi

eval `ultrapossum-config init` 2> /dev/null

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

########### Add Test

cat >$tmp <<EOF
dn: ou=add,$SUFFIX
objectClass: top
objectClass: organizationalUnit
ou: add

EOF

ldapadd -x -w $ROOTPW -D $ROOTDN -h $HOST -f $tmp
ldapsearch -x -w $ROOTPW -LLL -D $ROOTDN -h $HOST -b $SUFFIX ou=add > $tmp2
diff -c $tmp $tmp2

sleep 5

for host in $BACKUP $SLAVES
do
  echo "Checking for $host..."
  ldapsearch -x -w $ROOTPW -LLL -D $ROOTDN -h $host -b $SUFFIX ou=add > $tmp2
  diff -c $tmp $tmp2
done

############ Modify Test

cat >$tmp <<EOF
dn: cn=people,$SUFFIX
objectClass: top
objectClass: person
cn: people
userPassword: old
sn: people
EOF

ldapadd -x -w $ROOTPW -D $ROOTDN -h $HOST -f $tmp

# modify data
cat >$TMPFILE <<EOF
dn: cn=people,$SUFFIX
changetype: modify
replace: userPassword
userPassword: new
EOF

ldapmodify -x -w $ROOTPW -D $ROOTDN -h $HOST -f $TMPFILE 
ldapsearch -x -L -b $SUFFIX -h $HOST -D "cn=people,$SUFFIX" -w new cn=people userPassword > $tmp

sleep 5

for host in $BACKUP $SLAVES
do
  echo "Checking for $host..."
  ldapsearch -x -L -b $SUFFIX -h $host -D "cn=people,$SUFFIX" -w new cn=people userPassword > $tmp2
  diff -c $tmp $tmp2
done



############# Protocol Test

ldapdelete -x -h $HOST -w $ROOTPW -D $ROOTDN "ou=add,$SUFFIX" "cn=people,$SUFFIX"
