#!/bin/sh

WARNINGS=/WebManager/warnings.txt
NEWWARNINGS=/WebManager/newwarnings.txt

[ -f $WARNINGS ] || ( touch $WARNINGS; chmod 666 $WARNINGS )

# Note: busybox tar --exclude does not work, but -X does

echo "bin/gzip\
etc/fstab\
bin/tar\
bin/busybox\
etc/init.d/rcS\
etc/init.d/upgrade\
lib/libc.so.6\
var/state/dhcp/dhcpd.leases\
lib/ld-2.2.5.so" > /tmp/root.tar.exclude

rootspace=`df -k | awk '/\/dev\/hda1/{print $4}'`

IMGNAME=cs5100

if [ -f /tmp/usb_mount_pending ]; then
	x=0
	while [ $x -lt 15 ]; do
		grep -q usbstick /proc/mounts && break
		x=`expr $x + 1`
		sleep 1
	done
fi
rm -f /tmp/usb_mount_pending

if [ -f /ftp/$IMGNAME.root.tar.gz -o -f /ftp/$IMGNAME.root.tar ]; then
	IMGDIR=/ftp
elif [ -f /usbstick/$IMGNAME.root.tar.gz -o -f /usbstick/$IMGNAME.root.tar ]; then
	IMGDIR=/usbstick
fi

if [ -f $IMGDIR/$IMGNAME.root.tar.gz -o -f $IMGDIR/$IMGNAME.root.tar ]; then
	if [ $rootspace -gt 50000 ]; then
		if [ -f $IMGDIR/$IMGNAME.root.tar.gz ]; then
			gzip -t $IMGDIR/$IMGNAME.root.tar.gz
			if [ $? -eq 0 ]; then
				echo Extracting $IMGDIR/$IMGNAME.root.tar.gz
				gzip -cdf $IMGDIR/$IMGNAME.root.tar.gz | \
				tar -x -C / -X /tmp/root.tar.exclude -f -
				[ $IMGDIR = /ftp ] && rm /ftp/$IMGNAME.root.tar.gz
			else
				warn=`date "+%d/%m/%Y %H:%M rcS: "`
				warn=$warn" corrupted compressed root image found in $IMGDIR"
				echo $warn >> $WARNINGS
				echo $warn >> $NEWWARNINGS
				[ $IMGDIR = /ftp ] && mv /ftp/$IMGNAME.root.tar.gz /ftp/$IMGNAME.root.tar.gz.bad
			fi
		fi
		if [ -f $IMGDIR/$IMGNAME.root.tar ]; then
			echo Extracting $IMGDIR/$IMGNAME.root.tar
			tar -x -C / -X /tmp/root.tar.exclude -f $IMGDIR/$IMGNAME.root.tar
			[ $IMGDIR = /ftp ] && rm /ftp/$IMGNAME.root.tar
		fi
		for f in `cat /tmp/root.tar.exclude`; do
			if [ -f /${f}.patch ]; then
				mvaside /${f}.patch /${f}
			fi
		done

		[ -f /SpliceCom/rcS ] && mvaside /SpliceCom/rcS /etc/init.d/rcS
	else
		warn=`date "+%d/%m/%Y %H:%M rcS: "`
		warn=$warn" insufficient disk space to expand root image found in $IMGDIR"
		echo $warn >> $WARNINGS
		echo $warn >> $NEWWARNINGS
	fi
fi


if [ -f /ftp/$IMGNAME.patch.tar.gz -o -f /ftp/$IMGNAME.patch.tar ]; then
	IMGDIR=/ftp
elif [ -f /usbstick/$IMGNAME.patch.tar.gz -o -f /usbstick/$IMGNAME.patch.tar ]; then
	IMGDIR=/usbstick
fi

