#! /bin/bash
#
# Startup script handle the initialisation of LVS
#
# chkconfig: - 08 92
#
# description: Initialise the Linux Virtual Server
#              http://www.linuxvirtualserver.org/
#
# Script Author: Horms <horms@vergenet.net>
#
# Based on init script for ipchains by Joshua Jensen <joshua@redhat.com>
#
# config: /etc/sysconfig/ipvsadm
# config: /etc/ipvsadm.rules


# Exit silently if there is no configuration file
if [ -f /etc/sysconfig/ipvsadm ]; then
	IPVSADM_CONFIG="/etc/sysconfig/ipvsadm"
elif [ -f /etc/ipvsadm.rules  ]; then
	IPVSADM_CONFIG="/etc/ipvsadm.rules"
else
	exit 0
fi

# Use the funtions provided by Red Hat or use our own
[ -f /etc/rc.d/init.d/functions ] || exit 0
. /etc/rc.d/init.d/functions

[ ! -x /sbin/ipvsadm -a ! -x /usr/sbin/ipvsadm ] || exit 0

function start() {
	action $"Clearing the current IPVS table:" ipvsadm -C
	echo -n $"Applying IPVS configuration: "
		grep -v "^#" $IPVSADM_CONFIG | ipvsadm-restore -p -f && \
		success $"Applying IPVS configuration" || \
		failure $"Applying IPVS configuration"
	echo
	touch /var/lock/subsys/ipvsadm
}

function stop() {
	action $"Clearing the current IPVS table:" ipvsadm -C
	rm -f /var/lock/subsys/ipvsadm
}

case "$1" in
	start)
		start
		;;
  stop)
		stop
		;;
  reload|reload-force|restart)
		start
		;;
	condrestart)
		if [ -f /var/lock/subsys/ipvsadm ]; then
			stop
			start
		fi
	;;
  panic)
		action $"Clearing the current IPVS table:" ipvsadm -C
	;;

  status)
		ipvsadm -L -n
	;;

  save)
		echo -n $"Saving IPVS table to $IPVSADM_CONFIG: "
		ipvsadm-save > $IPVSADM_CONFIG  2>/dev/null && \
	  success $"Saving IPVS table to $IPVSADM_CONFIG" || \
	  failure $"Saving IPVS table to $IPVSADM_CONFIG"
		echo
		;;
  *)
		echo "Usage: $0 {start|stop|restart|status|panic|save|reload|reload-force}"
		exit 1
esac

exit 0

