#!/bin/sh

. /usr/share/dirsrv/data/DSSharedLib

libpath_add "/usr/lib64/dirsrv/"
libpath_add ""
libpath_add "/usr/lib64"
libpath_add ""

export LD_LIBRARY_PATH
SHLIB_PATH=$LD_LIBRARY_PATH
export SHLIB_PATH

cwd=`pwd`

usage()
{
    echo "Usage: db2ldif [-Z serverID] {-n backend_instance}* | {-s includesuffix}* [{-x excludesuffix}*] [-a outputfile]"
    echo "               [-E] [-r] [-u] [-U] [-m] [-1] [-q] [-V] [-v] [-h]"
    echo "Note: either \"-n backend\" or \"-s includesuffix\" is required."
    echo "Options:"
    echo "        -Z serverID       - Server instance identifier"
    echo "        -n backend        - Backend database name.  Example: userRoot"
    echo "        -s inclduesuffix  - Suffix to include"
    echo "        -x                - Suffix to exclude"
    echo "        -a outputfile     - Name of the exported LDIF file"
    echo "        -r                - Include replication data"
    echo "        -E                - Decrypt attributes"
    echo "        -u                - Do not export the nsUniqueId attribute"
    echo "        -U                - Do not wrap long lines"
    echo "        -m                - Do not base64 encode values"
    echo "        -1                - Do not include version text"
    echo "        -q                - Quiet mode - suppresses output"
    echo "        -V                - Verbose output"
    echo "        -v                - Display version"
    echo "        -h                - Display usage" 
}

make_ldiffile()
{
    be=""
    while [ "$1" != "" ]
    do
        if [ "x$1" = "x-a" ]; then
            shift
            if [ `expr "$1" : "/.*"` -gt 0 ]; then 
	            if [ `expr "$1" : "/.*"` -gt 0 ]; then 
	                # full path 
	                echo $1 
	                return 1 
	            else 
	                echo $cwd/$1 
	                shift 
	                return 0 
	            fi 
            else 
                echo $cwd/$1 
                shift 
                return 0 
            fi
        elif [ "x$1" = "x-n" ]; then
            shift
            if [ -z "$be" ]; then
                be="$1"
            else
                tmpbe="$be"
                be="${tmpbe}-$1"
            fi
        elif [ "x$1" = "x-s" ]; then
            shift
            if [ -n "$1" ]; then
                rdn=`echo $1 | awk -F, '{print $1}'`
                rdnval=`echo $rdn | awk -F= '{print $2}'`
                if [ "$be" = "" ]; then
                    be="$rdnval"
                else
                    tmpbe="$be"
                    be="${tmpbe}-$rdnval"
                fi
            fi
        elif [ "x$1" = "x-M" ]; then
            be=""
        fi
        if [ -n "$1" ]; then
            shift
        fi
    done

    if [ -z "$be" ]; then
        echo /var/lib/dirsrv/slapd-$servid/ldif/$servid-`date +%Y_%m_%d_%H%M%S`.ldif
    else
        echo /var/lib/dirsrv/slapd-$servid/ldif/$servid-${be}-`date +%Y_%m_%d_%H%M%S`.ldif
    fi
    return 0
}

if [ $# -lt 2 ];
then
    usage
    exit 1
fi

while getopts "hZ:vd:D:ENa:rs:x:CSut:n:UmMo1qVc:" flag
do
    case $flag in
        h) usage
           exit 0;;
        Z) servid=$OPTARG;; 
        n) benameopt=$benameopt" -n $OPTARG"
           required_param="yes";;
        s) includeSuffix=$includeSuffix" -s \"$OPTARG\""
           required_param="yes";;
        x) excludeSuffix=$excludeSuffix" -x \"$OPTARG\"";;
        a) outputFile="-a \"$OPTARG\"";;
        d) args=$args" -d \"$OPTARG\"";;
        D) args=$args" -D \"$OPTARG\"";;
        N) args=$args" -N";;
        E) args=$args" -E";;
        S) args=$args" -S";;
        v) args=$args" -v";;
        r) args=$args" -r";;
        C) args=$args" -C";;
        u) args=$args" -u";;
        U) args=$args" -U";;
        m) args=$args" -m";;
        M) args=$args" -M";;
        1) args=$args" -1";;
        q) args=$args" -q";;        
        V) args=$args" -V";;
        c) cwd=$OPTARG;;
        ?) usage
           exit 1;;
    esac
done

ARGS=$@
shift $(($OPTIND - 1))
if [ $1 ]
then
    echo "ERROR - Unknown option: $1"
    usage
    exit 1
fi

if [ "$required_param" != "yes" ]
then
    usage
    exit 1
fi

instance=$(get_slapd_instance "/etc/dirsrv" $servid)
if [ $? -eq 1 ]
then
    usage
    echo "You must supply a valid server instance identifier.  Use -Z to specify instance name"
    echo "Available instances: $instance"
    exit 1
fi

CONFIG_DIR="/etc/dirsrv/slapd-$instance"

ldif_file=`make_ldiffile $ARGS`
rn=$?

echo "Exported ldif file: $ldif_file"
if [ $rn -eq 1 ]
then
    eval /usr/sbin/ns-slapd db2ldif -D $CONFIG_DIR $benameopt $includeSuffix $excludeSuffix $outputFile $args
else
    eval /usr/sbin/ns-slapd db2ldif -D $CONFIG_DIR $benameopt $includeSuffix $excludeSuffix $args -a $ldif_file
fi
