#!/bin/sh
#
# Startup script handle the initialisation of LVS
#
# chkconfig: - 11 89
#
# description: Initialise the Linux Virtual Server
#              http://www.linuxvirtualserver.org/
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Initialise the Linux Virtual Server
# Short-Description: start and stop ipvsadm
### END INIT INFO
#
# Script Author: Horms <horms@vergenet.net>
#
# Based on init script for ipchains by Joshua Jensen <joshua@redhat.com>
#
# Changes:
#	Wenzhuo Zhang		:	fixed the typo of failure function 
#	LAKostis <lakostis@>	:	rewrite for ALTLinux
#	dubrsl <dubrsl@>	:	fixed for ALTLinux
#
# config: /etc/sysconfig/ipvsadm
# config: /etc/ipvsadm.rules

WITHOUT_RC_COMPAT=1

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

# set the configuration file
IPVSADM_CONFIG="/etc/sysconfig/ipvsadm"
LOCKFILE=/var/lock/subsys/ipvsadm

RETVAL=0

start()
{
	echo -n "Applying IPVS configuration: "
	ipvsadm-restore < "$IPVSADM_CONFIG"
	RETVAL=$?
	if [ "$RETVAL" -eq 0 ] ; then
	    echo_success
	    echo
	elif [ "$RETVAL" -ne 0 ] ; then
	    echo_failure
	    echo
	fi
	return $RETVAL
}

stop()
{
	action "Clearing the current IPVS table:" ipvsadm -C
	RETVAL=$?
	return $RETVAL
}

save()
{
	echo -n "Saving IPVS table to $IPVSADM_CONFIG:"
	ipvsadm-save -n > $IPVSADM_CONFIG  2>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 0 ] ; then
	    echo_success
	    echo
	elif [ "$RETVAL" -ne 0 ] ; then
	    echo_failure
	    echo
	fi
	return $RETVAL
}

case "$1" in
	start)
	    start
	[ $RETVAL -eq 0 ] && touch $LOCKFILE
	;;

	stop)
	    stop
	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	;;

	reload|restart)
	    stop
	    start
	;;

	panic)
	# I'm not sure what panic does but in the case of IPVS	
	# it makes sense just to clear everything
	    stop
	;;

	condstop)
	if [ -e "$LOCKFILE" ]; then
	    stop
	fi
	;;

	condreload|condrestart)
	if [ -e "$LOCKFILE" ]; then
	    stop
	    start
	fi
	;;

	status)
	    ipvsadm -L -n
	;;

	save)
	    save
	;;

	*)
	    msg_usage "${0##*/} {start|stop|restart|reload|condstop|condreload|condrestart|status|panic|save}"
	RETVAL=1
esac

exit $RETVAL

