#!/bin/sh
# postgresql    This is the init script for starting up the Slony-I
#
# chkconfig: - 64 36
# description: Starts and stops the Slon daemon that handles
#              Slony-I replication.
# processname: slon
# pidfile:	/var/run/slony1-11/slon.pid
#
# v1.0.0 Devrim GUNDUZ <devrim@gunduz.org>
# - Initial version of Red Hat / Fedora init script, based on Ubuntu one.
#
# v2.0.0 Devrim GÜNDÜZ <devrim@gunduz.org>
# - Multiple fixes for multiple postmaster feature.
# - Start logging to startup logging file.
#
# v2.0.1 Devrim GÜNDÜZ <devrim@gunduz.org>
# Fix init script so that it reads the conninfo
# correctly. Per Tomonari Katsumata.
#
# v2.0.2.RS Rob Brucks <rob.brucks@rackspace.com>
# Heavily modified as needed:
#  - moved sysconfig file override code to proper location
#  - fixed "status" to check for daemon based on pidfile
#  - added code to prevent multiple daemons from starting for the same configs
#  - changed code to leverage /etc/init.d/functions
#  - added code to allow for multiple concurrent daemons, pids, and configs based on script name
#  - added code to clean up pid file in case of daemon crash
#  - changed "stop" command to only kill the watchdog daemon based on associated pid file
#  - set return codes to Linux LSB init script standards
#      per http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html


# 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

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

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

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

# Set defaults for configuration variables
SLONENGINE=/usr/pgsql-11/bin
SLONDAEMON=$SLONENGINE/slon
SLONCONF=/etc/${NAME}/slon.conf
SLONPID=/var/run/slony1-11/${NAME}.pid
SLONLOGDIR=/var/log/${NAME}

# read in overrides for defaults
if [ -r /etc/sysconfig/${NAME} ]; then
    . /etc/sysconfig/${NAME}
fi

test -x $SLONDAEMON || exit 5


RETVAL=0
RC=0

start(){
	echo -n "Starting ${NAME} service: "
	if [ $EUID -ne 0 ]; then
		RC=4
		failure
	else
		# if pidfile exists and slony is running, then return
		if [ -f $SLONPID ]; then
			if [[ -n `slon_status | grep "running"` ]]
			then
				echo
				return 0
			fi
		fi

		$SU -l postgres -c "$SLONDAEMON -f $SLONCONF -p $SLONPID $SLONCLUSTERNAME \"$SLONCONNINFO\" &" >> "$SLONLOGDIR/slonystartup.log" 2>&1 < /dev/null
		sleep 2
		RC=$?
	
		if [[ $RC -eq 0 && -n `slon_status | grep "running"` ]]
		then
			success
			touch /var/lock/subsys/${NAME}
		else
			failure
			rm -f "/var/lock/subsys/${NAME}"
			RC=7
		fi
	fi
	echo
	return $RC
}

stop(){
	echo -n $"Stopping ${NAME} service: "
	if [ $EUID -ne 0 ]; then
		RC=4
		failure
	else
		if [ -f $SLONPID ]; then
			killproc -p $SLONPID slon
			RC=$?
			[ $RC -eq 0 ] && rm -f "$SLONPID"
		else
			failure
		fi
		rm -f /var/lock/subsys/${NAME}
	fi
	echo
	return $RC
}

restart(){
	stop
	start
	return $?
}

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

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

slon_status(){
	status -p $SLONPID $SLONDAEMON
	return $?
}

# See how we were called.
	case "$1" in
	start)
		start
		RETVAL=$?
		;;
	stop)
		stop
		RETVAL=$?
		;;
	status)
		slon_status
		RETVAL=$?
		;;
	reload)
		echo 'reload not implemented'
		RETVAL=3
		;;
	restart)
		restart
		RETVAL=$?
		;;
	condrestart)
		condrestart
		RETVAL=$?
		;;
	condstop)
		condstop
		RETVAL=$?
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop}"
		RETVAL=2
	esac

exit $RETVAL
