#!/bin/sh
#
# /etc/init.d/monetdb
#
# postgresql	This is the init script for starting up the MonetDB
# chkconfig: - 85 15
# description: Starts and stops the MonetDB backend daemon that handles \
#	       all database requests.
# processname: monetdb
# pidfile: /var/lib/monetdb5/dbfarm/merovingian.pid

### BEGIN INIT INFO
# Provides: monetdb monetdbd merovingian
# Required-Start: $local_fs $remote_fs $network $time
# Required-Stop: $local_fs $remote_fs $network $time
# Should-Start: $syslog
# Should-Stop: $syslog
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: MonetDB
# Description: MonetDB is an open source column-oriented database management system
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

RETVAL=0
LOCKFILE=/var/lock/subsys/monetdb
DBFARM=/var/lib/monetdb5/dbfarm
PIDFILE="$DBFARM/merovingian.pid"

initdb()
{
	action "Creating database farm:" su -l _monetdb -s /bin/sh -c "\"monetdbd create \"\"$DBFARM\"\" && monetdbd set logfile=/var/log/monetdb/monetdb.log \"\"$DBFARM\"\" \""
	RETVAL=$?
	[ "$RETVAL" -ne 0 ] && return $RETVAL
}

start()
{
	msg_starting monetdb
	if [ -d "$DBFARM" ]; then
		su -l _monetdb -s /bin/sh -c "/usr/bin/monetdbd start $DBFARM && /usr/bin/monetdb start -a" >/dev/null 2>&1
		RETVAL=$?
		[ "$RETVAL" -ne 0 ] && touch $LOCKFILE
		echo
		echo_success
		return $RETVAL
	else
		echo
		echo "$DBFARM is missing. Use \"service monetdb initdb\" to initialize the database farm."
		echo_failure
		echo
		RETVAL=1
		return $RETVAL
	fi
}

stop()
{
if [ -f "$PIDFILE" ]; then
	msg_stopping monetdb
	if su -l _monetdb -s /bin/sh -c "/usr/bin/monetdb stop -a && /usr/bin/monetdbd stop $DBFARM && sleep 10" >/dev/null 2>&1; then
		rm -f "$LOCKFILE"
		echo_success
	else
		echo_failure
	fi
else
	msg_not_running monetdb
	echo_passed
fi
	echo
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	initdb)
		initdb
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --expect-user _monetdb monetdbd
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|initdb|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL

