#!/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
PATH=$PATH:/bin

usage()
{
    echo "Usage: dbverify [-Z serverID] [-n backend_instance] [-V] [-v] [-d debuglevel] [-h]"
    echo "Note if \"-n backend\" is not passed, verify all DBs."
    echo "Options:"
    echo "        -Z             - Server instance identifier"
    echo "        -n backend     - Backend database name.  Example: userRoot"
    echo "        -V             - Verbose output"
    echo "        -d debuglevel  - Debugging level"
    echo "        -v             - Display version"
    echo "        -h             - Display usage"
}

display_version="no"
while getopts "Z:n:hVvfd:n:D:" flag
do
    case $flag in
        h) usage
           exit 0;;
        Z) servid=$OPTARG;;
        n) args=$args" -n $OPTARG";;
        d) args=$args" -d $OPTARG";;
        V) args=$args" -V";;
        v) args=$args" -v"
           display_version="yes";;
        f) args=$args" -f";;
        D) args=$args" -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

. $initfile

@sbindir@/ns-slapd dbverify -D $CONFIG_DIR $args
if [ $display_version == "yes" ]; then
    exit 0
fi
if [ $? -eq 0 ]; then
    echo "DB verify: Passed"
    exit 0
else
    echo "DB verify: Failed"
    exit 1
fi
