#!/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
SYSTEMD_ENABLE=0

if [ ${SYSTEMD_ENABLE} -eq 1 ] ; then
    systemctl stop dirsrv-admin.service
else

    PID_FILE=/var/run/dirsrv/admin-serv.pid
    if test -f $PID_FILE ; then
        kill -TERM `cat $PID_FILE`
        if test $? -ne 0 ; then
            exit 1
        fi
    else
        echo server not running
        exit 1
    fi

    loop_counter=1
    max_count=30
    while test $loop_counter -le $max_count; do
        loop_counter=`expr $loop_counter + 1`
        if test -f $PID_FILE ; then
            sleep 2
        else
            exit 0
        fi
    done
    echo server not responding to exit command
    echo killing process group
    kill -9 `cat $PID_FILE`
    rm $PID_FILE

    exit 1

fi
