### BEGIN INIT INFO
# Provides:          umapi-mimercv
# Required-Start:    umapi-server
# X-UnitedLinux-Should-Start: $ALL
# Required-Stop:     umapi-server
# X-UnitedLinux-Should-Stop: $ALL
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: umapi receive process
# Description:       umapi receive process
### END INIT INFO#
#####################################################################
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 89 21
# description: umapi receive process
#
#####################################################################

NAME=umapi-mimercv
#
# umapi Initialisation-Script
# Author:			 E. Bergter, VIPcom GmbH
# based on postfix's init.d script, Ubuntu 
#
set +x

#
# Source function library.
#
# get the message flavor
if [ -f /lib/lsb/init-functions ]; then
	. /lib/lsb/init-functions
	type log_failure_msg 1>/dev/null 2>/dev/null && DISTRO=LSB
	type log_end_msg 1>/dev/null 2>/dev/null && DISTRO=LSB_ENH
else
	if [ -f /etc/rc.d/init.d/functions ]; then 
		. /etc/rc.d/init.d/functions
		DISTRO=RedHat
	fi
fi

umapi_action_msg() {
case "$DISTRO" in
	LSB_ENH)
		log_action_msg "$1"
		;;
	*)
		echo "$1"
		;;
esac
}

umapi_failure_msg() {
case "$DISTRO" in
	LSB | LSB_ENH)
		log_failure_msg "$1"
		;;
	*)
		echo "$1" FAILED
		;;
esac
}

umapi_daemon_msg() {
case "$DISTRO" in
	LSB_ENH)
		log_daemon_msg "$1"
		;;
	*)
		echo "$1"
		;;
esac
}

umapi_success() {
case "$DISTRO" in
	LSB)
		log_success_msg
		;;
	LSB_ENH)
		log_end_msg 0
		;;
	RedHat)
		echo_success
		echo
		;;
	*)
		echo OK
		;;
esac
}

umapi_failure() {
case "$DISTRO" in
	LSB)
		log_failure_msg
		;;
	LSB_ENH)
		log_end_msg 1
		;;
	RedHat)
		echo_failure
		echo
		;;
	*)
		echo FAILED
		;;
esac
}

############################################
USER=conversations
HOME=~conversations

if [ -f $HOME/etc/umapi.env ]; then
	. $HOME/etc/umapi.env
fi
if [ -z "$TXCROOT" ]; then
	umapi_failure_msg "missing umapi environment"
	exit 1
fi

START_LOG=/var/log/umapi.log		# Path to Logfile
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$TXCROOT/bin
NAME_SERVER=umapi-server
NAME_RCV=umapi-mimercv
NAME_SND=umapi-mimesnd
NAME_IMAP4=umapi-imap4d
PIDFILE=$TXCROOT/pid/$NAME.pid
WRAPPER=$TXCROOT/bin/wrapper
POSTGRES=postgresql
STATUSFILE=$TXCROOT/pid/$NAME_SERVER.sta

SUBDAEMON="$NAME_RCV $NAME_SND $NAME_IMAP4"

test -x $TXCROOT/bin/$NAME_SERVER || exit 1

script_result=0

# stellt fest, ob prozess laeuft, legt fehlendes pidfile an
running() {
	if [ -z "$1" ]; then
		return 0
	fi
	
    if [ -f $TXCROOT/pid/$1.pid ]; then
        PID=$(sed 's/ //g' $TXCROOT/pid/$1.pid)
		kill -0 $PID 2>/dev/null && echo y
    else 
    	if [ -f $TXCROOT/bin/$1 ]; then
			PID=$(pidof -s $TXCROOT/bin/$1)
    	else
    		PID=
			for i in $(pidof wrapper)
			do
				CHECKPID=$(ps -a $i 2> /dev/null | grep $1 | grep wrapper | wc -l)
				if [ "$CHECKPID" -eq 1 ]; then
					PID=$i
				fi
			done
		fi
		if [ -n "$PID" ]; then
			echo $PID > $TXCROOT/pid/$1.pid
			echo y
		fi
	fi
}

