#!/bin/bash
# openerp-server        This shell script takes care of starting and stopping
#                       OpenERP server
#
# chkconfig: 345 95 05
# description: Odoo server
#
# pidfile: /var/run/odoo/odoo-server.pid
# config: /etc/odoo.conf

### BEGIN INIT INFO
# Provides: openerp-server
# Required-Start: <%= @postgresServiceName %>
# Required-Stop: <%= @postgresServiceName %>
# Should-Start: $network harddrake
# Default-Start: 345
# Short-Description: Launches the Odoo server.
# Description: This startup script launches the Odoo server.
### END INIT INFO
PATH=/bin:/sbin:/usr/bin
DAEMON=/opt/bahmni-erp/odoo-bin

# Source function library.
. /etc/rc.d/init.d/functions

PIDFILE=/var/run/odoo/odoo-server.pid
LOCKFILE=/var/lock/subsys/odoo-server
LOGFILE=/var/log/odoo/odoo.log

prog="odoo-server"
desc="Odoo Server Daemon"

SHUTDOWN_WAIT=20

cd /var/run
mkdir odoo
chown odoo:odoo odoo

# Source function library
if [ -f /etc/init.d/functions ] ; then
    . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
else
 exit 0
fi

# check the existence of the openerp-server script
[ -z "/usr/bin/odoo" ] && exit 0

RETVAL=0

start() {
    if [[ -f $PIDFILE ]] ; then
        echo "$desc already started."
        return 0
    fi

    if [ -d /etc/odoo/start.d ] ; then
        echo -n $"Preparing $desc: "
        run-parts --exit-on-error /etc/odoo/start.d
        RETVAL=$?
        echo
        [ $RETVAL -ne 0 ] && return $RETVAL
    fi

    echo -n $"Starting $desc ($prog): "
    daemon --user odoo --check odoo-server \
    "/usr/bin/setsid /usr/bin/odoo \
    -c /etc/odoo.conf \
    --update all \
    --database odoo \
    --without-demo all \
    --pidfile=$PIDFILE \
    --logfile=$LOGFILE &"

    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $LOCKFILE
    return $RETVAL
}

openerp_pid() {
    echo `pgrep -f /usr/bin/odoo`
}

ensure_stopped() {
    pid=$(openerp_pid)
    echo $pid
    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      rm -rf $PIDFILE
      kill -9 $pid
    fi
}

stop() {
    if [[ ! -f $PIDFILE ]] ; then
        echo "$desc already stopped."
        return 0
    fi
    echo -n $"Stopping $desc ($prog): "
    kill -TERM `cat $PIDFILE` > /dev/null 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        rm -f $LOCKFILE
        ensure_stopped
        echo_success
        echo
    else
        echo_failure
        echo
    fi
    if [ -d /etc/odoo/stop.d ] ; then
        echo -n $"Clearing $desc: "
        run-parts /etc/odoo/stop.d
        echo
    fi
    return $RETVAL
}

restart() {
    stop
    start
}

condrestart() {
    [ -e $LOCKFILE ] && restart || :
}

status() {
    if [ -f $PIDFILE ] ; then
        checkpid `cat $PIDFILE`
        RETVAL=$?
        if [ $RETVAL -eq 0 ] ; then
            echo  $"$prog is running..."
        else
            echo  $"$prog is stopped"
        fi
    else
        echo  $"$prog is stopped"
    fi
    return $RETVAL
}

case "$1" in
start)
    start
    ;;

stop)
    stop
    ;;

restart|reload)
    restart
    ;;

condrestart)
    condrestart
    ;;

status)
    status
    ;;

probe)
    exit 0
    ;;

*)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
    exit 1
esac
