#!/bin/sh

[ -z "$ROOT" ] && ROOT=/www
CONFIG=$ROOT/config
SCRIPT=$ROOT/script

touch /tmp/applying

md5() {
	md5sum $1 | awk '{ print $1 }'
}

echo "Reload config"

hashWirelessOld=`md5 /etc/config/wireless`
hashNetworkOld=`md5 /etc/config/network`
hashDhcpOld=`md5 /etc/config/dhcp`

backupDir="/tmp/backup"
[ -s "$backupDir/wireless" ] && hashWirelessOld=`md5 "$backupDir/wireless"`
[ -s "$backupDir/network" ] && hashNetworkOld=`md5 "$backupDir/network"`
[ -s "$backupDir/dhcp" ] && hashDhcpOld=`md5 "$backupDir/dhcp"`

$SCRIPT/setup/setup.sh no

hashWirelessNew=`md5 /etc/config/wireless`
hashNetworkNew=`md5 /etc/config/network`
hashDhcpNew=`md5 /etc/config/dhcp`

echo
if [ "$hashNetworkOld" != "$hashNetworkNew" ] ; then
	echo "  Reload network"
	/etc/init.d/network restart
	hashDhcpNew='reload'
	sleep 10
fi

if [ "$hashWirelessOld" != "$hashWirelessNew" ] ; then
	echo "  Reload wireless"
	wifi
	sleep 10
fi

if [ "$hashDhcpOld" != "$hashDhcpNew" ] ; then
	echo "  Reload dhcp"
	/etc/init.d/dnsmasq restart
fi

echo "  Reload uhttpd"
/etc/init.d/uhttpd restart

echo "  Reload ng_ap"
[ -z "$skip" ] && /etc/init.d/ng_ap2 restart

echo
echo "Done"

sleep 30
rm -f /tmp/applying

$SCRIPT/monitor-config.sh