umapi_start_daemon() {
	if [ -z "$1" ]; then
		return 0
	fi
	
	rm -f $TXCROOT/pid/$1.pid
	case $1 in 
		"$NAME_IMAP4")
		# als root starten
			cd $TXCROOT/bin
			ulimit -c unlimited 
			$WRAPPER $1.wap >> $START_LOG 2>&1 
			sleep 1
			if [ -f $TXCROOT/pid/$1.pid ]; then 
				PID=$(sed 's/ //g' $TXCROOT/pid/$1.pid)
			fi
			;;
		"$NAME_SERVER")
			su -c "cd $TXCROOT/bin; ulimit -c unlimited; $TXCROOT/bin/$1 &" $USER >> $START_LOG 2>&1 < /dev/null
			sleep 1
			PID=$(pidof -s $TXCROOT/bin/$1)
			;;
		"$NAME_SND"|"$NAME_RCV")
			su -c "cd $TXCROOT/bin; ulimit -c unlimited; $WRAPPER $1.wap" $USER >> $START_LOG 2>&1 
			sleep 5
			if [ -f $TXCROOT/pid/$1.pid ]; then 
				PID=$(sed 's/ //g' $TXCROOT/pid/$1.pid)
			fi
			;;
	esac
	if [ -n "$PID" ]; then
		sleep 5
		CHECKPID=$(kill -0 $PID 2>/dev/null && echo ok)
		if [ "$CHECKPID" = "ok" ]; then
			echo $PID > $TXCROOT/pid/$1.pid
			if [ -d /var/lock/subsys ]; then
				touch /var/lock/subsys/$1
			fi
			echo y
		fi
	fi
	if [ "$1" = "$NAME_SERVER" ]; then
	  while 
	    read serverstatus server_ec < $STATUSFILE
	    test "$serverstatus" = starting
	  do
	    echo 'still waiting...' >&2
	    sleep 3
	  done
	  case "$serverstatus" in
	    running) 
	      ;;
	    exit) 
	      if test "$server_ec" = 80070062 
	      then 
	        echo "Error binding to server port!" >&2
	        echo "Try again later ..." >&2
	      else
	        echo "Error starting server, Errorcode = $server_ec" >&2
	      fi
	      ;;
	    *) echo "unknown Status: $serverstatus $server_ec" >&2
	      ;;
	  esac
	fi
	
}

umapi_stop_daemon() {
	if [ -z "$1" ]; then
		return 0
	fi
	
	if [ -f /var/lock/subsys/$1 ]; then
		rm /var/lock/subsys/$1
	fi
	if [ -f $TXCROOT/pid/$1.pid ]; then 
		PID=$(sed 's/ //g' $TXCROOT/pid/$1.pid)
	fi
	if [ -z "$PID" ]; then
		if [ -f /var/lock/subsys/$1 ]; then
			rm /var/lock/subsys/$1
		fi
		return 0
	fi
	
	if [ "$1" = "$NAME_IMAP4" ]; then
		# als root starten und stoppen
		kill $PID  >> $START_LOG 2>&1 < /dev/null
	else
		su -c "kill $PID" $USER>> $START_LOG 2>&1 < /dev/null
	fi
	sleep 5
	kill -0 $PID 2>/dev/null || echo y
}

case "$1" in
    start)
        RUNNING=$(running $NAME)
        if [ -n "$RUNNING" ]; then
            umapi_success
        else
			if [ "$NAME" = "$NAME_SERVER" ]; then
				test -f /etc/init.d/$POSTGRES && (/etc/init.d/$POSTGRES status || /etc/init.d/$POSTGRES start)
			else
				RUNNING_SERVER=$(running $NAME_SERVER)
				if [ -z "$RUNNING_SERVER" ]; then
					/etc/init.d/$NAME_SERVER start
				fi
			fi
			umapi_daemon_msg "Starting $NAME" 
            RUNNING=$(umapi_start_daemon $NAME)
			if [ -n "$RUNNING" ]; then
                umapi_success
            else
            	script_result=1
                umapi_failure
			fi
        fi
	;;
	
    status)
        RUNNING=$(running $NAME)
        if [ -n "$RUNNING" ]; then
           PID=$(sed 's/ //g' $TXCROOT/pid/$NAME.pid)
           umapi_daemon_msg "$NAME (pid $PID) is running"
           exit 0
        else
           umapi_daemon_msg "$NAME is not running"
           exit 3
        fi
    ;;
	
    stop)
        RUNNING=$(running $NAME)
        if [ -n "$RUNNING" ]; then
			if [ "$NAME" = "$NAME_SERVER" ]; then
				for i in $SUBDAEMON  ; do
				if [ -f /etc/init.d/$i ] ; then
					RUNNING=$(running $i)
					if [ -n "$RUNNING" ]; then
						/etc/init.d/$i stop
				fi; fi; done
			fi
			umapi_daemon_msg "Stopping $NAME"
			STOPPED=$(umapi_stop_daemon $NAME)
			if [ -n "$STOPPED" ]; then
				rm -f $PIDFILE
				umapi_success
			else
				umapi_failure_msg "Cannot stop $NAME"
			fi
		else
			rm -f $PIDFILE
			umapi_success
		fi
    ;;

    restart|force-reload|reload)
        $0 stop
        $0 start
		if [ "$NAME" = "$NAME_SERVER" ]; then
			RUNNING=$(running $NAME)
			if [ -n "$RUNNING" ]; then
				for i in $SUBDAEMON  ; do
				if [ -f /etc/init.d/$i ] ; then
					/etc/init.d/$i start
				fi; done
			fi
		fi
    ;;

    *)
        umapi_action_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}"
        exit 1
    ;;
esac

exit $script_result
  
