#!/bin/sh
#
# Copyright (C) 2004 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: 11recovery,v 1.2 2004/10/14 09:50:46 taru Exp $

set -e 

if test "x$BACKUP" = "x"; then
  echo "Ignoring $0 for failover"
  exit $IGNORE
fi

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

# Adding an entry while the replication is stopped

$MODULEDIR/server/startup slurp_stop

cat >$TMPFILE <<EOF
dn: ou=add,$TESTSUFFIX
objectClass: top
objectClass: organizationalUnit
ou: add

EOF

ldapadd -x -h $LDAPMASTER -D $ROOTDN -w $ROOTPW -f $TMPFILE

# Now taking over starting

date
$MODULEDIR/server/startup stop

# Checki whether the previous addition is availble

for host in $SLAVES
do
  ( for i in $PIDTICK
    do
      echo "Checking for $host..."
      ldapsearch -x -w $ROOTPW -LLL -D $ROOTDN -h $host -b $TESTSUFFIX ou=add | diff -c $TMPFILE - && exit 0
      sleep $i
    done
    exit 1
   ) || exit 1
done

# Simple recovery OK.

# Now deleting the entry on the backup server

(
 for i in $PIDTICK
  do
  if ldapdelete -x -h $BACKUP -D $ROOTDN -w $ROOTPW  ou=add,$TESTSUFFIX; then
    exit 0
  fi
  sleep $i
  done
  exit 1
) || exit 1

# Failback

$MODULEDIR/server/startup start


# Now we check for the slurpd.status file handling

if ldapsearch -x -w $ROOTPW -LLL -D $ROOTDN -h $BACKUP -b $TESTSUFFIX ou=add | grep ou=add; then
  echo "E: ou=add,$TESTSUFFIX should be deleted on $BACKUP" 1>&2
  exit $NG
fi

exit $OK
