#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR

# Read current settings
. basicFunctions
. /boot/repetier-image-setup.txt

AP_ACTIVE="true"
writeServerConf

# nmcli device disconnect $WLAN_HOTSPOT
# nmcli dev set $WLAN_HOTSPOT managed no

systemctl stop dnsmasq
systemctl stop hostapd
systemctl stop wpa_supplicant@${WLAN}.service
systemctl disable wpa_supplicant@${WLAN}.service


cat > /etc/dnsmasq.d/hotspot.conf <<EOF
interface=wlan0
dhcp-range=10.10.10.2,10.10.10.50,255.255.255.0,24h
EOF

cat > /etc/hostapd/hostapd.conf <<EOF
interface=$WLAN_HOTSPOT
country_code=$COUNTRY
ieee80211d=1
hw_mode=g
channel=8
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=$PASSWORD
ssid=$SSID
ieee80211n=1
wmm_enabled=1
ignore_broadcast_ssid=0
macaddr_acl=0
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
EOF

updateDHCPCD

systemctl restart dhcpcd
systemctl unmask hostapd
systemctl enable hostapd
systemctl enable dnsmasq

service hostapd start
service dnsmasq start
if [[ -f /usr/sbin/iptables ]]; then
  iptables -t nat -A  POSTROUTING -o eth0 -j MASQUERADE
  sh -c "iptables-save > /etc/iptables.ipv4.nat"
fi
