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

# Pass a patch to the instance, and return the server ID
normalize_server_id()
{
    servid=$1
    servid=`echo "$servid" | sed 's#^/etc/dirsrv/slapd-##'`
    echo $servid
}

#
# Get all the instances
#
get_slapd_instances ()
{
    CONFIG_DIR=$1
    instances=
    found=1
    for inst in $CONFIG_DIR/slapd-* ; do
        if [[ ! $inst =~ ".removed" ]] ; then
            if [ -z "$instances" ] ; then
                instances=$inst
            else
                instances="$instances $inst"
            fi
            found=0
        fi
    done
    echo $instances
    return $found
}

get_slapd_instance ()
{
    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 instance in `get_slapd_instances $dir`
    do
        inst_count=`expr $inst_count + 1`
        id=`normalize_server_id $instance`
        if [ -n "$servid" ] && [ "$id" = "$servid" ]
        then
            # found it
            echo $id
            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 $instances
        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"
}
