#!/bin/sh
LANG=de_DE.UTF-8
JAVA_HOME=/usr/lib64/jvm/java
PATH=$PATH:/opt/open-xchange/sbin
export PATH LANG JAVA_HOME
unset LC_ALL LC_MESSAGES

cd $(dirname $0)
if [ -r /var/run/ldapsync.pid ]
then
  oldpid=$(cat /var/run/ldapsync.pid)
  if kill -0 $oldpid 2>/dev/null
  then
	status="pid $oldpid is running"
	exitcode=0
  else
	status="pid $oldpid is not running"
	exitcode=1
  fi
else
	status="no pidfile found"
	exitcode=1
fi

if [ "$1" = "status" ]
then
	echo $status
	exit $exitcode
fi

if [ "$exitcode" = 0 ]
then
	kill $oldpid
	sleep 3
fi

if [ "$1" = "stop" ]
then
	exit 0
fi

exec python -u main.py server 8098 >>/var/log/ldapsync.log 2>&1 &
echo $! >/var/run/ldapsync.pid
