#!/bin/sh

cp /mnt/wifi/udhcpd.conf /etc/
cp /mnt/wifi/hostapd.conf /etc/
cp /mnt/wifi/hostapd /sbin/

mkdir -p /mnt/udhcpd
rm -rf /mnt/udhcpd/udhcpd.leases
touch /mnt/udhcpd/udhcpd.leases

localip=$(cat /mnt/prog_800s/palmip.txt | awk -F'[.]' '{ print $1"."$2"."$3}')
wlanip=$(cat /mnt/prog_800s/palmip.txt | awk -F'[.]' '{ print $4}')
sed -i 's/192.168.6/'$localip'/g' /etc/udhcpd.conf
sync

ifconfig br0 "$localip"."$wlanip"

ip route > /mnt/wifi/route.txt
gateway=$(cut -d ' ' -f 3 /mnt/wifi/route.txt | grep 1 )

eth=$(cat /sys/class/net/eth0/operstate)
if [ "$eth". = "up". ];
then
	ifconfig eth0 0.0.0.0
	route add default gw "$gateway"
else
	route del default
	rm /etc/resolv.conf
	sed -i 's/myip/'$localip.$wlanip'/g' /etc/udhcpd.conf
	sed -i 's/opt[ ]*dns/#opt      dns/g' /etc/udhcpd.conf
	udhcpd /etc/udhcpd.conf
fi

ifconfig wlan0 0.0.0.0
ifconfig eth0 0.0.0.0
brctl addif br0 wlan0
brctl addif br0 eth0
hostapd -B /etc/hostapd.conf
