#!/bin/bash

# radvd is the router advertisement daemon for IPv6.  It 
# listens to router solicitations and sends router 
# advertisements as described in "Neighbor Discovery for IP 
# Version 6 (IPv6)" (RFC 2461).  With these advertisements 
# hosts can automatically configure their addresses and some
# other parameters.  They also can choose a default router
# based on these advertisements.

# pidfile: /var/run/radvd/radvd.pid

. /etc/functions

RETVAL=0
PROG="radvd"
LOCKFILE=/tmp/run/radvd
RADVD_CONF="/etc/radvd.conf"


# See how we were called.
case "$1" in
   add) 
        # NOTE: all interfaces should use same config
        grep "interface $2" $RADVD_CONF 2>&1 >/dev/null
        if [ ! "$?" = 0 ]; then
            echo "# begin for interface $2" >> $RADVD_CONF
            echo "interface $2" >> $RADVD_CONF
            echo "{" >> $RADVD_CONF
            echo "        # inside begin for interface $2" >> $RADVD_CONF
            #echo "" >> $RADVD_CONF
            echo "        # header begin for interface $2" >> $RADVD_CONF 
            echo "        AdvSendAdvert on;" >> $RADVD_CONF
            echo "        MinRtrAdvInterval 3;" >> $RADVD_CONF
            echo "        MaxRtrAdvInterval 10;" >> $RADVD_CONF
            echo "        AdvHomeAgentFlag off;" >> $RADVD_CONF
            echo "        # header end for interface $2" >> $RADVD_CONF
            echo "" >> $RADVD_CONF
			index=1
			for arg in "$@"
			do
				if [ $index -ge 3 ]; then
					echo "        # begin of prefix $arg for interface $2" >> $RADVD_CONF
					echo "        prefix $arg {" >> $RADVD_CONF
					echo "            AdvOnLink on;" >> $RADVD_CONF
					echo "            AdvAutonomous on;" >> $RADVD_CONF
					echo "        };" >> $RADVD_CONF
					echo "        # end of prefix $arg for interface $2" >> $RADVD_CONF
				fi
                let "index+=1"
            done
			echo "        # inside end for interface $2" >> $RADVD_CONF
			echo "};" >> $RADVD_CONF
            echo "# end for interface $2" >> $RADVD_CONF
            sync
            /etc/init.d/radvd stop > /dev/null
            /etc/init.d/radvd start > /dev/null
        else
			index=1
            flag=0
			for arg in "$@"
			do
				if [ $index -ge 3 ]; then
					grep "prefix $arg" $RADVD_CONF 2>&1 >/dev/null
					if [ ! "$?" = 0 ]; then
						sed -i "s@# inside end for interface $2@# begin of prefix $arg for interface $2\n        prefix $arg {\n            AdvOnLink on;\n            AdvAutonomous on;\n        };\n        # end of prefix $arg for interface $2\n        # inside end for interface $2@g" $RADVD_CONF
                        flag=1
					fi
				fi
				let "index+=1"
			done
            sync
            #if [ $flag -eq 1 ]; then
            /etc/init.d/radvd stop > /dev/null
            /etc/init.d/radvd start > /dev/null
            #fi
        fi
        ;;
  delete)
        # NOTE: you must change the lines to delete according to configs
		sNum=$#
		if [ $sNum -le 2 ]; then 
			grep "# begin for interface $2" $RADVD_CONF 2>&1 >/dev/null
			if [ "$?" = 0 ]; then
				sed -i "/# begin for interface $2/,/# end for interface $2/d" $RADVD_CONF 
			else
				#echo "interface $2 does not exist."
				exit
			fi
            sync
            /etc/init.d/radvd stop > /dev/null
            /etc/init.d/radvd start > /dev/null
            RETVAL=$?
		else
			index=1
            flag=0
            for arg in "$@"
            do
                if [ $index -ge 3 ]; then
					grep "# begin of prefix $arg for interface $2" $RADVD_CONF 2>&1 >/dev/null
					if [ "$?" = 0 ]; then
						sed -i "s@# begin of prefix ${arg} for interface $2@# begin of prefix temp@g" $RADVD_CONF
						sed -i "s@# end of prefix ${arg} for interface $2@# end of prefix temp@g" $RADVD_CONF
						sed -i "/# begin of prefix temp/,/# end of prefix temp/d" $RADVD_CONF
						#sed -i '/# begin of prefix ${arg} for interface $2/,/# end of prefix ${arg} for interface $2/d' $RADVD_CONF
					    flag=1
					fi 
                fi
                let "index+=1"
            done
            sync
            #if [ $flag -eq 1 ]; then
            /etc/init.d/radvd stop > /dev/null
            /etc/init.d/radvd start > /dev/null
            RETVAL=$?
            #fi
		fi
        ;;
  stop)
        #clear radvd.conf
        sed -i "/# begin for interface/,/# end for interface/d" /etc/radvd.conf
        /etc/init.d/radvd stop > /dev/null
        ;;
  *)
	echo $"Usage: $0 {add|delete|stop}"
	exit 2
esac

exit $RETVAL
