#!/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.
#

set -e

eval `ultrapossum-config init`
trap "eval `ultrapossum-config term`" 0

if test "x$NFSDIR" = "x" && test "x$RPLDIR" = "x"; then
  echo "E: Need to specify both NFSDIR and RPLDIR" 1>&2
  exit 1
fi

RPLDIR=$NFSDIR/$RPLDIR

#SLURPD=/usr/sbin/slurpd
SLURPD=slurpd
TMPDIR=/tmp
TMPRPL=replica.$$

OWN=$HOST
OTHER=$ANOTHER_MASTER

# check replication log of previous master server
needrecover=0

install -d $RPLDIR/$OWN $RPLDIR/$OTHER $RPLDIR/$OWN/replica $RPLDIR/$OTHER/replica

if ! [ -f $RPLDIR/$OWN/replog ] && [ -f $RPLDIR/$OTHER/replog ]; then
    $MODULEDIR/failover/checkreplog $RPLDIR/$OTHER/replog $RPLDIR/$OWN/replog || exit 1
    needrecover=1
fi

if [ $RPLDIR/$OTHER/replog -nt $RPLDIR/$OWN/replog ]; then
    $MODULEDIR/failover/checkreplog $RPLDIR/$OTHER/replog $RPLDIR/$OWN/replog || exit 1
    needrecover=1
fi

if ! [ -f $RPLDIR/$OWN/replica/slurpd.replog ] && [ -f $RPLDIR/$OTHER/replica/slurpd.replog ]; then
    $MODULEDIR/failover/checkreplog $RPLDIR/$OTHER/replica/slurpd.replog $RPLDIR/$OWN/replica/slurpd.replog || exit 1
    needrecover=1
fi

if [ $RPLDIR/$OTHER/replica/slurpd.replog -nt $RPLDIR/$OWN/replica/slurpd.replog ]; then
    $MODULEDIR/failover/checkreplog $RPLDIR/$OTHER/replica/slurpd.replog $RPLDIR/$OWN/replica/slurpd.replog || exit 1
    needrecover=1
fi

if [ $RPLDIR/$OTHER/replica/slurpd.status -nt $RPLDIR/$OWN/replica/slurpd.status ]; then
    if $MODULEDIR/failover/checkstatus -f $ULTRAPOSSUM_MASTER_SLAPD_CONF $RPLDIR/$OTHER/replica/slurpd.status
	then
	cp $RPLDIR/$OTHER/replica/slurpd.status $RPLDIR/$OWN/replica/slurpd.status || exit 1
    elif [ $? -eq 1 ]; then
	cp $RPLDIR/backup/slurpd.status $RPLDIR/$OWN/replica/slurpd.status || exit 1
    else
	exit 1
    fi
fi

    # import master data 
if [ $needrecover -gt 0 ]; then
    mkdir $TMPDIR/$TMPRPL
    cp -r $RPLDIR/$OWN/replica $TMPDIR/$TMPRPL
    cat $RPLDIR/$OWN/replog >> $TMPDIR/$TMPRPL/replica/slurpd.replog
    if test "`$MODULEDIR/server/startup status`" = "stopped"; then
      /etc/init.d/slapd start
      sleep 1
    fi
    echo "Import master data" 1>&2
    echo "replica: $TMPRPL/replica" 1>&2
    echo "$SLURPD -d 255 -o -f $CONFDIR/recovery.conf -r $TMPDIR/$TMPRPL/replica/slurpd.replog -t $TMPDIR/$TMPRPL" 1>&2
    if ! $SLURPD -d 255 -o -f $CONFDIR/recovery.conf -r $TMPDIR/$TMPRPL/replica/slurpd.replog -t $TMPDIR/$TMPRPL 2>&1 > $TMPDIR/$TMPRPL/slurpd.log.$$; then
	echo "Error: importing slurpd abnormally exitted" 1>&2
        cat $TMPDIR/$TMPRPL/slurpd.log.$$ 1>&2
    else
	rm -rf $TMPDIR/$TMPRPL
    fi
fi
