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

# Open log file.
logfile=/var/log/app-startup.log

# 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
#
# PORTF:
#	Bit 7 : 1 : Inp : Unused
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : Unused
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Out : RES_CPU#
#	Bit 2 : 0 : Out : CCI_BIT#
#	Bit 1 : 1 : Inp : DEFAULT#
#	Bit 0 : 1 : Inp : Unused
#
# NOTE: We keep CCI_BIT# high here, then set it low after all apps
#       have been started.
#	Initial output value = 0xff (RES_CPU# and CCI_BIT# high).
#	Final output value   = 0xfb (RES_CPU# high, CCI_BIT# low).
#
portf 0xff 0x0c
#
# PORTH:
#	Bit 7 : 1 : Inp : Unused
#	Bit 6 : 1 : Inp : Unused
#	Bit 5 : 1 : Inp : Unused
#	Bit 4 : 1 : Inp : Unused
#
#	Bit 3 : 1 : Out : RTC_OUT
#	Bit 2 : 1 : Out : RTC_CS#
#	Bit 1 : 1 : Inp : Unused
#	Bit 0 : 1 : Inp : Unused
#
porth 0xff 0x0c

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

# Bring up the network interface.
/scripts/network_up

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

# Create bas_cfg_csv.xml file if bas_cfg.csv is present.
# The bas_cfg_csv.xml file must remain in /tmp as it's used by the data server
# to create the merge file when restarting.
tmp_files=0
if [ -e /data/config/bas_cfg.csv ]
then
	tmp_files=1
	if [ -e /data/config/group-enable ]
	then
		echo "Creating /tmp/bas_cfg_csv.xml" >> $logfile
		/app/bin/csv-to-xml /data/config/bas_cfg.csv /tmp/bas_cfg_csv.xml 1
	else
		echo "Creating /tmp/bas_cfg_csv.xml" >> $logfile
		/app/bin/csv-to-xml /data/config/bas_cfg.csv /tmp/bas_cfg_csv.xml
	fi
fi

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

# 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.
# 1-Aug-2012 RCW: Now starting both sedona and bacnet from data server; no need for
# delay after starting data server.
/app/bin/data-server &

# Now have a choice of boa or lighttpd.
#
if [ -e /app/bin/lighttpd ]
then

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

else
	# Remove the current boa marker file if it exists.
	if [ -e /var/run/boa.pid ]
	then
		rm /var/run/boa.pid
	fi

	# Start web server.
	/app/bin/boa -c /app/bin &
fi

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

# Now make CCI_BIT# active to stop blinking led.
portf 0xfb

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