#!/bin/sh

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

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

export LD_LIBRARY_PATH
SHLIB_PATH=$LD_LIBRARY_PATH
export SHLIB_PATH

usage ()
{
    echo "Usage: upgradedb [-a archivedir] [-Z serverID] [-f] [-v] [-d debuglevel] [-h]"
    echo "Options:"
    echo "        -a archivedir  - Location of database backup"
    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:vfrd:h" flag
do
    case $flag in
        Z) servid=$OPTARG;;
        v) args=$args" -v";;
        f) args=$args" -f";;
        r) args=$args" -r";;
        d) args=$args" -d $OPTARG";;
        a) args=$args" -a $OPTARG"
           archive_provided="yes";;
        D) args=$args" -D $OPTARG";;
        h) usage
           exit 0;;
    esac
done

initfile=$(get_init_file "@initconfigdir@" $servid)
if [ $? == 1 ]
then
    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" != "yes" ]
then
    bak_dir=@localstatedir@/lib/@PACKAGE_NAME@/slapd-$servid/bak/upgradedb_`date +%Y_%m_%d_%H_%M_%S`
    args=$args" -a $bak_dir"
fi

echo upgrade index files ...
@sbindir@/ns-slapd upgradedb -D $CONFIG_DIR $args
