#!/bin/sh

source @datadir@/@package_name@/data/DSSharedLib

libpath_add "@libdir@/@package_name@/"
libpath_add "@nss_libdir@"
libpath_add "@libdir@"

export LD_LIBRARY_PATH
SHLIB_PATH=$LD_LIBRARY_PATH
export SHLIB_PATH
      
usage ()
{
    echo "Usage: dn2rdn [-Z serverID] [-h] [-f] [-v] [-d debuglevel]"
    echo "Options:"
    echo "        -Z serverID    - Server instance identifier"
    echo "        -f             - Force upgrade"
    echo "        -v             - Display version"
    echo "        -d debuglevel  - Debugging level"
    echo "        -h             - Display usage"
}
    
while getopts "Z:d:ha:vfr:D:" flag
do
    case $flag in
        h) usage
           exit 0;;
        Z) servid=$OPTARG;;
        d) arg=$arg" -d $OPTARG";;
        a) arg=$arg" -a $OPTARG"
           archive="provided";;
        v) arg=$arg" -v";;
        f) arg=$arg" -f";;
        D) arg=$arg" -D $OPTARG";;
        ?) usage
           exit 1;;
    esac
done

initfile=$(get_init_file "@initconfigdir@" $servid)
if [ $? == 1 ]
then
    usage
    echo "You must supply a valid server instance identifier.  Use -Z to specify instance name"
    echo "Available instances: $initfile"
    exit 1
fi

servid=`normalize_server_id $initfile`
. $initfile

if [ "$archive" != "provided" ]; then
    bak_dir=@localstatedir@/lib/@PACKAGE_NAME@/slapd-$servid/bak/reindex_`date +%Y_%m_%d_%H_%M_%S`
    args=$args"-a $bak_dir"
fi

@sbindir@/ns-slapd upgradedb -D $CONFIG_DIR -r $args
