#!/usr/bin/env bash
#
# Sample Test Script for UltraPossum
#
#  Add a simple entry to the directory and check whether
#  it can be searched.
#
# 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: 00protocol,v 1.3 2004/04/02 02:34:28 taru Exp $

set -e

if test "x$MASTER" != "x$HOST" && test "x$BACKUP" != "x$HOST"; then
  echo "Ignoring $0 for master server: $HOST (M:$MASTER, B:$BACKUP)"
  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

if test "x$BACKUP" = "x$HOST"; then
  ssh $ssh_act$MASTER "$ROOTCMD sh -c '
  $SHAREDIR/startup stop
'" > /dev/tty
  (for i in 1 2 3 4 5 6 7 8 9 0
  do
    if test `$SHAREDIR/startup master_status` = "running"; then
      exit 0
    fi
    sleep 5
  done
  echo "E: LDAP master server can't acquire" 1>&2
  exit 1)
fi

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

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

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

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