#! /bin/sh
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]
# OpenLdap admin manager for STTA Ldap Servers
#
USAGE="oldapadm [ ldapserver-name] [ ldiff | load | back | restore ] [ data-file-path ]";
STTA_TOOLSDIR="STTATOOLSDIR"
if [ -z "$LDAP_daemon" ] ; then 
     [ -x $STTA_TOOLSDIR/setenv_vars ] &&  . $STTA_TOOLSDIR/setenv_vars
fi
[ -z "$LDAP_daemon" ] && echo "LDAP_daemon $msg_envnotset. $msg_runsttacfg (shells) !!! " && exit 2
case "$#" in
    0|1) echo "$USAGE" ; exit 2 ;;
esac 
ahora=`date '+%d-%m-%Y %H:%M:%S'`
[ -n "$3" ] && echo $ahora
case "$1" in
    $LDAP_dirname) [ "$LDAP_TYPE" = "IPLANET" ] && echo "$1 : $LDAP_TYPE " && exit 1
                  LDAP_dbpath=`cat $LDAP_conf | egrep directory | awk '{ print $2 }'`
                  [ ! -d "$LDAP_dbpath" ] && echo "$msg_notexistdir " && exit 1
                  ;;
    $LDAPCACHE_dirname) 
                  [ "$LDAPCACHE_TYPE" = "IPLANET" ] && echo "$1 : $LDAPCACHE_TYPE " && exit 1
                  LDAPCACHE_dbpath=`cat $LDAPCACHE_conf | egrep directory | awk '{ print $2 }'`
                  [ ! -d "$LDAPCACHE_dbpath" ]  && echo "$msg_notexistdir " && exit 1
                 ;;
   *) echo "$1: $msg_noldapserver " ; exit 2 
                 ;;
esac
case "$2" in
    ldiff|ldif)
        [ -n "$3" ] && OUTF=" -l $3" &&  echo "$1: $2  --> $3"
        case "$1" in
             $LDAP_dirname) $OpenLDAP_cat -f $LDAP_conf $OUTF 
                            echo "$ahora : $OpenLDAP_cat -f $LDAP_conf $OUTF ">> $LDAP_log
                            ;;

             $LDAPCACHE_dirname) $OpenLDAP_cat -f $LDAPCACHE_conf $OUTF 
                            echo "$ahora : $OpenLDAP_cat -f $LDAPCACHE_conf $OUTF ">> $LDAPCACHE_log
                            ;;
        esac
        [ $? -eq 0 ] && [ -n "$3" ] && echo "$1: $msg_done $2  --> $3"
        ;;
    load)
        [ -z "$3" ] || [ ! -r "$3" ] && echo "$1: $msg_nofile $3" && echo "$USAGE" && exit 2
        case "$1" in
             $LDAP_dirname) $LDAP_stop 
                            rm $LDAP_dbpath/*db* 2>$dn
                            echo "$1: $msg_loading $msg_from $3"
                            $OpenLDAP_add -f $LDAP_conf -l $3 
                            echo "$ahora : $OpenLDAP_add -f $LDAP_conf -l $3 ">> $LDAP_log
                            $LDAP_start
                            ;;
             $LDAPCACHE_dirname) $LDAPCACHE_stop
                            rm $LDAPCACHE_dbpath/*db* 2>$dn
                            echo "$1: $msg_loading $3"
                            $OpenLDAP_add -f $LDAPCACHE_conf -l $3 
                            echo "$ahora : $OpenLDAP_add -f $LDAPCACHE_conf -l $3 ">> $LDAPCACHE_log
                            $LDAPCACHE_start
                            ;;
        esac
        [ $? -eq 0 ] && echo "$1: $msg_done $2  --> $3"
        ;;
    backup|back)
        [ -z "$3" ] || [ ! -d "$3" ] && echo "$1: ($2) $msg_notexistdir $3 " &&  exit 2 
        case "$1" in
             $LDAP_dirname) 
                            echo "$1: $msg_cpfiles $LDAP_dbpath --> $3 " 
                            cp -pr $LDAP_dbpath $3
                            echo "$ahora : cp -pr $LDAP_dbpath $3 ">> $LDAP_log
                            ;;
             $LDAPCACHE_dirname) 
                            echo "$1: $msg_cpfiles $LDAPCACHE_dbpath --> $3 " 
                            cp -pr $LDAPCACHE_dbpath $3
                            echo "$ahora : cp -pr $LDAPCACHE_dbpath $3 ">> $LDAPCACHE_log
                            ;;
        esac
        ;;
    restore|res)
        [ -z "$3" ] || [ ! -d "$3" ] && echo "$1: ($2) $msg_dirnotexist $3 " &&  exit 2 
        case "$1" in
             $LDAP_dirname) $LDAP_stop
                            echo "$1: $msg_cpfiles $3 --> $LDAP_dbpath " 
                            cp -pr $3/$LDAP_dirname/*  $LDAP_dbpath 
                            echo "$ahora : cp -pr $3/$LDAP_dirname/* $LDAP_dbpath ">> $LDAP_log
                            $LDAP_start
                            ;;
             $LDAPCACHE_dirname) $LDAPCACHE_stop
                            echo "$1: $msg_cpfiles $3 --> $LDAPCACHE_dbpath " 
                            cp -pr $3/$LDAPCACHE_dirname/* $LDAPCACHE_dbpath 
                            echo "$ahora : cp -pr $3/$LDAPCACHE_dirname/* $LDAPCACHE_dbpath ">> $LDAPCACHE_log
                            $LDAPCACHE_start
                            ;;
        esac
        ;;
    *)
	echo "$USAGE"
        exit 1
esac
