#!/bin/sh
#
# Author: Jesus Perez 
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]
#
# 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 added !!!
# Fix vers. 1.1 case for different transfer methods
# Fix vers. 1.2 fix for stta from 1.2
# Vers. 1.3 move settings to STTA and load from common scripts env file
# Vers. 1.3 setting lang strings from STTA files and load from common scripts env file
# Vers. 1.3 adding Webmin https method for transfers
#

STTA_TOOLSDIR="/opt/tarantella/tools"
[ -x $STTA_TOOLSDIR/setenv_vars ] &&  . $STTA_TOOLSDIR/setenv_vars
[ -z "$STTA_masterSenderHost" ] && echo "MasterSenderHost $msg_envnotset. $msg_runsttacfg (shells) !!! " && exit 2
[ -z "$STTA_targetHosts" ] && echo "targetHosts $msg_envnotset. $msg_runsttacfg (shells) !!! " && exit 2
sttaDefs=`basename $STTA_defslist`
sttaDirDefs=`dirname $STTA_defslist`

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

if [ "$thisHost" != "$STTA_masterSenderHost" ]
then
   $UnZipTool $STTA_toGetDir/*.gz 2>$dn
   for ldap in ` echo $LDAP_dirname $LDAPCACHE_dirname `
   do
      nom=`echo $ldap | sed 's/slapd-//g'` 
      file=$nom"_"$ahora
      # Stoping LDAP server
      [ "$LDAP_dirname" = "$ldap" ] && eval "$LDAP_stop" 
      [ "$LDAPCACHE_dirname" = "$ldap" ] && eval "$LDAPCACHE_stop" 
      # Loading  LDAP server
      templateFiles=$STTA_toGetDir/$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 $STTA_saveSendDir/$dirLoad 2>/dev/null 
          nload=$STTA_saveSendDir/$dirLoad/$nom"_load"
          echo "$msg_loading .... $nom ... $msg_from  `basename $ldif_file ` ..."
          LDAPTYPE="" ; LDAPCONF=""
          [ "$LDAP_dirname" = "$ldap" ] && LDAPTYPE=$LDAP_TYPE && LDAPLOAD=$LDAP_load
          [ "$LDAPCACHE_dirname" = "$ldap" ] && LDAPTYPE=$LDAPCACHE_TYPE && LDAPLOAD=$LDAPCACHE_load
          case "$LDAPTYPE" in 
            OpenLDAP|OPENLDAP )  eval "$LDAPLOAD $ldif_file >$nload ";;
            iplanet|IPLANET ) eval "$LDAPLOAD $ldif_file >$nload " ;;
            *) continue ;;
          esac
          if [ "$?" -eq "0" ] ; then 
              mv $ldif_file $STTA_saveSendDir/$dirLoad 
              $ZipTool $STTA_saveSendDir/$dirLoad/$nameFile 2>$dn
              touch $nload
          else 
              $ZipTool $ldif_file 2>$dn
          fi
      done
      # Starting LDAP server
      [ "$LDAP_dirname" = "$ldap" ] && eval "$LDAP_start" 
      [ "$LDAPCACHE_dirname" = "$ldap" ] && eval "$LDAPCACHE_start" 
   done
   # loading Defs 
   nom=$sttaDefs 
   templateFiles=$STTA_toGetDir/$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 $STTA_saveSendDir/$dirLoad 2>/dev/null 
      echo "$msg_loading .... $nom ... $msg_from  `basename $def_file ` ..."
      cd $sttaDirDefs
      tar xvf $def_file
      if [ "$?" -eq "0" ] ; then
           mv $def_file $STTA_saveSendDir/$dirLoad
           $ZipTool $STTA_saveSendDir/$dirLoad/$nameFile 2>$dn
      else 
           $ZipTool $def_file 2>$dn
      fi
   done
else 
   # Running on MasterHost so pack and transfer files ... 
   rm $STTA_toSendDir/* 2>$dn
   for ldap in ` echo $LDAP_dirname $LDAPCACHE_dirname `
   do
      nom=`echo $ldap | sed 's/slapd-//g'` 
      file=$nom"_"$ahora
      LDAPTYPE="" ; LDAPCONF=""
      [ "$LDAP_dirname" =  "$ldap" ] && LDAPTYPE=$LDAP_TYPE && LDAPLDIF=$LDAP_ldif
      [ "$LDAPCACHE_dirname" = "$ldap" ] && LDAPTYPE=$LDAPCACHE_TYPE && LDAPLDIF=$LDAPCACHE_ldif
      case "$LDAPTYPE" in 
        OpenLDAP|OPENLDAP ) eval "$LDAPLDIF $STTA_toSendDir/$file" ;;
        iplanet|IPLANET ) eval "$LDAPLDIF $STTA_toSendDir/$file" ;;
        *) continue ;;
      esac
   done
   file="defs_"$ahora".tar"
   cd $sttaDirDefs
   tar cvf $STTA_toSendDir/$file $sttaDefs $STTA_ldapLog
   $ZipTool $STTA_toSendDir/* 2>$dn

   case "$STTA_serviceToSend" in
    ftp|FTP)
         passwToSend="";
         if [ -r  $STTA_passwToSend ] ; then 
              passwToSend=`cat $STTA_passwToSend 2>/dev/null`    # put password in this file
         fi
         [ -z "$passwToSend" ] && echo "passwToSend $msg_envnotset. $msg_runsttacfg (shells) !!! " && exit 2
         [ -z "$STTA_loginToSend" ] && echo "loginToSend $msg_envnotset. $msg_runsttacfg (shells) !!! " && exit 2
         wkFtp=$STTA_toGetDir/ftp.$$
         echo "" >$wkFtp
         echo "user $STTA_loginToSend $passwToSend ">>$wkFtp
         echo "binary ">>$wkFtp
         echo "prompt">>$wkFtp
         echo "cd $STTA_toGetDir ">>$wkFtp
         echo "lcd $STTA_toSendDir"  >>$wkFtp
         echo "mput *$ahora*"  >>$wkFtp
         echo "bye " >>$wkFtp
         for server in `echo $STTA_targetHosts` 
         do
             ftp -n $server  <$wkFtp 
             result=$?
         done
         [ "$result" -eq "0" ] && {
              mkdir $STTA_saveSendDir/$ahora 2>/dev/null 
              mv $STTA_toSendDir/*$ahora* $STTA_saveSendDir/$ahora
              echo $ahora >$STTA_saveSendDir/last_send
              touch $STTA_saveSendDir/last_send
         }
         rm $wkFtp 2>/dev/null
         ;;
    http|https|HTTP|HTTPS)
         case "$1" in 
           tdone) 
              mkdir $STTA_saveSendDir/$ahora 2>/dev/null 
              mv $STTA_toSendDir/* $STTA_saveSendDir/$ahora
              echo $ahora >$STTA_saveSendDir/last_send
              touch $STTA_saveSendDir/last_send
              ;;
         esac
         ;;
    *) echo "$msg_unsuppmethod";  exit 2
         ;;
    esac
fi
