#! /bin/sh
#
# chkconfig: 2345 97 03
# description: Hinemos PostgreSQL

#Copyright (C) 2006, 2007 NTT DATA Corporation
#
#This program is free software; you can redistribute it and/or
#Modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation, version 2.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details

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


. /opt/hinemos/hinemos.cfg

PROG="hinemos_pg"
USER=hinemos


# Parse command line parameters.
case $1 in
  start)
        echo -n "Starting $PROG: "
        daemon --user=${USER} "${PG}/bin/pg_ctl start -D '${PGDATA}' -s -l ${HINEMOS_HOME}/var/log/postgresql.log"
	echo ""
        ;;
  stop)
        echo -n "Stopping $PROG: "
	if [ -f ${HINEMOS_JBOSS_PID} ];  then
                 echo_failure
                 echo ""
                 echo "hinemos_jboss is running"
        else 
        	su - $USER -c "${PG}/bin/pg_ctl stop -D '${PGDATA}' -s -m fast"
		RETVAL=$?
        	if [ $RETVAL -eq 0 ] ; then
                	echo_success
                	echo ""
        	else
                	echo_failure
                	echo ""
			return $RETVAL
        	fi
	fi	
        ;;
  restart)
        echo -n "Restarting $PROG: "
        su - $USER -c "${PG}/bin/pg_ctl restart -D '${PGDATA}' -s -m fast -l ${HINEMOS_HOME}/var/log/postgresql.log"
	RETVAL=$?
         if [ $RETVAL -eq 0 ] ; then
                echo_success
                echo ""
        else
                echo_failure
                echo ""
        fi
        ;;
  status)
        su - $USER -c "${PG}/bin/pg_ctl status -D '${PGDATA}'"
        ;;
  *)
        # Print help
        echo "Usage: $0 {start|stop|restart|status}" 1>&2
        exit 1
        ;;
esac

exit 0