if [ -f $IMGDIR/$IMGNAME.patch.tar.gz -o -f $IMGDIR/$IMGNAME.patch.tar ]; then
	if [ $rootspace -gt 50000 ]; then
		if [ -f $IMGDIR/$IMGNAME.patch.tar.gz ]; then
			gzip -t $IMGDIR/$IMGNAME.patch.tar.gz
			if [ $? -eq 0 ]; then
				echo Extracting $IMGDIR/$IMGNAME.patch.tar.gz
				gzip -cdf $IMGDIR/$IMGNAME.patch.tar.gz | tar -x -C / -f -
				[ $IMGDIR = /ftp ] && rm /ftp/$IMGNAME.patch.tar.gz
			else
				warn=`date "+%d/%m/%Y %H:%M rcS: "`
				warn=$warn" corrupted compressed patch image found in $IMGDIR"
				echo $warn >> $WARNINGS
				echo $warn >> $NEWWARNINGS
				[ $IMGDIR = /ftp ] && mv /ftp/$IMGNAME.patch.tar.gz /ftp/$IMGNAME.patch.tar.gz.bad
			fi
		fi
		if [ -f $IMGDIR/$IMGNAME.patch.tar ]; then
			echo Extracting $IMGDIR/$IMGNAME.patch.tar
			tar -x -C / -f $IMGDIR/$IMGNAME.patch.tar 
			[ $IMGDIR = /ftp ] && rm /ftp/$IMGNAME.patch.tar
		fi
	else
		warn=`date "+%d/%m/%Y %H:%M rcS: "`
		warn=$warn" insufficient disk space to expand patch found in $IMGDIR"
		echo $warn >> $WARNINGS
		echo $warn >> $NEWWARNINGS
	fi
fi


for pcstype in pcs100 pcs400.patch pcs410 pcs5xx pcs5x1 pcs5x3 pcs54x pcs400 pcs580 pcs582; do
	tarfile=${pcstype}.tar
	tgzfile=${tarfile}.gz
	IMGDIR=/ftp
	[ -f $IMGDIR/$tarfile -o -f $IMGDIR/$tgzfile ] || IMGDIR=/usbstick
	tarfile=$IMGDIR/$tarfile
	tgzfile=$IMGDIR/$tgzfile
	[ ! -f $tarfile -a ! -f $tgzfile ] && continue
	rootspace=`df -k | busybox awk '/\/dev\/hda1/{print $4}'`
	pcsspace=0

	case $pcstype in
		pcs100 )	bigfile=/ftp/tftp/pcs100.jffs2;		need=20000;	dest=/ftp/tftp	;;
		pcs400.patch )	bigfile=/ftp/tftp/pcs400-16M.jffs2;	need=20000;	dest=/ftp/tftp	;;
		pcs410 )	bigfile=/ftp/tftp/pcs410-256M.jffs2;	need=140000;	dest=/ftp/tftp	;;
		pcs5xx )	bigfile=/ftp/tftp/pcs5xx-256M.jffs2;	need=40000;	dest=/ftp/tftp	;;
		pcs5x1 )	bigfile=/ftp/tftp/pcs5x1-256M.jffs2;	need=40000;	dest=/ftp/tftp	;;
		pcs5x3 )	bigfile=/ftp/tftp/pcs5x3-256M.jffs2;	need=40000;	dest=/ftp/tftp	;;
		pcs54x )	bigfile=/ftp/tftp/pcs54x-256M.jffs2;	need=40000;	dest=/ftp/tftp	;;
		pcs580 )	bigfile=/ftp/tftp/pcs5lxg-256M.jffs2;	need=100000;	dest=/ftp/tftp	;;
		pcs582 )	bigfile=/ftp/tftp/pcs5lxg-256M.jffs2;	need=100000;	dest=/ftp/tftp	;;
		pcs400 )	bigfile=/pcs400;			need=160000;	dest=/		;;
	esac

	[ -e $bigfile ] && pcsspace=`du -s $bigfile | busybox awk '{print $1}'`
	rootspace=`expr $rootspace + $pcsspace`

	if [ $rootspace -gt $need ]; then
		if [ -f $tgzfile ]; then
			gzip -t $tgzfile
			if [ $? -eq 0 ]; then
				echo Extracting $tgzfile
				gzip -cdf $tgzfile | tar -x -C $dest -f -
				[ $IMGDIR = "/ftp" ] && rm -f $tgzfile
			else
				warn=`date "+%d/%m/%Y %H:%M rcS: "`
				warn=$warn" corrupted compressed $tgzfile image found"
				echo $warn >> $WARNINGS
				echo $warn >> $NEWWARNINGS
				mv $tgzfile ${tgzfile}.bad
			fi
		fi
		if [ -f $tarfile ]; then
			echo Extracting $tarfile
			tar -x -C $dest -f $tarfile 
			[ $IMGDIR = "/ftp" ] && rm -f $tarfile
		fi
	else
		warn=`date "+%d/%m/%Y %H:%M rcS: "`
		warn=$warn" insufficient disk space to expand $tarfile image"
		echo $warn >> $WARNINGS
		echo $warn >> $NEWWARNINGS
	fi
done


if [ -f /usbstick/newdatabase.txt ]; then
	cp /usbstick/newdatabase.txt /ftp
	/ftp/ChangeMac
fi

