#! /bin/sh
#
# chkconfig: 2345 97 03
# description: Hinemos OpenLDAP

#Copyright (C) 2006, 2007 NTT DATA Corporation
#
#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.
#
#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

# Source function library.
. /etc/rc.d/init.d/functions

. /opt/hinemos/hinemos.cfg

PROG="hinemos_ldap"
USER=hinemos

start() {
        echo -n "Starting $PROG: "
        daemon --user=hinemos ${HINEMOS_HOME}/openldap-2.3.20/libexec/slapd -h "ldap://0.0.0.0:24000/"
        echo ""
}

stop() {
        echo -n "Stopping $PROG: "
	if [ -f ${HINEMOS_JBOSS_PID} ];  then 
		 echo_failure
		 echo ""
		 echo "hinemos_jboss is running"
		 return 1
	fi
        kill -INT `cat ${HINEMOS_LDAP_PID}`
       	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
                echo_success
		echo ""
        else
                echo_failure
		echo ""
        fi	
}


# Parse command line parameters.
case $1 in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
	start
        ;;
  status)
	if [ -f $HINEMOS_LDAP_PID ] ; then
		read LDAP_PID < $HINEMOS_LDAP_PID
		ps -p $LDAP_PID e | grep $HINEMOS_HOME > /dev/null && TMP=1
		if [ "$TMP" ] ; then
	        	echo "Hinemos OpenLDAP (pid $LDAP_PID) is running..."
        		exit 0
		else
			echo "Hinemos OpenLDAP is stopped"
			exit 3
		fi
	else
		echo "Hinemos OpenLDAP is stopped"
		exit 3
	fi
	;;
  *)
        # Print help
        echo "Usage: $0 {start|stop|restart|status}" 1>&2
        exit 1
        ;;
esac

exit 0
