#!/bin/sh
# BEGIN COPYRIGHT BLOCK
# Copyright (C) 2001 Sun Microsystems, Inc.  Used by permission.
# Copyright (C) 2005 Red Hat, Inc.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

# END COPYRIGHT BLOCK

# This script sets up the environment for the httpd server and starts it.
# for httpd, on RHEL, this will typically be something like /usr/sbin/httpd.worker
# On HP-UX, this may be /opt/hpws/apache/bin/httpd.

unset PASSWORD_PIPE

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

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

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

OLD_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
OLD_SHLIB_PATH=${SHLIB_PATH}
OLD_LIBPATH=${LIBPATH}
LD_LIBRARY_PATH=
SHLIB_PATH=
LIBPATH=

ldlibpath_add "/usr/lib64"
ldlibpath_add "${OLD_LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

shlibpath_add "/usr/lib64"
shlibpath_add "${OLD_SHLIB_PATH}"
export SHLIB_PATH

libpath_add "/usr/lib64"
libpath_add "${OLD_LIBPATH}"
libpath_add "/usr/threads/lib"
libpath_add "/usr/ibmcxx/lib"
libpath_add "/usr/lib"
libpath_add "/lib"
export LIBPATH

HTTPD=/usr/sbin/httpd.worker
OS=`uname -s`

# see if httpd is linked with the openldap libraries - we need to override them if
# using mozldap
if [ -z "1" ] ; then
    if [ $OS = "Linux" ]; then
        hasol=0

        /usr/bin/ldd $HTTPD 2>&1 | grep libldap > /dev/null 2>&1 && hasol=1

        if [ $hasol -eq 1 ] ; then
            LD_PRELOAD=" /libldap60.so"
            ssl_lib="/usr/lib64/libssl3.so"
            if [ -f "$ssl_lib" ] ; then
                LD_PRELOAD="$ssl_lib $LD_PRELOAD"
            fi
            export LD_PRELOAD
        fi
    fi
fi

# mod_deflate is not supported on HP-UX using the /opt/hpws apache
if [ $OS = "HP-UX" ] ; then
    OMIT_DEFLATE="-DOmitDeflate"
fi

# source env. for admin server
[ -f /etc/sysconfig/dirsrv-admin ] && . /etc/sysconfig/dirsrv-admin

if [ -z "yes" ] ; then
    if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
        SELINUX_CMD="runcon -t unconfined_t --"
    fi
fi

$SELINUX_CMD $HTTPD $OMIT_DEFLATE -k start -f /etc/dirsrv/admin-serv/httpd.conf "$@"

PIDFILE=/var/run/dirsrv/admin-serv.pid

loop_counter=1
# wait for 10 seconds for the pid file to appear
max_count=10
while test $loop_counter -le $max_count; do
    loop_counter=`expr $loop_counter + 1`
    if test ! -f $PIDFILE ; then
        sleep 1;
    else
        PID=`cat $PIDFILE`
    fi
done
if test ! -f $PIDFILE ; then
    echo Server failed to start !!! Please check errors log for problems
    exit 1
fi
