#!/bin/bash

PKG=timecapsule-uR4.7-i486-6lk

cd /boot/packages
if [ ! -f ${PKG}.tgz ]
then
	wget http://blueskyhorizon.com/unraid/${PKG}.tgz
	if [ $? -ne 0 ]
	then
		echo "Cannot download ${PKG}.tgz" >&2
		exit 1
	fi
fi

if [ ! -f libcap-2.14-i486-1.tgz ]
then
	wget http://slackware.mirrors.easynews.com/linux/slackware/slackware-12.2/slackware/l/libcap-2.14-i486-1.tgz
	if [ $? -ne 0 ]
	then
		echo "Cannot download libcap-2.14-i486-1.tgz" >&2
		exit 1
	fi
fi

grep 'installpkg /boot/packages/libcap-2.14-i486-1.tgz' /boot/config/go > /dev/null
if [ $? -eq 0 ]
then
	cat /boot/config/go | sed "s/timecapsule-uR4.7-i486-[0-9]*lk/${PKG}/" > /tmp/go
else
	cat /boot/config/go | sed "s/timecapsule-uR4.7-i486-[0-9]*lk/libcap-2.14-i486-1.tgz\ninstallpkg \/boot\/packages\/${PKG}/" > /tmp/go
fi
if [ $? -ne 0 ]
then
	echo "Cannot edit go script" >&2
	exit 1
fi
mv /tmp/go /boot/config/go
if [ $? -ne 0 ]
then
	echo "Cannot edit go script" >&2
	exit 1
fi
echo "Upgrade successful.  Please reboot to activate."
