#!/bin/sh
# rc.d Script to download and update DSLinux
# WARNING: This script relies heavily on the format of:
# $MIRROR/index.html
# It MUST contain the following:
# Updated to revision X
# X being the current revision of $MIRROR/dslinux-dldi.tgz
# You can set the following in rc.conf:
# MIRROR="http://yourmirror.com/
# DOWNLOADTO="/path/to/save/dslinux-dldi.tgz
#
# TODO
# General tidy up, it's a bit of a mess really
# Maybe use dialog?
# An option to force install
# Sanity checks: 
# download md5sum and compare to downloaded tgz
# calculate disk space needed and disk free

[ -e /etc/rc.defaults ] && . /etc/rc.defaults
[ -e /etc/rc.conf ] && . /etc/rc.conf

# Set default mirror to Kineox
DEFAULTMIRROR="http://kineox.free.fr/DS/"
# Set $ARG1 as $1 gets overwritten later
ARG1=$1

case "$ARG1" in
	check)
		# Check for extra RAM
		exec 0</proc/meminfo
		read line
		exec 0</dev/null
		set -- $line
		if [ $2 -gt 4000 ]
		then 
			echo "$2 KB of extra RAM detected"
		else
			echo "No extra RAM detected, exiting"
			exit 1
		fi
		# Remove previous temp file if found
		if [ -f /tmp/update.mirror ]
		then
			rm /tmp/update.mirror
		fi
		if [ -z "$MIRROR" ]
		then
			echo "No mirror specified, defaulting to $DEFAULTMIRROR"
			MIRROR=$DEFAULTMIRROR
		fi
		#Write $MIRROR to tmp file as export doesn't seem to work
		echo $MIRROR > /tmp/update.mirror
		# Clear out previous temp file if found
		if [ -f /tmp/update.downloadto ]
		then
			rm /tmp/update.downloadto
		fi
		if [ -z "$DOWNLOADTO" ] 
		then
			echo "No download location specified, downloading to /media"
			DOWNLOADTO="/media"
		else	
			echo "Downloading to $DOWNLOADTO"
		fi
		#Write $DOWNLOADTO to tmp file as export doesn't seem to work
		echo $DOWNLOADTO > /tmp/update.downloadto
		if [ ! -f /etc/revision ]
		then
			echo "No /etc/revision found!"
			exit 1
		fi

		# Strip revision from /etc/revision
		LOCALVER=`cat /etc/revision|grep Revision|sed s/Revision\:\ //`
		# Strip revision from web page
		echo "Retrieving remote version information from $MIRROR"
		REMOTEVER=`links -dump $MIRROR|grep revision|sed s/\ Updated\ to\ revision\ //|sed s/\\.//`
		echo "Remote version = $REMOTEVER, Local version = $LOCALVER"

		if [ $LOCALVER -lt $REMOTEVER ]
		then
			echo "New update available"
		else
			if [ $LOCALVER -gt $REMOTEVER ]
			then
				echo "Local version is newer than remote version?"
				echo "Only Kineox should be creating builds with:"
				echo "CONFIG_DEFAULTS_RELEASE_BUILD=y"
				exit 1
			else
				echo "Already at latest update"
        			exit 1
			fi
		fi
		;;

	download)
		# Check for update
		$0 check
		# Download
		# Get MIRROR from temp file as export doesn't seem to work
		if [ -f /tmp/update.mirror ]
		then
			MIRROR=`cat /tmp/update.mirror`
			rm /tmp/update.mirror
		else
			echo "Could not find /tmp/update.mirror"
			exit 1
		fi
		# Get DOWNLOADTO from file
		if [ -f /tmp/update.downloadto ]
		then
			DOWNLOADTO=`cat /tmp/update.downloadto`
		else
			echo "Could not find /tmp/update.downloadto"
			exit 1
		fi
		if [ -f $DOWNLOADTO/dslinux-dldi.tgz ]
		then
			echo "Previous download detected"
		fi
		wget -c --progress=bar -O $DOWNLOADTO/dslinux-dldi.tgz $MIRROR/dslinux-dldi.tgz
		echo "Download complete"
		;;
	install)
		# Download (checks for update also)
		$0 download
		# Unpack
		# Get DOWNLOADTO from file
		if [ -f /tmp/update.downloadto ]
		then
			DOWNLOADTO=`cat /tmp/update.downloadto`
			rm /tmp/update.downloadto
		else
			echo "Could not find /tmp/update.downloadto"
			exit 1
		fi
		echo "Unpacking, may take a long time.  Do not switch off!"
		gzip -d -c $DOWNLOADTO/dslinux-dldi.tgz | tar xv --no-same-owner -C /media
		echo "Unpacking complete"
		echo "syncing disk"
		sync
		echo "Update complete!"
		echo "If your firmware does not support Autopatching,"
		echo "please patch /media/dslinux.nds before restarting"
		echo "eg: dlditool /path/to/dldi /media/dslinux.nds"
		exit 0
		;;
	*)
		echo "Usage:"
		echo "'update check' - Checks for update"
		echo "'update download' - Checks for update and downloads"
		echo "'update install' - Checks for update, downloads and installs"
		exit 1
		;;
esac
