#!/bin/sh
#
# This will prepare and begin operation of the "application" program(s).
#

# Splash.
#
echo "Executing /app/scripts/startup..."

# Not running anything...
# echo "Not initializing network or applications..."
# exit 0

# Source the port pin setup. This will be determined by hardware type.
# Any initialization specific to the hardware should be done there.
#
. /app/scripts/gpio-setup


# Get portf status.
portf_val=$(portf | cut -d' ' -f4)

# Check default bit
portf_val=$(($portf_val & 0x02))

# If default pushbutton is low.
if [ "$portf_val" = "0" ]
then
	echo "Default pushbutton pressed: setting default ip and passwords."
	#
	cd /app/config
	tar -xzf /scripts/recover-network.tgz
	cd -
	#
	cd /etc
	/scripts/read-write.sh
	tar -xzf /scripts/recover-etc.tgz
	sleep 3
	/scripts/read-only.sh
	# Store new config so it's good for next boot.
	/scripts/config_store
fi

# Mount sysfs
mount -t sysfs sysfs /sys

# Mount usbfs
mount -t usbfs none /proc/bus/usb

# Setup MAC address first as config-loader application needs it.
EP93XX_MAC=$(getmac)
/sbin/ifconfig eth0 down
/sbin/ifconfig eth0 hw ether $EP93XX_MAC
/sbin/ifconfig eth0 up

# Create config files from settings file
/app/bin/config-loader

# Source definitions after config-loader is done updating them
. /app/config/network_eth0.ini
. /app/config/network_eth1.ini

# Bring up the network interface.
/scripts/network_up

# Disable multicasts and promiscuous mode only is WiFi is disabled
if [ "$EIPR_WIFI" != "YES" ]; then
macreg 0 0x73401
fi

# Start inetd.
inetd

# Start telnetd.
telnetd

# Not running applications...
# echo "NOT running application"
# exit 0

# Start remote command line interface; active for 60 seconds.
#
/app/bin/rcli 60 &

# Common iptables settings
sleep 5
/scripts/iptables_Init2 > /dev/null 2>&1

# Start web server.
/app/bin/lighttpd -m /usr/lib/lighttpd -f /etc/lighttpd/lighttpd.conf &

# Monitor default push button
/app/scripts/resetIP.sh &

# Monitor Settings Change
/app/scripts/settingsChange &

if [ "$EIPR_WIFI" = "YES" ]
then
	/app/bin/hostapd /app/config/hostapd.conf &
	if [ "$EIPR_FW" = "YES" ]; then
		/scripts/wifi2lan.sh &
	fi
fi


# And exit.
exit 0
