libpath_add()
{
    [ -z "$1" ] && return
    LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$1
}

# pass in a string that ends in dirsrv-name or slapd-name,
# and convert it to just "name"
normalize_server_id()
{
    servid=$1
    servid=`echo "$servid" | sed 's/^.*\///'`
    servid=`echo "$servid" | sed 's/^dirsrv-//'`
    servid=`echo "$servid" | sed 's/^slapd-//'`
    echo $servid
}

# look for all initconfig files in the given directory
# the initconfig files are the files used in the startup scripts
# to start each instance
# e.g. /etc/sysconfig/dirsrv-INST
# these scripts contain the pointer CONFIG_DIR to where the instance
# configuration files are to be found
# if the given directory is empty, look in /etc/sysconfig
# if not running as root, look for non-system instances in
# $HOME/.dirsrv
# ignore the dirsrv-admin admin server config file
#
get_initconfig_files()
{
    dir=${1:-/etc/sysconfig}
    # convert
    # uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),.....
    # to
    # 0
    # this is the only portable, secure way to determine the id number
    userid=`id | awk -F'[=(]+' '{print $2}'`
    if [ "$userid" -ne 0 ] ; then
        # magic - see start-dirsrv, DSCreate.pm::get_initconfigdir, etc.
        extradir=$HOME/.dirsrv
        if [ -d $extradir ] ; then
            extra="$extradir/dirsrv-*"
        fi
    fi
    # setting the env var forces the use of it and nothing else
    if [ -n "$INITCONFIGDIR" ] ; then
        dir=$INITCONFIGDIR
        extra=
    fi
    retfiles=
    found=1
    for file in $dir/dirsrv-* $extra ; do
        if [ ! -r "$file" ] ; then continue ; fi
        case "$file" in */dirsrv-admin) continue ;; esac
        if [ -z "$retfiles" ] ; then
            retfiles=$file
        else
            retfiles="$retfiles $file"
        fi
        found=0
    done
    echo $retfiles
    return $found # 0 return means success - at least one found
}

#
#  get_init_file()
#  
#  The init file is the instance specific file under
#  the @initconfig@ directory e.g.
#  /etc/sysconfig/dirsrv-instance
#  The presence and readability of this file means this is a
#  valid instance of directory server (except dirsrv-admin)
#  The CONFIG_DIR directive in this file tells us where to
#  look for the main server config
#  First grab all the dirsrv init files
#  Then check if a server id was provided, if not, return the
#  one found if there is only one
#  If a servid was provided, make sure there is an init file
#  for that instance
#  Return the /etc/sysconfig/dirsrv-$servid file name
#
get_init_file()
{
    dir=$1
    servid=$2
    first="yes"
    inst_count=0
    instances="<none>"

    # normalize servid, if given
    if [ -n "$servid" ]
    then
        servid=`normalize_server_id $servid`
    fi

    for configfile in `get_initconfig_files $dir`
    do
        inst_count=`expr $inst_count + 1`
        id=`normalize_server_id $configfile`
        if [ -n "$servid" ] && [ "$id" = "$servid" ]
        then
            # found it
            echo $configfile
            exit 0
        fi
        if  [ $first = "yes" ]
        then
            instances=$id
            first="no"
        else
            instances=$instances", $id"
        fi
    done

    # server id not provided, check if there is only one instance
    if [ -z "$servid" ] && [ $inst_count -eq 1 ]
    then
        # return the file
        echo $configfile
        exit 0
    else 
        # Either we have an invalid name, or more than one instance is available
        # Return the available instances instead of the config file
        echo $instances
        exit 1;
    fi
}

#
#
#
process_dse ()
{
    configdir=$1
    pid=$2
    file="$configdir/dse.ldif"
    OLD_IFS=$IFS
    IFS=""
    while read -r LINE
    do
        case $LINE in
            ' '*)
                ;;
            *)
                if [ -n "$output" ]
                then
                    echo "$output" >> /tmp/DSSharedLib.$pid
                    output=""
                fi
                ;;
        esac
        if [ -n "$output" ]
        then
            case $LINE in
                ' '*)
                    # continuation line, strip the space and append it
                    LINE=`echo "$LINE" | sed -e 's/^ //'`
                    output=$output$LINE
                    ;;
            esac
        else
            case $LINE in
                nsslapd-certdir*|\
                nsslapd-ldapiautobind*|\
                nsslapd-ldapilisten*|\
                nsslapd-ldapifilepath*|\
                nsslapd-localhost*|\
                nsslapd-port*|\
                nsslapd-rootdn*|\
                nsslapd-securePort*|\
                nsslapd-security*)
                    output=$LINE
                    ;;
            esac
        fi
    
    done < $file

    IFS=$OLD_IFS
}

#
# Check protocol
#
check_protocol ()
{
    protocol=$1
    security=$2
    ldapi=$3
    openldap=$4
       
    if [ "$protocol" = "LDAPI" ] && [ "$openldap" != "yes" ]; then
        echo ""
        exit
    elif [ "$protocol" = "LDAPI" ] && [ "$ldapi" = "off" ]; then
        echo ""
        exit
    elif [ "$protocol" = "STARTTLS" ]; then
        if [ -z "$security" ] || [ "$security" = "off" ]; then
            echo ""
            exit
        fi
    elif [ "$protocol" = "LDAPS" ]; then
        if [ -z "$security" ] || [ "$security" = "off" ]; then
            echo ""
            exit
        fi
    fi
    
    if [ "$protocol" != "" ]; then
        if [ "$protocol" != "STARTTLS" ] && 
           [ "$protocol" != "LDAPS" ] &&
           [ "$protocol" != "LDAPI" ] && 
           [ "$protocol" != "LDAP" ]
        then
            echo ""
            exit
        fi
    fi

    echo "$protocol"
}
