#!/bin/bash

source /usr/share/dirsrv/data/DSSharedLib

libpath_add "/usr/lib/arm-linux-gnueabihf/dirsrv/"
libpath_add "/usr/lib/arm-linux-gnueabihf"
libpath_add ""
libpath_add "/usr/lib/arm-linux-gnueabihf"

export LD_LIBRARY_PATH
SHLIB_PATH=$LD_LIBRARY_PATH
export SHLIB_PATH

usage ()
{
    echo "Usage: vlvindex [-Z serverID] -n backendname | {-s includesuffix}* -T vlvTag [-d debuglevel] [-h]"
    echo "Note: either \"-n backend\" or \"-s includesuffix\" are required."
    echo "Options:"
    echo "        -Z serverID        - Server instance identifier"
    echo "        -n backend         - Backend database name.  Example: userRoot"
    echo "        -s includessuffix  - Suffix to index"
    echo "        -T vlvTag          - VLV index name"
    echo "        -d debuglevel      - Debugging level"
    echo "        -h                 - Display usage"
}

while getopts "Z:vd:a:T:Sn:s:x:hD:" flag
do
    case $flag in
        Z) servid=$OPTARG;;
        v) args=$args" -v";;
        s) args=$args" -s $OPTARG";;
        d) args=$args" -d $OPTARG";;
        a) args=$args" -a $OPTARG";;
        T) args=$args" -T $OPTARG";;
        S) args=$args" -S";;
        n) args=$args" -n $OPTARG";;
        x) args=$args" -x $OPTARG";;
        D) args=$args" -D $OPTARG";;
        h) usage
           exit 0;;
        ?) usage
           exit 1;;
    esac
done

initfile=$(get_init_file "/etc/default" $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

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

/usr/sbin/ns-slapd db2index -D $CONFIG_DIR $args
