#!/bin/sh

DIRECTORY=$1
MTDBOOT=2
MTDROOT=3

if grep -qs 7020 /proc/bus/dreambox ; then
	BOXTYPE=dm7020
	VSND=35
	OPTIONS="--eraseblock=0x4000 -n -b"
elif grep -qs DM600PVR /proc/bus/dreambox ; then
	BOXTYPE=dm600pvr
	VSND=54
	OPTIONS="--eraseblock=0x4000 -n -b"
elif grep -qs DM500PLUS /proc/bus/dreambox ; then
	BOXTYPE=dm500plus
	VSND=54
	OPTIONS="--eraseblock=0x4000 -n -b"
elif grep -qs 'ATI XILLEON HDTV SUPERTOLL' /proc/cpuinfo ; then
	BOXTYPE=dm7025
	VSND=61
	OPTIONS="--eraseblock=0x4000 -n -l"
elif grep -qs 'dm8000' /proc/stb/info/model ; then
	BOXTYPE=dm8000
	VSND=65
	OPTIONS="--eraseblock=0x20000 -n -l"
elif grep -qs 'dm800' /proc/stb/info/model ; then
	BOXTYPE=dm800
	VSND=62
	OPTIONS="--eraseblock=0x4000 -n -l"
else
	echo "Box not found !!!"
	exit 0
fi

echo $BOXTYPE " found"
echo "***"

DATE=`date +%Y%m%d_%H%M%S`
MKFS=/usr/bin/mkfs.jffs2
BUILDIMAGE=/usr/bin/buildimage
BACKUPIMAGE=$DIRECTORY/backup-image-$BOXTYPE-$DATE.nfi
SND=secondstage-$BOXTYPE-$VSND.bin

if [ ! -f $MKFS ] ; then
	echo $MKFS" not found"
	exit 0
fi
if [ ! -f $BUILDIMAGE ] ; then
	echo $BUILDIMAGE" not found"
	exit 0
fi

rm -rf $DIRECTORY/bi
mkdir -p $DIRECTORY/bi
mkdir -p /tmp/bi/root
mkdir -p /tmp/bi/boot

mount -t jffs2 /dev/mtdblock/$MTDROOT /tmp/bi/root
mount -t jffs2 /dev/mtdblock/$MTDBOOT /tmp/bi/boot

echo "download " $SND
wget -q http://sources.dreamboxupdate.com/download/7020/$SND -O /tmp/secondstage.bin > /dev/null


if [ -s /tmp/secondstage.bin ] ; then
	echo "create boot.jffs2"
	$MKFS --root=/tmp/bi/boot --faketime --output=$DIRECTORY/bi/boot.jffs2 $OPTIONS
	echo "create root.jffs2"
	$MKFS --root=/tmp/bi/root --faketime --output=$DIRECTORY/bi/root.jffs2 $OPTIONS
	echo "create secondstage"
	if [ $BOXTYPE = "dm800" -o $BOXTYPE = "dm8000" ] ; then
		cp /tmp/secondstage.bin $DIRECTORY/bi/main.bin.gz
	else
		gzip -c /tmp/secondstage.bin > $DIRECTORY/bi/main.bin.gz
	fi
	rm /tmp/secondstage.bin
	
	echo "create" $BOXTYPE "nfi-image"
	
	if [ $BOXTYPE = "dm7025" ] ; then
		$BUILDIMAGE $DIRECTORY/bi/main.bin.gz $DIRECTORY/bi/boot.jffs2 $DIRECTORY/bi/root.jffs2 $BOXTYPE > $BACKUPIMAGE
	elif [ $BOXTYPE = "dm800" ] ; then
		$BUILDIMAGE $DIRECTORY/bi/main.bin.gz $DIRECTORY/bi/boot.jffs2 $DIRECTORY/bi/root.jffs2 $BOXTYPE 64 > $BACKUPIMAGE
	elif [ $BOXTYPE = "dm8000" ] ; then
		$BUILDIMAGE $DIRECTORY/bi/main.bin.gz $DIRECTORY/bi/boot.jffs2 $DIRECTORY/bi/root.jffs2 $BOXTYPE 64 large > $BACKUPIMAGE
	elif [ $BOXTYPE = "dm7020" ] ; then
		$BUILDIMAGE $DIRECTORY/bi/main.bin.gz $DIRECTORY/bi/boot.jffs2 $DIRECTORY/bi/root.jffs2 > $BACKUPIMAGE
	elif [ $BOXTYPE = "dm600pvr" ] ; then
		$BUILDIMAGE $DIRECTORY/bi/main.bin.gz $DIRECTORY/bi/boot.jffs2 $DIRECTORY/bi/root.jffs2 $BOXTYPE > $BACKUPIMAGE
	elif [ $BOXTYPE = "dm500plus" ] ; then
		$BUILDIMAGE $DIRECTORY/bi/main.bin.gz $DIRECTORY/bi/boot.jffs2 $DIRECTORY/bi/root.jffs2 $BOXTYPE > $BACKUPIMAGE
	fi

	if [ -f $BACKUPIMAGE ] ; then
		echo "Image create on" $BACKUPIMAGE
	fi
else
	echo "download error"
fi

echo "***"
umount /tmp/bi/root
umount /tmp/bi/boot
#rm -rf /tmp/bi
rm -rf $DIRECTORY/bi

exit