#!/bin/sh

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

invalidConfig() {
  echo "Load config failed: $1"
  echo "Please check ng.conf or run $SCRIPT/update/update-ng-config.sh to update"
  exit 0
}

[ ! -s "$CONFIG/ng.conf" ] && invalidConfig "missing"

validConfig=`cat "$CONFIG/ng.conf" | grep HASH_CONFIG_SITE`
[ -z "$validConfig" ] && invalidConfig "invalid"

source $CONFIG/ng.conf
[ $? -ne 0 ] && invalidConfig "error when load"

source $CONFIG/ap.conf

source $SCRIPT/utils/random.sh
source $SCRIPT/utils/network.sh
source $SCRIPT/utils/wifi.sh
source $SCRIPT/utils/hotspot.sh

echo "Opkg config"
$SCRIPT/setup/setup-opkg.sh

echo "Global config"
echo "  system"

model=`$SCRIPT/model.sh`
wanMac="`getWanMac | awk -F ':' '{print $5 $6}'`"
uci set system.@system[0].hostname="$model-$wanMac"

echo "  uhttpd"

uci -q del uhttpd.main
uci -q del uhttpd.config

echo "  DHCP"


# DNS - LOCAL
uci -q del dhcp.local
uci -q del dhcp.local_lan
uci -q del dhcp.@dnsmasq[0]

[ -z "$DNS_DNS1" ] && DNS_DNS1="$DNS1"
[ -z "$DNS_DNS2" ] && DNS_DNS2="$DNS2"

uciKey=dhcp.main
uci -q del $uciKey

uci set $uciKey=dnsmasq
uci set $uciKey.domainneeded=1
uci set $uciKey.boguspriv=1
uci set $uciKey.filterwin2k=0
uci set $uciKey.localise_queries=1
uci set $uciKey.rebind_protection=1
uci set $uciKey.rebind_localhost=1
uci set $uciKey.expandhosts=1
uci set $uciKey.nonegcache=0
uci set $uciKey.authoritative=1
uci set $uciKey.readethers=1
uci set $uciKey.leasefile=/tmp/dhcp.leases
uci set $uciKey.resolvfile=/tmp/resolv.conf.auto
uci set $uciKey.localservice=1
uci add_list $uciKey.server=$DNS_DNS1
uci add_list $uciKey.server=$DNS_DNS2

# Custom DHCP
cat > /etc/dnsmasq.conf <<- EOM
dhcp-script=$SCRIPT/detect/client.sh
EOM

# DHCP - debug
uciKey=dhcp.debug
uci -q del $uciKey
uci set $uciKey=dhcp
uci set $uciKey.instance=main
uci set $uciKey.interface=debug
uci set $uciKey.force=1
uci set $uciKey.start=2
uci set $uciKey.limit=253
uci set $uciKey.leasetime=120m

# DHCP - LAN
uciKey=dhcp.lan
uci -q del $uciKey
if [ "$LAN2_BRIDGE" != 1 -a "$LAN2_DHCP" == 1 ] ; then
  uci set $uciKey=dhcp
  uci set $uciKey.instance=main
  uci set $uciKey.interface=lan
  uci set $uciKey.force=1
  [ -n "$LAN2_DHCP_START" ] && uci set $uciKey.start="$LAN2_DHCP_START" || uci set $uciKey.start=50
  [ -n "$LAN2_DHCP_LIMIT" ] && uci set $uciKey.limit="$LAN2_DHCP_LIMIT" || uci set $uciKey.limit=100
  [ -n "$LAN2_DHCP_LEASETIME" -a -n "$LAN2_DHCP_LEASETIME_UNIT" ] && uci set $uciKey.leasetime="$LAN2_DHCP_LEASETIME""$LAN2_DHCP_LEASETIME_UNIT"

  [ -z "$LAN2_IP" ] && LAN2_IP="192.168.102.1"
  dnsIps=""
  [ -z "$LAN2_DHCP_DNS1" ] || dnsIps="${dnsIps},$LAN2_DHCP_DNS1"
  [ -z "$LAN2_DHCP_DNS2" ] || dnsIps="${dnsIps},$LAN2_DHCP_DNS2"
  [ -z "$dnsIps" ] && dnsIps=",${LAN2_IP}"

  uci add_list $uciKey.dhcp_option="3,${LAN2_IP}"
  uci add_list $uciKey.dhcp_option="6${dnsIps}"
fi

