#!/bin/ash

[ -z "$ROOT" ] && ROOT=/www
SCRIPT=$ROOT/script

echo "  Install dependencies"
$SCRIPT/setup/setup-opkg.sh
$SCRIPT/utils/misc.sh

[ "`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`" -lt 60000 ] || highMem=true

[ "$highMem" ] && read -r -d '' highMemPackages << EOM
mosquitto-client-ssl
netcat
openssl-util
tcpdump
privoxy
EOM

read -r -d '' packages << EOM
$highMemPackages
ca-certificates
wget
luasec
libopenssl
hostapd-utils
EOM

if opkg update ; then
	for p in $packages ; do
		[ -z "`opkg list-installed $p`" ] && opkg install $p
	done

	# Upgrade wpad-mini to wpad
	wpadInstalled=`opkg list-installed wpad`
	if [ "$highMem" ] && [ ! "$wpadInstalled" ] ; then
		cd /tmp
		wpadFile=`opkg info wpad | grep Filename | awk '{print $2}'`
		opkg download wpad
		if [ $? == 0 -a -f "$wpadFile" ] ; then
			opkg remove wpad-mini
			opkg install $wpadFile
		fi
	fi
fi

[ "$highMem" ] || rm -rf $ROOT/bish-bosh/*