#!/bin/sh

# Start or stop openerp server (odoo)

# Note: openerp is used as a naming convention instead of odoo
# primarily because the application that is launched and shows
# up in the process list is still called openerp.

# I really wanted to use the routines in rc.subr but they break
# when the name of a service includes a dash (i.e. openerp-server).

# PROVIDE: openerpserver
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="openerp-server"
app=/home/odoo/odoo/${name}
pidfile="/var/run/${name}.pid"

load_rc_config ${name}

openerpserver_enable=${openerpserver_enable:-"NO"}
openerpserver_user=${openerpserver_user:-"odoo"}
openerpserver_config=${openerpserver_config:-"/usr/local/etc/openerp-server.conf"}
openerpserver_flags=${openerpserver_flags:-"--config=$openerpserver_config --pidfile=$pidfile"}

rcvar=openerpserver_enable

status_cmd="openerpserver_status"
start_cmd="openerpserver_start"
stop_cmd="openerpserver_stop"

openerpserver_status()
{
    # if pid file exists, read it to get the pid
    if [ -f "${pidfile}" ]; then
        openerpd_pid="$(cat "${pidfile}")"
    fi

    if [ -z "${openerpd_pid}" ]; then
        # pid file present but nothing in it
        # look in process table for something matching the server name
        # and if found then write out a new pid file to reflect the
        # fact that the server is still running
        openerpd_pid=$(pgrep -f "${name} ${openerpd_flags}")

        # echo the pid we found in the process table to the file
        [ -n "${openerpd_pid}" ] && echo "${openerpd_pid}" > "${pidfile}"
    fi

    if [ -n "${openerpd_pid}" ]; then
        # pid file present and it contains a pid, report process running
        echo "${name} running with pid: $openerpd_pid"
        return 0
    else
        echo "${name} not running"
        return 1
    fi
}

openerpserver_start()
{
    if ! checkyesno openerpserver_enable ; then
	echo "${name} is disabled."
        return 0
    fi
    
    echo "Starting ${name}."

    app_command="nohup ${app} $openerpserver_flags"

    #echo "debug: app_command = $app_command"
   
    # if given a --pidfile option openerp-server will write its pid
    # to that file so we don't have to create it here. 
    su -l $openerpserver_user -c "nohup $app_command > /dev/null 2>&1 &"
   
    sleep 2
 
    if [ -f $pidfile ]; then
     	echo "${name} started with PID `cat $pidfile`."
    else
       echo "Tried to start ${name} but could not obtain pid."
    fi
}

openerpserver_stop()
{
    if [ -f "${pidfile}" ]; then 
	# pid file exists so look in it to get the pid
        openerpserver_pid="$(cat "${pidfile}")"
    fi

    if [ -z "${openerpserver_pid}" ]; then
        # pid file present but nothing in it (error condition)
        # look in process table for something matching the server name
        openerpserver_pid=$(pgrep -f "${app} ${openerpserver_flags}")
    fi
   
    if [ -n "${openerpserver_pid}" ]; then
        # okay, we have a pid so kill the process
        echo "Sending SIGTERM to PID $openerpserver_pid..."
        # for some reason openerp-server acknowledges a QUIT signal but 
        # then fails to terminate and asks for a second QUIT signal.
        kill -15 ${openerpserver_pid}
	sleep 2
        kill -15 ${openerpserver_pid}

        # I also noticed that the application takes what I consider an
	# abnormally long time to shutdown and remove itself from the 
	# process table.  This takes anywhere from 10-45 seconds,
	# apparently depending on whether anyone is logged in.  Hence
	# we spin on it.  And thankfully in this case we can use the
	# upstream rc.subr wait routine.
	echo "Waiting for $name to shutdown..."
	wait_for_pids ${openerpserver_pid}
	echo "$name is now terminated."

        # the process does not delete or clear the pid file so we have to
        # do it here instead. 
    	#rm -f ${pidfile}
    else
        echo "${name} not running (could not find pid)"
    fi

    # the process does not delete or clear the pid file so we have to
    # do it here instead. 
    rm -f ${pidfile}
}

run_rc_command "$1"
