#!/bin/sh
# mcp_server	This is the init script for starting up the Mammoth Replicator
#
# chkconfig: - 64 36
# description: Starts and stops the Mammoth Replicator Process
# processname: mcp_server
# pidfile: /var/run/mcp_server.pid

# Version 1.0 Devrim Gunduz <devrim@CommandPrompt.com>
# Initial script
# Version 1.1 Devrim Gunduz <devrim@CommandPrompt.com>
# Fixed many bugs

# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`

# Get config.
. /etc/sysconfig/network

# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
	NAME=${NAME:3}
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
    SU=runuser
else
    SU=su
fi

# Set defaults for configuration variables
MCPENGINE=/usr/bin
MCPTXLOGPATH=/var/lib/pgsql/data/mcp_data
MCPLOG=/var/lib/pgsql/data/mcp_data/mcp_server.log

export MCPTXLOGPATH

# Check that networking is up.
# Pretty much need it for mcp_server.
[ "${NETWORKING}" = "no" ] && exit 0

[ -f "$MCPENGINE/mcp_server" ] || exit 1

script_result=0

start(){
	MCP_START=$"Starting ${NAME} service: "

	# If mcp_server is already running, then don't attempt to start it:
	pid=`pidof -s "$MCPENGINE/mcp_server"`
	if [ $pid ] 
	then
		echo "mcp_server is already running."
		failure "$MCP_START"
		echo_warning
		echo
		script_result=1
		exit
	fi

	# Make sure startup-time log file is valid
	if [ ! -e "$MCPLOG" -a ! -h "$MCPLOG" ]
	then
		touch "$MCPLOG" || exit 1
		chown postgres:postgres "$MCPLOG"
		chmod go-rwx "$MCPLOG"
		[ -x /usr/bin/chcon ] && /usr/bin/chcon -t postgresql_log_t "$MCPLOG"
	fi

	if [ ! -e "$MCPTXLOGPATH/mcp_server.conf" ]
	then
		echo "mcp_server.conf file is missing."
		failure "$MCP_START"
		echo
		script_result=1
		exit
	fi

	echo -n "$MCP_START"
	$SU -l postgres -c "$MCPENGINE/mcp_ctl -D $MCPTXLOGPATH start &" >> "$MCPLOG" 2>&1 < /dev/null
	sleep 2
	pid=`pidof -s "$MCPENGINE/mcp_server"`
	if [ $pid ] 
	then
		success "$MCP_START"
		touch /var/lock/subsys/${NAME}
		echo
	else
		failure "$MCP_START"
		echo
		script_result=1
	fi
}

stop(){
	echo -n $"Stopping ${NAME} service: "
	pid=`pidof -s "$MCPENGINE/mcp_server"`
	if [ ! $pid ]
        then
	echo "No mcp process running!"
	echo_failure
	script_result=1
	exit
	fi

        killproc mcp_server
	ret=$? 
	if [ $ret -eq 0 ]
	then
		echo_success
	else
		echo_failure
		script_result=1
	fi
	echo
	rm -f "/var/run/mcp_server.${PGPORT}.pid"
	rm -f "/var/lock/subsys/${NAME}"
}

restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/${NAME} ] && restart
}

condstop(){
    [ -e /var/lock/subsys/${NAME} ] && stop
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status mcp_server
	script_result=$?
	;;
  restart)
	restart
	;;
  condrestart)
	condrestart
	;;
  condstop)
	condstop
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop}"
	exit 1
esac

exit $script_result
