#!/bin/sh
#
# Author: Jesus Perez 
# Pragma: Aug 7 2002  vers. 1.2
# STTA Rev. 1.3 Copyright (c) 2001-2003 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.3pre1049295437- [2003_4_02_165717]
#
# This is to replicate LDAPs as a mechanism to synchronize contents as all are active.
# The idea is:
#   In case this script is running on the Master to replicate ...
#   1)  to generate "ldif" files from the "primary" LDAPs 
#   2)  to send it to the "secondary" LDAPs
#   In case this script is running on a target LDAP ...
#   1)  Re-load inf if there is info available i
#       (so just in case of paranoia it can be run form system cron)
#
# some criteria can be changed ... the LDAP commands, etc are for IPLANET 
#
# Fix vers. 1.1 include SUN shell test problems and sending STTA "defs" files, gzip is added !!!
# Fix vers. 1.1 case for different transfer methods
#
# SET THE FOLLOWING VARIABLES TO MATCH YOUR SYSTEM REQUIREMENTS
masterHost=              # master host equal `hostname` string
targetHost=""            # list of target hosts
loginToSend=             # Login to send by ftp 
passwToSend=`cat /opt/tarantella/tools/etc/.passwd`    # put password in this file
serviceToSend=ftp        # Transfer method 
ldapLog=/opt/save/ldap.log # Path for ldap log to copy 


ldapHome=/opt/netscape/ldap
masterDirs="slapd-ttaldap slapd-ttacache"
ldifcmd=db2ldif
ldif2dbcmd=ldif2db
ldapStop=stop-slapd
ldapStart=start-slapd
workingDir=/opt/save/toreplicate
saveReplica=/opt/save/replicas
sttaDefs=defs
sttaDirDefs=/opt/tarantella

ahora=`date +%Y_%m_%d_%H%M%S` 
thisHost=`hostname` 


if [ "$thisHost" != "$masterHost" ]
then
   gunzip $workingDir/*.gz
   for ldap in ` echo $masterDirs` 
   do
      nom=`echo $ldap | sed 's/slapd-//g'` 
      file=$nom"_"$ahora

      # Stoping LDAP server
      $ldapHome/$ldap/$ldapStop

      # Loading  LDAP server
      templateFiles=$workingDir/$nom"_*" 
      for ldif_file in `ls $templateFiles 2>/dev/null` 
      do
          chmod 777 $ldif_file
          # This is just for IPLANET
          nameFile=`basename $ldif_file`
          dirLoad=`basename $ldif_file | sed "s,$nom,,g" | sed 's/^_*//g' `
          mkdir $saveReplica/$dirLoad 2>/dev/null 
          nload=$saveReplica/$dirLoad/$nom"_load"
          echo "Loading .... $nom ... from  `basename $ldif_file ` ..."
          $ldapHome/$ldap/$ldif2dbcmd -i $ldif_file >$nload 
          [ "$?" -eq "0" ] && {
              mv $ldif_file $saveReplica/$dirLoad
              touch $nload
          }
          gzip $saveReplica/$dirLoad/$nameFile
      done
      # Starting LDAP server
      $ldapHome/$ldap/$ldapStart
   done
   # loading Defs 
   nom=$sttaDefs 
   templateFiles=$workingDir/$nom"_*" 
   for def_file in `ls $templateFiles 2>/dev/null` 
   do
      nameFile=`basename $def_file`
      dirLoad=`basename $def_file | sed "s,$nom,,g" | sed 's/^_*//g' | sed 's/.tar//g' `
      mkdir $saveReplica/$dirLoad 2>/dev/null 
      echo "Loading .... $nom ... from  `basename $def_file ` ..."
      cd $sttaDirDefs
      tar xvf $def_file
      [ "$?" -eq "0" ] && {
           mv $def_file $saveReplica/$dirLoad
      }
   done
   gzip $saveReplica/$dirLoad/$nameFile
else 
   for ldap in ` echo $masterDirs` 
   do
      nom=`echo $ldap | sed 's/slapd-//g'` 
      file=$nom"_"$ahora
      $ldapHome/$ldap/$ldifcmd $workingDir/$file
   done
   file="defs_"$ahora".tar"
   cd $sttaDirDefs
   tar cvf $workingDir/$file $sttaDefs $ldapLog
   gzip $workingDir/* 

   case "$serviceToSend" in
    ftp|FTP)
         wkFtp=$workingDir/ftp.$$
         echo "" >$wkFtp
         echo "user $loginToSend $passwToSend ">>$wkFtp
         echo "binary ">>$wkFtp
         echo "prompt">>$wkFtp
         echo "cd $workingDir ">>$wkFtp
         echo "lcd $workingDir"  >>$wkFtp
         echo "mput *$ahora*"  >>$wkFtp
         echo "bye " >>$wkFtp
         for server in `echo $targetHost` 
         do
             ftp -n $server  <$wkFtp 
             result=$?
         done
         [ "$result" -eq "0" ] && {
              mkdir $saveReplica/$ahora 2>/dev/null 
              mv $workingDir/*$ahora* $saveReplica/$ahora
              echo $ahora >$saveReplica/last_send
              touch $saveReplica/last_send
         }
         rm $wkFtp 2>/dev/null
         ;;
    *) echo " Unsupported transfer method !!! ";  exit 2
         ;;
    esac
fi