# DHCP - WAN
uciKey=dhcp.wan
uci -q del $uciKey
if [ "$WAN_PROTO" == static -a "$WAN_DHCP" == 1 ] ; then
  uci set $uciKey=dhcp
  uci set $uciKey.instance=main
  uci set $uciKey.interface=wan
  uci set $uciKey.force=1
  [ -n "$WAN_DHCP_START" ] && uci set $uciKey.start="$WAN_DHCP_START" || uci set $uciKey.start=50
  [ -n "$WAN_DHCP_LIMIT" ] && uci set $uciKey.limit="$WAN_DHCP_LIMIT" || uci set $uciKey.limit=100
  [ -n "$WAN_DHCP_LEASETIME" -a -n "$WAN_DHCP_LEASETIME_UNIT" ] && uci set $uciKey.leasetime="$WAN_DHCP_LEASETIME""$WAN_DHCP_LEASETIME_UNIT"

  dnsIps=""
  [ -z "$WAN_DHCP_DNS1" ] || dnsIps="${dnsIps},$WAN_DHCP_DNS1"
  [ -z "$WAN_DHCP_DNS2" ] || dnsIps="${dnsIps},$WAN_DHCP_DNS2"
  [ -z "$dnsIps" ] && dnsIps=",${WAN_IP}"
  [ -z "$WAN_DHCP_GATEWAY" ] && gateway="$WAN_GATEWAY" || gateway="$WAN_DHCP_GATEWAY"

  [ -z "$gateway" ] || uci add_list $uciKey.dhcp_option="3,${gateway}"
  uci add_list $uciKey.dhcp_option="6${dnsIps}"
fi


# UHTTPD
uci -q del uhttpd.config

uciKey=uhttpd.hotspot
uci -q del $uciKey
uci set $uciKey=uhttpd
uci set $uciKey.home="$ROOT/web"
uci set $uciKey.rfc1918_filter=0
uci set $uciKey.max_requests=50
uci set $uciKey.max_connections=500
uci set $uciKey.interpreter=.lua=/usr/bin/lua
uci set $uciKey.lua_prefix=/wifi
uci set $uciKey.lua_handler=$CGI/wifi-handler.lua
uci set $uciKey.script_timeout=10
uci set $uciKey.network_timeout=30
uci set $uciKey.http_keepalive=1
uci set $uciKey.tcp_keepalive=1
uci set $uciKey.no_dirlists=1
uci set $uciKey.no_symlinks=1
uci set $uciKey.index_page=index.lua
uci set $uciKey.error_page=/index.lua

uciKey=uhttpd.autologin
uci -q del $uciKey
uci set $uciKey=uhttpd
uci set $uciKey.rfc1918_filter=0
uci set $uciKey.max_requests=10
uci set $uciKey.max_connections=100
uci set $uciKey.interpreter=.lua=/usr/bin/lua
uci set $uciKey.lua_prefix=/
uci set $uciKey.lua_handler=$CGI/autologin-handler.lua
uci set $uciKey.script_timeout=60
uci set $uciKey.network_timeout=30
uci set $uciKey.http_keepalive=1
uci set $uciKey.tcp_keepalive=1
uci set $uciKey.no_dirlists=1
uci set $uciKey.no_symlinks=1

# PRIVOXY - keep CNA
echo "  Privoxy"
rm -rf $CONFIG/privoxy*

# NETWORK
echo "  Network"
sh "$SCRIPT/setup/setup-network.sh"

# WIRELESS
for radio in `seq 1 $radios` ; do
  "$SCRIPT/setup/setup-wireless.sh" "$radio"
done

# SSID Specific
ssids=`seq 1 "$NO_SSID"`
radios=`getRadioCount`
for ssid in $ssids ; do
  "$SCRIPT/setup/setup-ssid.sh" "$ssid"
  for radio in `seq 1 $radios` ; do
    "$SCRIPT/setup/setup-wireless-ssid.sh" "$ssid" "$radio"
  done
done

# 3G
[ -z "$MOBILE_DEV" ] || "$SCRIPT/setup/setup-mobile.sh"

rm -rf /tmp/blocked.maclist
"$SCRIPT/setup/setup-blocked-mac.sh" >/dev/null

# CUSTOM
"$SCRIPT/setup/setup-custom.sh"

# SCHEDULE
echo -n "Schedule "
"$SCRIPT/setup/setup-schedule.sh"

# Done
uci commit

# Init crontap
echo "Init crontab rules"
mkdir -p /etc/crontabs
echo -n > /etc/crontabs/root

# Drop cache every hour
echo "4 * * * * sync ; echo 3 > /proc/sys/vm/drop_caches" >> /etc/crontabs/root

# Update time every 10 mins
echo "0,10,20,30,40,50 * * * * /etc/init.d/ng_ap2 restart time" >> /etc/crontabs/root

# Check new code every 20 minutes
echo "1,21,41 * * * * $SCRIPT/update/update-code.sh" >> /etc/crontabs/root

# Check new config every 10 minutes
m=`ramdomNumber 2 9`
echo "${m},1${m},2${m},3${m},4${m},5${m} * * * * $SCRIPT/update/update-ng-config.sh" >> /etc/crontabs/root

# monitor job
echo "* * * * * $SCRIPT/monitor.sh" >> /etc/crontabs/root

# Setup reboot cron
[ -z "$CRON_REBOOT" ] || echo "$CRON_REBOOT sleep 70 && touch /etc/banner && reboot" >> /etc/crontabs/root

chmod 775 /etc/crontabs/root
/etc/init.d/cron enable
/etc/init.d/cron restart
