#!/bin/sh

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

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

export LD_LIBRARY_PATH
SHLIB_PATH=$LD_LIBRARY_PATH
export SHLIB_PATH

usage ()
{
    echo "Usage: restoreconfig [-Z serverID] [-h]"
    echo "Options:"
    echo "        -Z serverID  - Server instance identifier"
    echo "        -h           - Display usage"
}

while getopts "Z:h" flag
do
    case $flag in
        Z) servid=$OPTARG;;
        h) usage
           exit 0;;
        ?) 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

conf_ldif=`ls -1t @localstatedir@/lib/@PACKAGE_NAME@/slapd-$servid/bak/$servid-*.ldif 2>/dev/null | head -1 `
if [ -z "$conf_ldif" ]
then
    echo No configuration to restore in @localstatedir@/lib/@PACKAGE_NAME@/slapd-$servid/bak/ ; exit 1
fi
echo Restoring $conf_ldif...
@sbindir@/ns-slapd ldif2db -D $CONFIG_DIR -i $conf_ldif -n NetscapeRoot 2>&1
exit $?
