#! /bin/sh

### BEGIN INIT INFO
# Provides:          iw_ldap_proxy
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Starts the inWebo ldap proxy
### END INIT INFO

set -o errexit

DIR=@iw_ldap_proxy_installation_dir@
RUN="$DIR"/bin/run_standalone.sh
STOP="$DIR"/bin/stop.sh
USER=@iw_ldap_proxy_user@

test -x "$RUN" || {
    echo "ERROR: run.sh not found: $DIR"
    exit 1
}

test -x "$STOP" || {
    echo "ERROR: stop.sh not found: $DIR"
    exit 1
}

getent passwd "$USER" >/dev/null || {
    echo "ERROR: System user not found: $USER"
    exit 2
}

case "$1" in
  start)
    test -x "$RUN" || exit 0
    echo "Starting iw_ldap_proxy..."
    su - $USER -c "$RUN" &
    sleep 2
    echo "done."
    ;;
  stop)
    test -x "$STOP" || exit 0
    echo "Stopping iw_ldap_proxy..."
    su - $USER -c "$STOP"
    echo "done."
    ;;
  force-reload|restart)
    "$0" stop
    "$0" start
    ;;
  status)
    if [ $(ps -u $USER -f |grep "iwLdapProxy.jar" |grep -v grep |wc -l) -eq 1 ]
    then
        echo "iw_ldap_proxy is up"
    else
        echo "iw_ldap_proxy is down"
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|force-reload|status}"
    exit 1
esac

exit 0