#!/bin/sh

# Script that starts the ns-slapd server.
# Exit status can be:
#       0: Server started successfully
#       1: Server could not be started
#       2: Server already running

. /usr/share/dirsrv/data/DSSharedLib

RUN_DIR="/run/dirsrv";

# Starts a single instance
start_instance() {
    # The first argument is the server ID.  Anything
    # after that is an argument to ns-slapd.
    SERV_ID=$1
    shift

    prefix="$DS_ROOT"

    libpath_add "$prefix$SERVER_DIR"
    libpath_add "$prefix"
    libpath_add "$prefix/usr/lib64"
    libpath_add ""
    libpath_add "$prefix"

    export LD_LIBRARY_PATH
    SHLIB_PATH=$LD_LIBRARY_PATH
    export SHLIB_PATH

    DS_CONFIG_DIR=$CONFIG_DIR
    export DS_CONFIG_DIR
    #
    # Use systemctl if available and running as root, 
    # otherwise start the instance the old way.
    #
    if [ -d "/usr/lib/systemd/system" ] && [ $(id -u) -eq 0 ];then
        /usr/bin/systemctl start dirsrv@$SERV_ID.service -l
        if [ $? -ne 0 ]; then
            return 1
        fi
    else
        instance=`get_slapd_instance /etc/dirsrv $SERV_ID` || { echo Instance $SERV_ID not found. ; return 1 ; }

        CONFIG_DIR="/etc/dirsrv/slapd-$instance";
        PIDFILE=/var$RUN_DIR/slapd-$SERV_ID.pid

        if test -f $PIDFILE ; then
            PID=`cat $PIDFILE`
            if kill -s 0 $PID > /dev/null 2>&1 ; then
                echo There is an ns-slapd running: $PID
                return 2;
            else
                rm -f $PIDFILE
            fi
        fi
        if test 1 -eq 0; then
            echo "NOTICE: Starting instance ${SERV_ID} with ASAN options."
            echo "This is probably not what you want. Please contact support."
            : ${ASAN_LOG_PATH:=/var$RUN_DIR/ns-slapd-${SERV_ID}.asan}
            echo "Asan errors will go to ${ASAN_LOG_PATH}*"
            export ASAN_OPTIONS="detect_leaks=1 symbolize=1 detect_deadlocks=1 log_path=${ASAN_LOG_PATH}"
            export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer
        fi
        /usr/sbin/ns-slapd -D $CONFIG_DIR -i $PIDFILE "$@"
        if [ $? -ne 0 ]; then
            return 1
        fi
        loop_counter=1
        # wait for 10 minutes (600 times 1 seconds)
        max_count=${PID_TIME:-600}
        while test $loop_counter -le $max_count; do
            loop_counter=`expr $loop_counter + 1`
            if test -f $PIDFILE ; then
                PID=`cat $PIDFILE`
                # if kill -s 0 $PID > /dev/null 2>&1 ; then
                if kill -s 0 $PID ; then
                    return 0;
                else
                    echo Server failed to start !!! Please check errors log for problems
                    return 1
                fi
            else
                sleep 1
            fi
        done
        echo Server not running!! Failed to start ns-slapd process.  Please check the errors log for problems.
        return 1
    fi
}

while getopts "d:" flag
do
    case "$flag" in
        d) initconfig_dir="$OPTARG";;
    esac
done
shift $(($OPTIND-1))


found=0
if [ $# -eq 0 ]; then
    # We're starting all instances.
    ret=0
    #
    # Use systemctl if available and running as root,
    #
    instances=`get_slapd_instances /etc/dirsrv` || { echo No instances found in /etc/dirsrv ; exit 1 ; }
    for i in $instances; do
        inst=`normalize_server_id $i`
        echo Starting instance \"$inst\"
        start_instance $inst
        rv=$?
        if [ $rv -ne 0 ]; then
            ret=$rv
        fi
    done
    exit $ret
else
    # We're starting a single instance.
    start_instance $@
    exit $?
fi
