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

# Open log file.
logfile=/var/log/app-startup.log
macfile=/tmp/mac.txt

# Splash.
#
echo "Executing /app/scripts/startup..." > $logfile

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

# Setup ports a,b,f,h.
#
# Use: portX init_val pin_use_bitmask
# 	pin_use_bitmask: 1=output 0=input
#
# PORTA:
#	Bit 7 : 1 : Out : FLASH_EN#
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : Unused
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Inp : Unused
#	Bit 2 : 1 : Inp : Unused
#	Bit 1 : 1 : Inp : Unused
#	Bit 0 : 1 : Inp : Unused
#
porta 0xff 0x80
#
# PORTB:
#	Bit 7 : 1 : Inp : Unused
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : Unused
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Inp : Unused
#	Bit 2 : 1 : Inp : Unused
#	Bit 1 : 1 : Inp : Unused
#	Bit 0 : 1 : Inp : Unused
#
portb 0xff 0x00
#
# PORTC:
#	Bit 7 : 1 : Inp : Unused
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : Unused
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Inp : Unused
#	Bit 2 : 1 : Inp : Unused
#	Bit 1 : 1 : Inp : Unused
#	Bit 0 : 1 : Inp : DEFAULT#
#
portc 0xff 0x00
#
# PORTF:
#	Bit 7 : 1 : Inp : Unused
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : Unused
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Inp : Unused
#	Bit 2 : 0 : Out : RES_CPU#
#	Bit 1 : 1 : Inp : Unused
#	Bit 0 : 1 : Inp : Unused
#
# NOTE: We keep RES_CPU# high
#	Initial output value = 0xff (RES_CPU# high).

portf 0xff 0x04
#
# PORTH:
#	Bit 7 : 1 : Inp : Unused
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : ACK
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Inp : TNE
#	Bit 2 : 1 : Inp : SDA#
#	Bit 1 : 1 : Inp : Unused
#	Bit 0 : 1 : Inp : Unused
#
porth 0xff 0x0

# If network ini file not present.
if [ ! -e /app/config/network.ini ]
then
	echo "network.ini not present: recovering network data from backup" >> $logfile
	cd /app/config
	tar -xzf /scripts/recover-network.tgz
	cd -
fi	

# Bring up the network interface.
/scripts/network_up
# disable promiscuous mode and multicast
macreg 0 0x73401

# Start inetd.
inetd

# Start telnetd.
telnetd

# Not running applications...
# echo "Not initializing applications; executing com-passthru" >> $logfile
# /usr/sbin/com-passthru &
# exit 0

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

# Kill any process that generates an alignment trap signal.
echo 5 > /proc/cpu/alignment


#get MAC address
echo -n "document.write(\""		> $macfile
echo -n $(getmac)			>> $macfile
echo "\");"				>> $macfile

# Start data server.
# 1-Oct-2008 RCW: Allowing 3 sec instead of 2 because 3 unconnected expansion units
# wouldn't allow modreg-server to start.
/app/bin/data-server &
sleep 12

# Start bacnet.
/app/bin/bacnet-ip &

# 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 &

# And exit.
echo "Done!" >> $logfile
exit 0
