#!/bin/sh

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

source $CONFIG/ap.conf
source $CONFIG/ng.conf

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

ssid="$1"
[ -z "$ssid" -o "$ssid" -gt "$NO_SSID" ] && echo "Invalid SSID $ssid" && exit

echo "SSID $ssid config"

eval ssidIp="\$SSID${ssid}_IP"
eval ssidDomain="\$SSID${ssid}_HOSTNAME"

echo "  uhttpd"
###########################################################
# UHTTPD

uciKey="uhttpd.ssid${ssid}"
uci -q del $uciKey

if [ `isHotspotEnabledOnSsid $ssid` ] ; then
  uciKey=uhttpd.hotspot
  listenPort=$((8079 + ssid))
  uci add_list $uciKey.listen_http="$listenPort"

  uciKey=uhttpd.autologin
  listenPort=$((8069 + ssid))
  uci add_list $uciKey.listen_http="$listenPort"
fi

echo "  DHCP"
###########################################################
# DHCP

uciKey="dhcp.local_wifi$ssid"
uci -q del $uciKey

###########################################################
# DHCP - SSID

uciKey="dhcp.wifi$ssid"
uci -q del $uciKey

eval dhcpEnabled="\$SSID${ssid}_DHCP"
[ -z "$DNS_DNS1" ] && DNS_DNS1="$DNS1"
[ -z "$DNS_DNS2" ] && DNS_DNS2="$DNS2"

eval dhcpDns1="\$SSID${ssid}_DHCP_DNS1"
eval dhcpDns2="\$SSID${ssid}_DHCP_DNS2"
if [ `isSsidEnabled $ssid` ] && [ ! `isSsidInBridgeMode $ssid` ] && [ "$dhcpEnabled" != 0  ] ; then
  uci set $uciKey=dhcp
  uci set $uciKey.instance=main
  uci set $uciKey.interface=wifi"$ssid"
  uci set $uciKey.force=1

  dnsIps=""
  [ -z "$dhcpDns1" ] || dnsIps="${dnsIps},${dhcpDns1}"
  [ -z "$dhcpDns2" ] || dnsIps="${dnsIps},${dhcpDns2}"
  [ -z "$dnsIps" ] && dnsIps=",${ssidIp}"
  uci add_list $uciKey.dhcp_option="3,${ssidIp}"
  uci add_list $uciKey.dhcp_option="6${dnsIps}"

  eval dhcpLease="\$SSID${ssid}_DHCP_LEASETIME"
  eval dhcpLeaseUnit="\$SSID${ssid}_DHCP_LEASETIME_UNIT"
  if [ -n "$dhcpLease" -a -n "$dhcpLeaseUnit" ] ; then
    uci set $uciKey.leasetime="${dhcpLease}${dhcpLeaseUnit}"
  fi

  if [ `isHotspotEnabledOnSsid $ssid` ] ; then
    dhcpStart=2
    dhcpLimit=250
  else
    eval dhcpStart="\$SSID${ssid}_DHCP_START"
    eval dhcpLimit="\$SSID${ssid}_DHCP_LIMIT"
    [ -z "$dhcpStart" ] && dhcpStart=2
    [ -z "$dhcpLimit" ] && dhcpLimit=250
  fi
  uci set $uciKey.start="$dhcpStart"
  uci set $uciKey.limit="$dhcpLimit"
fi

uciKey="dhcp.wifi${ssid}_dns"
uci -q del $uciKey

if [ `isSsidEnabled $ssid` ] && [ `isHotspotEnabledOnSsid $ssid` ]; then
  uci set $uciKey=dnsmasq
  uci set $uciKey.port=$((5300 + ssid))
  uci set $uciKey.add_local_domain=1
  uci set $uciKey.interface="lo"
  uci add_list $uciKey.server=$DNS_DNS1
  uci add_list $uciKey.server=$DNS_DNS2

  # uci add_list $uciKey.ipset="/clients1.google.com/keepcnadst"
  # uci add_list $uciKey.ipset="/clients3.google.com/keepcnadst"
  # uci add_list $uciKey.ipset="/play.googleapis.com/keepcnadst"
  # uci add_list $uciKey.ipset="/connectivitycheck.gstatic.com/keepcnadst"
  # uci add_list $uciKey.ipset="/connectivitycheck.android.com/keepcnadst"

  eval wallgarden="\$SSID${ssid}_WALLGARDEN"
  ipsetName="wallgarden${ssid}"
  [ ! -z "$wallgarden" ] && wallgarden=`echo $wallgarden | tr ';' '\n'`
  for site in $wallgarden ; do
    [ -z "$site" ] && continue
    if ! [ "`addressIsIp $site`" ] ; then
      uci add_list $uciKey.ipset="/.${site}/${site}/${ipsetName}"
    fi
  done

  uciKey="dhcp.wifi${ssid}_domain"
  uci -q del $uciKey

  [ `isSsidInBridgeMode $ssid` ] && domainIp=`getWanIp` || domainIp="$ssidIp"
  if [ ! -z "$ssidDomain" ] ; then
    uci set $uciKey=domain
    uci set $uciKey.instance=wifi${ssid}_dns
    uci set $uciKey.name="$ssidDomain"
    uci set $uciKey.ip="$domainIp"
  fi
fi
