#!/bin/sh

EXEWORK="install -m 755"
OTWORK="install -m 644"
RMWORK=rm

REBOOT="no"

cd /tmp

echo "installing files"

for i in files/*.tgz ; do
	B=`basename $i`
	echo "Installing $B"
	
	if [ "$B" = "installer.tgz" ]; then
		echo "installing installer"
		tar -xzf $i -C /
		( cd /_install/installer; ./install.sh )
		continue
	fi
	
	if tar -tzf $i | grep "_install" >/dev/null ; then
		echo "Using installer"
		if [ -x /sbin/pkginstall ]; then
			if pkginstall $i ; then
				REBOOT="no"
			else
				REBOOT="yes"
			fi
			continue
		else
			echo "$B: please install installer.tar.gz first" 1>&2
		fi
	fi
	
	echo "$B: Invalid package" 1>&2
done

rm -fr /tmp/files
rm -f /tmp/files.tar.gz

echo "Finished."
if [ "$REBOOT" = "yes" ]; then
	echo "The system will shortly reboot itself."
	echo "Do NOT power off the unit until you see the new status screen."
	echo "Refresh by pressing the HOME button until you see this screen."
	echo "This will take about 90 seconds."
	( usleep 10000000 ; /sbin/reboot) &
fi
