#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#
# Meru configuration
#
# script used to turn on/off NAT
#

PROC=/opt/meru/bin/meru-nat-masq
MNAME=`basename $PROC`
LOG=/opt/meru/var/log/${MNAME}.log

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/meru/bin
export PATH

if [ -f /etc/init.d/meru-functions ]; then
	. /etc/init.d/meru-functions
else
	echo "/etc/init.d/meru-functions must exist to use $0"; exit 1
fi

set_myparams()
{
        set_params

        [ "$REAL_IF" ] || fail "Cannot determine interface"
}

start() {
	[ ! -f /opt/meru/var/run/meru-nat-masq ] && {
		touch /opt/meru/var/run/meru-nat-masq

		iptables -t nat -A POSTROUTING -o $REAL_IF -j MASQUERADE >> $LOG 2>&1
	}
}

stop() {
	[ -f /opt/meru/var/run/meru-nat-masq ] && {
		iptables -t nat -D POSTROUTING -o $REAL_IF -j MASQUERADE >> $LOG 2>&1
		rmmod ipt_MASQUERADE iptable_nat ip_conntrack >> $LOG 2>&1

		rm -f /opt/meru/var/run/meru-nat-masq
	}
}

status() {
	iptables -t nat -L POSTROUTING 2>&1 | grep MASQUERADE | tee -a $LOG
}	

#
# main()
#
case "$1" in
 start)
	set_myparams
 	start
	;;
 stop)
 	stop
	;;
 status)
 	status
	;;
 *)
	echo $"Usage: $0 {start|stop|status}"
	exit 1
	;;
esac

exit $?
