#!/bin/sh

# Common
. /etc_ro_fs/init.d/common

# Lecture de la configuration du routeur
. /etc_ro_fs/autoconf.conf

readconf /etc/bluedsl.conf ENABLE_DHCP_SERVER DHCP_START DHCP_STOP

readconf /etc/bluedsl.conf ENABLE_UMA_DHCP_SERVER

case $1 in
    start)
	if [ "$ENABLE_DHCP_SERVER" = "1" -a ! -z "$DHCP_START" -a ! -z "$DHCP_STOP" ] ; then
	    echo "dnsmasq configured as dhcp server"
	    REAL_DHCP_STOP=$DHCP_STOP
	    OPTION=""
	    
	    if [ "$ENABLE_UMA_DHCP_SERVER" = "1" ] ; then 
		last_digit=$( echo $DHCP_STOP |  sed "s,\([0-9]*\.[0-9]*\.[0-9]*\.\)\([0-9]*\),\2," )
		three_first_digits=$( echo $DHCP_STOP |  sed "s,\([0-9]*\.[0-9]*\.[0-9]*\.\)\([0-9]*\),\1,")
		REAL_DHCP_STOP=${three_first_digits}$(( $last_digit - 3 ))
		UMA_DHCP_START=${three_first_digits}$(( $last_digit - 2 ))
		OPTION="--dhcp-vendorclass=uma,FSVDSL.UMA --dhcp-range net:uma,$UMA_DHCP_START,$DHCP_STOP,300s"
	    fi
	    
	    # On lance avec le debug, pour que les logs du serveur dhcp puissent tre crits correctement
	    OPTION="--dhcp-range=$DHCP_START,$REAL_DHCP_STOP,24h --dhcp-leasefile=/etc/dhcp.leases $OPTION"
        else
	    echo "dnsmasq not configured as dhcp server"
	fi
	/bin/echo "dnsmasq start ( DNS / DHCP server ) ..."
	dnsmasq $OPTION 
	;;
    stop)
	[ -f /var/run/dnsmasq.pid ] && kill $(cat /var/run/dnsmasq.pid) 
	rm /var/run/dnsmasq.pid
	;;
    *)
	echo "Usage : $0 [start|stop]"
esac
