#!/bin/sh
. /tmp/network-config

TMP_C=/tmp/chilli.conf
ETC_C=/etc/chilli.conf
PID_F=/var/run/chilli.pid
PID_LOOP_F=/var/run/chilli_loop.pid
LOG_LOOP_F=/var/log/chilli_loop.log
LANIP="$(ifconfig "$lan_ifname" | grep inet | awk -F'[: ]+' '{print $4}')"
MAC=$(ifconfig wifi0 | head -n1 | awk '{print $5}'|sed s/:/-/g)
MAC=${MAC:-fon}
LOOP=true

WHITELIST=/etc/config/whitelist.dnsmasq

DEVICE=$(cat /etc/fon_device)
VERSION=$(cat /etc/fon_version)
REVISION=$(cat /etc/fon_revision)

RADIUSSERVER1=radconfig01.fon.com
RADIUSSERVER2=radconfig02.fon.com
RADIUSSECRET=garrafon
RADIUSADMUSR=FON01-${DEVICE}-${VERSION}.${REVISION}
RADIUSADMPWD=chillispot

quit()
{
	LOOP="false"
	circular_log $LOG_LOOP_F "Signal caught. Exiting..."
}

circular_log() {
	echo "chillispot $(date) $2" >> $1
	tail -24 $1 > $1.tmp
	mv $1.tmp $1
}

is_alive() {
	if [ ! -f $PID_F ]; then
		echo "dead"
		return 0
	fi

	TEST_PID=$(cat $PID_F)

	if [ ! -d /proc/$TEST_PID ]; then
		rm $PID_F
		echo "dead"
		return 0
	fi

	CANDIDATE=$(cat /proc/$TEST_PID/status | grep Name: | awk '{ print $2 }')

	if [ "$CANDIDATE" = "chilli" ]; then
		echo "alive"
		return 0
	else
		rm $PID_F
		echo "dead"
		return 0
	fi
}

parse_whitelist() {
        if [ -s $TMP_C ]; then
                awk '/newdomain/ { print $2 }' $TMP_C |
                awk -F. '/[a-zA-Z0-9\-\_]/ { print }' |
                sed s/,/\\n/g > $WHITELIST
		# Remove IPs that may have skipped the validation
		cat $WHITELIST | grep -v [0-9]$ > $WHITELIST

                sed /^newdomain/d -i $TMP_C
        fi
}

radconfig() {
	/usr/sbin/chilli_radconfig \
		-c /dev/null \
		--radiusserver1="$RADIUSSERVER1" \
		--radiusserver2="$RADIUSSERVER2" \
		--radiussecret="$RADIUSSECRET" \
		--adminuser="$RADIUSADMUSR" \
		--adminpasswd="$RADIUSADMPWD" \
		--radiusnasid="$MAC" \
		--dhcpif $wifi_ifname \
		--wwwbin=/bin/true \
		--ipup=/bin/true \
		--ipdown=/bin/true \
		> $TMP_C
	
	parse_whitelist

	[ -n "$(cat $TMP_C)" ] && {
		MD5SUM_TMP=$(md5sum $TMP_C | awk '{ print $1 }')
		MD5SUM_ETC=$(md5sum $ETC_C | awk '{ print $1 }')
		if [ ! "$MD5SUM_TMP" = "$MD5SUM_ETC" ]; then
				rm $ETC_C
				mv $TMP_C $ETC_C
				circular_log $LOG_LOOP_F "RELOAD"
				reload
		else
				circular_log $LOG_LOOP_F "NO RELOAD"
		fi
		return 0
	}

	circular_log $LOG_LOOP_F "NO RELOAD"
}

do_start () {
	/sbin/insmod tun >/dev/null 2>&1
	ifconfig $wifi_ifname 0.0.0.0 # deconfigure the wifi interface

	/usr/sbin/chilli \
		--dns1="192.168.182.1" \
		--dns2="192.168.182.1" \
		--radiusnasid="$MAC" \
		--dhcpif $wifi_ifname \
		--papalwaysok \
		--pidfile=$PID_F \
		--localusers=/etc/config/localusers \
		--wwwbin=/bin/true \
		--ipup=/bin/true \
		--ipdown=/bin/true \
		--conup=/bin/true \
		--condown=/bin/true
	
	[ $? == 0 ] && return 0
	return 1
}


case $1 in
	restart)
		killall watch_chilli > /dev/null 2>&1
		killall chilli > /dev/null 2>&1
		rm -f /var/run/chilli.pid

		do_start
		;;
	start)
		ALIVE=$(is_alive)
		[ alive = "$(is_alive)" ] && return 0
		radconfig
		/sbin/ifup hotspot
		/etc/init.d/S60redirect stop
		do_start
		[ $? == 0 ] && /usr/sbin/watch_chilli &
		;;
	stop)
		/sbin/ifdown hotspot
		
		killall watch_chilli

		[ alive = "$(is_alive)" ] || {
			echo ERROR: chillispot is not running
			[ -f $PID_LOOP_F ] && kill $(cat $PID_LOOP_F) > /dev/null 2>&1
			rm -f $PID_LOOP_F > /dev/null 2>&1
			exit 0
		}
		[ -f $PID_F ] && kill $(cat $PID_F) >/dev/null 2>&1
		rm -f $PID_F
		[ -f $PID_LOOP_F ] && kill $(cat $PID_LOOP_F) > /dev/null 2>&1
		rm -f $PID_LOOP_F
		;;
	alive)
		ALIVE=$(is_alive)
		echo "chillispot is $ALIVE"
		if [ $ALIVE = "alive" ]; then
			exit 1
		fi
		exit 0
		;;
	reload)
		radconfig
		killall -HUP chilli
		;;
	*)
		echo "usage: $0 (start|stop|restart|reload|alive)"
		exit 1
esac

