#!/bin/sh

CONFFILE=/etc/cqureap.conf
IWCONFIG=/sbin/iwconfig
IFCONFIG=/sbin/ifconfig
DHCPCD="/sbin/udhcpc -i"
ROUTE=/sbin/route
BRCTL=/sbin/brctl

echo "Starting CQURE AP configuration script ..."

if [ -f $CONFFILE ]; then

	echo

	# What mode should we be running in ?
	AP_MODE=`grep -v "#" $CONFFILE | grep "AP_MODE" | cut -d'"' -f2`
	WI_DEV=`grep -v "#" $CONFFILE | grep "WI_DEV" | cut -d'"' -f2`
	ETH_DEV=`grep -v "#" $CONFFILE | grep "ETH_DEV" | cut -d'"' -f2`

	echo "Starting cqureap in $AP_MODE mode"
	echo "Wireless adapter: $WI_DEV"
	echo "Ethernet adapter: $ETH_DEV"
	
	WI_ESSID=`grep -v "#" $CONFFILE | grep "WI_ESSID" | cut -d'"' -f2`
	WI_CHANNEL=`grep -v "#" $CONFFILE | grep "WI_CHANNEL" | cut -d'"' -f2`
	WI_WEP=`grep -v "#" $CONFFILE | grep "WI_WEP" | cut -d'"' -f2`
	WI_RATE=`grep -v "#" $CONFFILE | grep "WI_RATE" | cut -d'"' -f2`
	WI_IP=`grep -v "#" $CONFFILE | grep "WI_IP" | cut -d'"' -f2`
	WI_MASK=`grep -v "#" $CONFFILE | grep "WI_MASK" | cut -d'"' -f2`

	$IWCONFIG $WI_DEV essid $WI_ESSID
	$IWCONFIG $WI_DEV enc "$WI_WEP"
	$IWCONFIG $WI_DEV channel $WI_CHANNEL
	$IWCONFIG $WI_DEV rate $WI_RATE

	#
	# dhcp or static for wireless adapter ?
	#
	if [ "$WI_IP" = "dhcp" ]; then
		echo "Requesting ipaddress from dhcp server for $WI_DEV ..."
		$IFCONFIG $WI_DEV up
		$DHCPCD $WI_DEV
	else
		echo "Setting ip $WI_IP mask $WI_MASK to $WI_DEV"
		$IFCONFIG $WI_DEV $WI_IP netmask $WI_MASK
	fi
	
	ETH_IP=`grep -v "#" $CONFFILE | grep "ETH_IP" | cut -d'"' -f2`
	ETH_MASK=`grep -v "#" $CONFFILE | grep "ETH_MASK" | cut -d'"' -f2`
	ETH_DRIVER=`grep -v "#" $CONFFILE | grep "ETH_DRIVER" | cut -d'"' -f2`

	# do we have a builtin ethernet adapter
	if [ "$ETH_DRIVER" != "" ]; then
	    echo "Installing additional ethernet driver"
	    /sbin/modprobe $ETH_DRIVER
	fi

	#
	# dhcp or static for ethernet adapter ?
	#
	if [ "$ETH_IP" = "dhcp" ]; then
		echo "Requesting ipaddress from dhcp server for $ETH_DEV ..."
		$IFCONFIG $ETH_DEV up
		$DHCPCD $ETH_DEV
	else
		echo "Setting ip $ETH_IP mask $ETH_MASK to $ETH_DEV"
		$IFCONFIG $ETH_DEV $ETH_IP netmask $ETH_MASK
	fi

	DNS1=`grep -v "#" $CONFFILE | grep "DNS1" | cut -d'"' -f2`
	DNS2=`grep -v "#" $CONFFILE | grep "DNS2" | cut -d'"' -f2`

	if [ "$DNS1" != "dhcp" ]; then
		echo "Setting up DNS information ..."
		echo "nameserver $DNS1" > /etc/resolv.conf

		if [ "$DNS2" != "dhcp" ]; then
			echo "nameserver $DNS2" >> /etc/resolv.conf
		fi
	fi

	#
	# Setting the default gw, if not set by dhcp
	#
	DEFGW=`grep -v "#" $CONFFILE | grep "DEFGW" | cut -d'"' -f2`

	if [ "$DEFGW" != "dhcp" ]; then
		echo "Setting default gateway to $DEFGW"
		$ROUTE add default gw $DEFGW
	fi


	FIREWALL_SCRIPT=`grep -v "#" $CONFFILE | grep "FIREWALL_SCRIPT" | cut -d'"' -f2`

	if [ "$AP_MODE" = "gateway" ]; then
		echo "enabling ipforwarding ..."
		echo "1" > /proc/sys/net/ipv4/ip_forward
		
		echo "Running firewall script $FIREWALL_SCRIPT ..."
		. $FIREWALL_SCRIPT
	fi

	if [ "$AP_MODE" = "bridge" ]; then
		echo "Configuring the bridge ..."
		$BRCTL addbr br0
		$BRCTL addif br0 $ETH_DEV
		$BRCTL addif br0 $WI_DEV
		$IFCONFIG $ETH_DEV 0.0.0.0
		$IFCONFIG $WI_DEV 0.0.0.0
		$IFCONFIG br0 $ETH_IP up
	fi

	DHCP_SERVER=`grep -v "#" $CONFFILE | grep "DHCP_SERVER" | cut -d'"' -f2`
	if [ "$DHCP_SERVER" = "yes" ]; then
	
		DHCPD_START_IP=`grep -v "#" $CONFFILE | grep "DHCPD_START_IP" | cut -d'"' -f2`
		DHCPD_END_IP=`grep -v "#" $CONFFILE | grep "DHCPD_END_IP" | cut -d'"' -f2`
		if [ "$AP_MODE" = "bridge" ]; then
		    DHCPD_IF="br0"
		else
		    DHCPD_IF=`grep -v "#" $CONFFILE | grep "DHCPD_IF" | cut -d'"' -f2`
		fi

		DHCPD_DNS1=`grep -v "#" $CONFFILE | grep "DHCPD_DNS1" | cut -d'"' -f2`
		DHCPD_DNS2=`grep -v "#" $CONFFILE | grep "DHCPD_DNS2" | cut -d'"' -f2`
		DHCPD_SUBNET=`grep -v "#" $CONFFILE | grep "DHCPD_SUBNET" | cut -d'"' -f2`
		DHCPD_ROUTER=`grep -v "#" $CONFFILE | grep "DHCPD_ROUTER" | cut -d'"' -f2`
		DHCPD_WINS=`grep -v "#" $CONFFILE | grep "DHCPD_WINS" | cut -d'"' -f2`
		DHCPD_DOMAIN=`grep -v "#" $CONFFILE | grep "DHCPD_DOMAIN" | cut -d'"' -f2`
		DHCPD_LEASE=`grep -v "#" $CONFFILE | grep "DHCPD_LEASE" | cut -d'"' -f2`

		cat /etc/udhcpd.orig | sed s/DHCPD_START_IP/$DHCPD_START_IP/ \
		| sed s/DHCPD_END_IP/$DHCPD_END_IP/ | sed s/DHCPD_IF/$DHCPD_IF/ \
		| sed s/DHCPD_DNS1/$DHCPD_DNS1/ | sed s/DHCPD_DNS2/$DHCPD_DNS2/ \
		| sed s/DHCPD_SUBNET/$DHCPD_SUBNET/ | sed s/DHCPD_ROUTER/$DHCPD_ROUTER/ \
		| sed s/DHCPD_WINS/$DHCPD_WINS/ | sed s/DHCPD_DOMAIN/$DHCPD_DOMAIN/ \
		| sed s/DHCPD_LEASE/$DHCPD_LEASE/ > /etc/udhcpd.conf

		echo "Starting DHCP server ..."
		/sbin/udhcpd
	fi

	TELNET_SERVER=`grep -v "#" $CONFFILE | grep "TELNET_SERVER" | cut -d'"' -f2`
	if [ "$TELNET_SERVER" = "yes" ]; then
	    echo "Starting telnet server"
	    /sbin/telnetd
	fi

	XENABLED=`grep -v "#" $CONFFILE | grep "802_1XENABLED" | cut -d'"' -f2`
	if [ "$XENABLED" = "yes" ]; then
	    echo "Configuring 802.1X support"

	    XWEPLEN=`grep -v "#" $CONFFILE | grep "802_1XWEPLEN" | cut -d'"' -f2`
	    XKEYMODE=`grep -v "#" $CONFFILE | grep "802_1XKEYMODE" | cut -d'"' -f2`

	    if [ "$XKEYMODE" = "broadcast" ]; then
		HOSTAPDPARAMS="-b $XWEPLEN"
	    fi
	    
	    if [ "$XKEYMODE" = "unicast" ]; then
		HOSTAPDPARAMS="-i $XWEPLEN"
	    fi

	    XRADIUSIP=`grep -v "#" $CONFFILE | grep "802_1XRADIUSIP" | cut -d'"' -f2`
	    XRADIUSPORT=`grep -v "#" $CONFFILE | grep "802_1XRADIUSPORT" | cut -d'"' -f2`
	    XRADIUSNASIP=`grep -v "#" $CONFFILE | grep "802_1XRADIUSNASIP" | cut -d'"' -f2`
	    
	    if [ "$XRADIUSNASIP" = "dhcp" ]; then
		XRADIUSNASIF=`grep -v "#" $CONFFILE | grep "802_1XRADIUSNASIF" | cut -d'"' -f2`
		XRADIUSNASIP=`/sbin/ifconfig $802_1XRADIUSNASIF | /bin/grep "inet addr" | cut -d':' -f2 | cut -d' ' -f1`
	    fi

	    XRADIUSSECRET=`grep -v "#" $CONFFILE | grep "802_1XRADIUSSECRET" | cut -d'"' -f2`

	    HOSTAPDPARAMS="$HOSTAPDPARAMS -o $XRADIUSNASIP"
	    HOSTAPDPARAMS="$HOSTAPDPARAMS -a $XRADIUSIP"
	    HOSTAPDPARAMS="$HOSTAPDPARAMS -p $XRADIUSPORT"
	    HOSTAPDPARAMS="$HOSTAPDPARAMS -s $XRADIUSSECRET"

	    echo "Starting hostap daemon with 802.1X support"
	    /usr/sbin/hostapd -x $HOSTAPDPARAMS -S $WI_ESSID $WI_DEV &

	else
	    echo "Starting hostap daemon ..."
	    /usr/sbin/hostapd -S $WI_ESSID $WI_DEV &
	fi



	echo

else
	echo "Could not find a valid configuration file for cqureap"
fi

echo "Finnished CQURE AP configuration ..."