#!/bin/bash
PID=`pidof -o %PPID /usr/sbin/slapd`
case "$1" in
  start)
    echo "Starting OpenLDAP"
    if [ -z "$PID" ]; then
      if [ -z "$SLAPD_SERVICES" ]; then
        /usr/sbin/slapd $SLAPD_OPTIONS
      else
        /usr/sbin/slapd -h "$SLAPD_SERVICES" $SLAPD_OPTIONS
      fi
      if [ $? -gt 0 ]; then
        echo "OpenLDAP start failed!"
      fi
    else
      echo "another OpenLDAP process running!"
    fi
    ;;
  stop)
    echo "Stopping OpenLDAP"
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      echo "OpenLDAP stop failed!"
    else
      rm -f /var/run/openldap/slapd.pid
      rm -f /var/run/openldap/slapd.args
      echo "OpenLDAP stopped"
    fi
    ;;
  restart)
    $0 stop
    sleep 3
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0

