#!/bin/sh

##############################################################################
#                                                                            #
# Copyright (c) 2015 by R1Soft All rights reserved                           #
#                                                                            #
##############################################################################
#
# chkconfig: 2345 99 99
# description: R1Soft Document Storage Service
# processname: docstore

### BEGIN INIT INFO
# Provides:          docstore
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts R1Soft Document Service
# Description:       Starts R1Soft Document Service
### END INIT INFO


INSTALLATION_DIR=/opt/r1soft/docstore

export R1SOFT_HOME=/opt/r1soft/docstore
export GOMAXPROCS=2

STATUSCODE=0
ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

checkprocess() {
	FOUND=`ps -ef | grep /opt/r1soft | grep docstore | grep -v docstore-service | grep -v grep | grep -v rpm`
	if [ -n "$FOUND" ]; then
		PID=`echo $FOUND | awk '{print $2}'`
		if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
		    STATUS="docstore (pid $PID) running"
		    RUNNING=1
		    STATUSCODE=0
		else
		    STATUS="docstore (pid $PID?) not running"
		    RUNNING=0
		    STATUSCODE=1
		fi
	else
		STATUS="docstore (no pid file) not running"
		RUNNING=0
		STATUSCODE=3
	fi
}

start() {
	checkprocess
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $ARG: docstore (pid $PID) already running"
	else
		nohup /opt/r1soft/docstore/bin/docstore -port 446 -keepHistory 1 > /dev/null 2>&1 &
		echo "$0 $ARG: docstore started"
	fi
}

stop() {
	checkprocess
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: $STATUS"
	else
		if kill $PID ; then
			count=300
			while ([ $count != 0 ]) do
				count=`/usr/bin/expr $count - 1`
				if ps -p $PID > /dev/null ; then
					echo -n .
					sleep 1
				else
					count=0
				fi
			done 
	    		echo "$0 $ARG: docstore stopped"

			if [ -f "/var/lock/subsys/docstore" ]; then
                                rm -f "/var/lock/subsys/docstore" 2>/dev/null
                        fi

		else
	    		echo "$0 $ARG: docstore could not be stopped"
	    		ERROR=1
		fi
	fi
}

restart() {
	checkprocess
        if [ $RUNNING -eq 0 ]; then
            	echo "$0 $ARG: docstore not running, trying to start"
		start
        else
	    	stop
	    	start
        fi
}

status() {
	checkprocess
        echo "$0 $ARG: $STATUS"
	exit $STATUSCODE
}

help() {
	echo "usage: $0 (start|stop|restart|status|help)"
	cat <<EOF

start      - start docstore
stop       - stop docstore
restart    - stop docstore if running then start again, 
             if docstore is not running it is started
status     - print status
help       - this screen

EOF
	ERROR=1
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
	status
	;;
  restart)
	restart
        ;;
  *)
	help
	;;
esac

exit $ERROR

