#!/bin/sh
#
# 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>
#
# Changes:
#	Wenzhuo Zhang		:	fixed the typo of failure function 
#	LAKostis <lakostis@>	:	rewrite 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
if [ -f "/etc/sysconfig/ipvsadm"  ]; then
  IPVSADM_CONFIG="/etc/sysconfig/ipvsadm"
elif [ -f "/etc/ipvsadm.rules"  ]; then
  IPVSADM_CONFIG="/etc/ipvsadm.rules"
else
  IPVSADM_CONFIG="/etc/sysconfig/ipvsadm"
fi

RETVAL=0

start()
{
	action "Applying IPVS configuration: "
	ipvsadm-restore < "$IPVSADM_CONFIG" && \
	success "Applying IPVS configuration" || \
	failure "Applying IPVS configuration"
	RETVAL=$?
	return $RETVAL
}

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

save()
{
	action "Saving IPVS table to $IPVSADM_CONFIG: "
	ipvsadm-save -n > $IPVSADM_CONFIG  2>/dev/null && \
	success "Saving IPVS table to $IPVSADM_CONFIG" || \
	failure "Saving IPVS table to $IPVSADM_CONFIG"
	RETVAL=$?
	return $RETVAL
}

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

	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
	action "Clearing the current IPVS table:" ipvsadm -C
	;;
	condstop)
	if [ -e "$LOCKFILE" ]; then
		stop
	fi
	;;
	condreload|condrestart)
	if [ -e "$LOCKFILE" ]; then
		restart
	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

