#!/bin/sh

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

localip=$(cat /mnt/prog_800s/palmip.txt | awk -F'[.]' '{ print $1"."$2"."$3}')
wlanip=$(cat /mnt/prog_800s/palmip.txt | awk -F'[.]' '{ print $4}')

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
	ifconfig br0 "$localip"."$wlanip"
	route add default gw "$gateway"
	sed -i 's/myip/'$localip.1'/g' /etc/udhcpd.conf
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
fi
sed -i 's/192.168.6/'$localip'/g' /etc/udhcpd.conf

mkdir -p /mnt/udhcpd
rm -rf /mnt/udhcpd/udhcpd.leases
touch /mnt/udhcpd/udhcpd.leases
sync
udhcpd /etc/udhcpd.conf

ifconfig br0 "$localip"."$wlanip"
brctl addif br0 wlan0
brctl addif br0 eth0
ifconfig wlan0 0.0.0.0
ifconfig eth0 0.0.0.0
hostapd -B /etc/hostapd.conf
