#!/bin/sh

. /etc/profile

gen_hostapd_conf() {
	ssid="${1}"
	pass="${2}"
	echo "ctrl_interface=/var/run/hostapd"
	echo "ctrl_interface_group=0"
	echo "ssid=${ssid}"
	echo "hw_mode=g"
	echo "channel=1"
	echo "beacon_int=100"
	echo "dtim_period=2"
	echo "max_num_sta=255"
	echo "rts_threshold=-1"
	echo "fragm_threshold=-1"
	echo "macaddr_acl=0"
	echo "auth_algs=3"
	echo "wpa=2"
	echo "wpa_passphrase=${pass}"
	echo "ieee80211n=1"
}



gen_udhcpd_conf() {
        interface=${1}
        ipv4_prefix=${2}
        echo "start ${ipv4_prefix}.100"
        echo "end ${ipv4_prefix}.200"
        echo "interface ${interface}"
        echo "pidfile /var/run/udhcpd.${interface}.pid"
        echo "lease_file /var/lib/misc/udhcpd.${interface}.leases"
        echo "option subnet 255.255.255.0"
        echo "option lease 864000"
}

start() {
	if [ -e /boot/wifi.sta ]
	then
		echo "wifi mode: sta"
		rm -f /etc/udhcpd.wlan0.conf
		rm -f /etc/hostapd.conf
		if [ -e /boot/wpa_supplicant.conf ]
		then
			cp /boot/wpa_supplicant.conf /etc/wpa_supplicant.conf
		else
			ssid=""
			pass=""
			if [ -e /boot/wifi.ssid ]
			then
				echo -n "ssid: "
				cat /boot/wifi.ssid
				ssid=`cat /boot/wifi.ssid`
			fi
			if [ -e /boot/wifi.pass ]
			then
				echo -n "wifi.pass: "
				cat /boot/wifi.pass
				pass=`cat /boot/wifi.pass`
			fi
			if [ ! -z "${ssid}${pass}" ]
			then
				echo "allow-hotplug wlan0" > /etc/network/interfaces.d/wlan0
				echo "iface wlan0 inet dhcp" >> /etc/network/interfaces.d/wlan0
				echo -e "\twpa-essid $ssid" >> /etc/network/interfaces.d/wlan0
				echo -e "\twpa-psk $pass" >> /etc/network/interfaces.d/wlan0
				echo -e "\twpa-scan-ssid 1" >> /etc/network/interfaces.d/wlan0
			fi
		fi
	elif [ -e /boot/wifi.ap ]
	then
		echo "wifi mode: ap"
		if [ -e /boot/hostapd.conf ]
		then
			cp /boot/hostapd.conf /etc/hostapd.conf
		else
			id2=$(printf "%d" 0x$(sha512sum /device_key_legacy | head -c 2))
			id3=$(printf "%d" 0x$(sha512sum /device_key_legacy | head -c 4 | tail -c 2))
			if [ "$id2" = "$id3" ]
			then
				id2=$((id2 + 1))
			fi
			if [ "$id2" -ge 255 ]
			then
				id2=253
			fi
			if [ "$id3" -ge 255 ]
			then
				id3=254
			fi
			ssid="licheervnano-${id2}${id3}"
			pass="licheervnano"
			if [ -e /boot/wifi.ssid ]
			then
				echo -n "ssid: "
				cat /boot/wifi.ssid
				ssid=`cat /boot/wifi.ssid`
			fi
			if [ -e /boot/wifi.pass ]
			then
				echo -n "wifi.pass: "
				cat /boot/wifi.pass
				pass=`cat /boot/wifi.pass`
			fi
			gen_hostapd_conf "$ssid" "$pass" > /etc/hostapd.conf
		fi
		if [ -e /boot/wifi.ipv4_prefix ]
		then
			ipv4_prefix=`cat /boot/wifi.ipv4_prefix`
		else
			ipv4_prefix=10.$id3.$id2
		fi
		if [ ! -e /etc/udhcpd.wlan0.conf ]
		then
			gen_udhcpd_conf wlan0 "${ipv4_prefix}"  > /etc/udhcpd.wlan0.conf
		fi
		ifconfig wlan0 up
		ip route del default || true
		# routes=$(ip route show | grep 'dev wlan0' | awk '{print $1}')
		# for route in $routes; do
		# 	ip route del $route dev wlan0
		# 	echo "Deleted route $route dev wlan0"
		# done
		ip add flush dev wlan0
		ip addr add $ipv4_prefix.1/24 dev wlan0
		#hostapd -B -i wlan0 /etc/hostapd.conf
		#udhcpd -S /etc/udhcpd.wlan0.conf
		echo "allow-hotplug wlan0" > /etc/network/interfaces.d/wlan0
		echo "iface wlan0 inet static" >> /etc/network/interfaces.d/wlan0
		echo -e "\taddress $ipv4_prefix.1" >> /etc/network/interfaces.d/wlan0
		echo -e "\tnetmask 255.255.255.0" >> /etc/network/interfaces.d/wlan0
		systemctl enable wifi-hostapd-wlan0.service
		systemctl enable wifi-builtin-wlan0.service
	elif [ -e /boot/wifi.mon ]
	then
		echo "wifi mode: mon"
		airmon-ng start wlan0
	else
		echo "allow-hotplug wlan0" > /etc/network/interfaces.d/wlan0
		echo "iface wlan0 inet manual" >> /etc/network/interfaces.d/wlan0
		rm -f /etc/udhcpd.wlan0.conf
		rm -f /etc/hostapd.conf
	fi
}

stop() {
	ps -ef|grep hostapd|grep -v grep|awk '{print $1}'|xargs kill -2 || true
	ps -ef|grep "udhcpd -S /etc/udhcpd.wlan0.conf" |grep -v grep|awk '{print $1}'|xargs kill -2 || true
	killall wpa_supplicant || true
	if [ -e /run/udhcpc.wlan0.pid ]
	then
		kill `cat /run/udhcpc.wlan0.pid` || true
		rm -f /run/udhcpc.wlan0.pid
	fi
	if [ -e /var/run/udhcpd.wlan0.pid ]
	then
		kill `cat /var/run/udhcpd.wlan0.pid`  || true
		rm -f /var/run/udhcpd.wlan0.pid
	fi
	airmon-ng stop wlan0mon || true
}

restart() {
	stop
	start
}

if [ "${1}" = "start" ]
then
	start
elif [ "${1}" = "stop" ]
then
	stop
elif [ "${1}" = "restart" ]
then
	restart
fi
