#!/bin/sh

TESTF0=/tmp/testf0
TESTF1=/tmp/testf1
TESTF2=/tmp/testf2
TESTF3=/tmp/testf3

test_generate_content ()
{
	touch $TESTF0
	dd if=/dev/urandom of=$TESTF0 count=30 2>1
	touch $TESTF1
	dd if=/dev/urandom of=$TESTF1 count=40 2>1
	touch $TESTF2
	dd if=/dev/urandom of=$TESTF2 count=50 2>1
	touch $TESTF3
	dd if=/dev/urandom of=$TESTF3 count=60 2>1
}

test_erase_file ()
{
	local FILEEXT=0;
	test_generate_random $2
	FILEEXT=$?
	
	while [ ! -e $1/f_$FILEEXT ]
	do
		test_generate_random $2
		FILEEXT=$?
	done

	echo
	echo "Deleting $1/f_$FILEEXT..."
	rm $1/f_$FILEEXT
}

test_generate_random ()
{
	# uporablja interno $RANDOM spremenljivko
	MYRAND=$RANDOM
	let "MYRAND %= $1"
#	echo $MYRAND
	return $MYRAND
}

test_umount_flash ()
{
	echo "Umounting /dev/$1 ..."
	umount -f /dev/$1
}

test_create_mount_points()
{
	if [ ! -e /tmp/$1 ]; then
		echo "Creating mount point /tmp/$1 ..."
		mkdir /tmp/$1
	fi
}

test_mount_flash()
{
	echo "Mounting /dev/$1 on /tmp/$1 ..."
	if [ $2 -eq 1 ]; then
		time mount -t jffs2 /dev/$1 /tmp/$1 2>> /tmp/logg
		echo "-- -- -- -- -- -- -- -- -- --" >> /tmp/logg
	else
		mount -t jffs2 /dev/$1 /tmp/$1
	fi
}

test_erase_flash()
{
	echo "Erasing files on /dev/c$1 ..."
	/usr/sbin/flash_eraseall /dev/c$1
}

test_create_file()
{
	if [ ! -e $1 ]; then
#		echo "Creating file $1 ..."

		touch $1
		dd if=/dev/urandom of=$1 count=10 2>1
	fi
}

test_append_file()
{
#	echo "Appending to file $1"
#	TMPFILE1="/tmp/fileeexxx1"
	TMPFILE2="/tmp/fileeexxx2"
	test_generate_random 4
	TFILE=$?
	if [ $TFILE -eq 0 ];then
		cat $1 $TESTF0 >> $TMPFILE2
	elif [ $TFILE -eq 1 ];then 
		cat $1 $TESTF1 >> $TMPFILE2
	elif [ $TFILE -eq 2 ];then 
		cat $1 $TESTF2 >> $TMPFILE2
	elif [ $TFILE -eq 3 ];then 
		cat $1 $TESTF3 >> $TMPFILE2
	fi
	mv $TMPFILE2 $1

#	ITER=0
#	while [ $ITER -lt 10 ]
#	do
#		echo -n "$ITER "
#		dd if=/dev/urandom of=$TMPFILE1 count=10 2>1
#		cat $1 $TMPFILE1 >> $TMPFILE2
#		mv $TMPFILE2 $1
#
#		let ITER=ITER+1
#	done
#	echo ""
}


# ---------------------------------------------------------

NANDS="flash4 flash5 flash6"

echo "Test START."
echo "FLASH test v1.0     START @ `date`" >> /tmp/logg
echo "Nakljucno dodajanje datotek, brisanje datotek" >> /tmp/logg

test_generate_content

echo "Making enviroment sane ..."
for NAND in $NANDS
do
	test_umount_flash $NAND
	test_erase_flash $NAND
	test_create_mount_points $NAND
done

echo "Mounting clean partition ..."
for NAND in $NANDS
do
	echo "Mounting $NAND clean partition" >> /tmp/logg
	test_mount_flash $NAND 1

	echo "Filling $NAND with files ..."
	CNT=0
	LIMIT=150
	if [ $NAND = "flash5" ]; then
		LIMIT=500
	elif [ $NAND = "flash6" ]; then
		LIMIT=700
	fi

	echo ""
	while [ $CNT -lt $LIMIT ]
	do
		# generiraj random file
		test_generate_random $LIMIT
		FILE=$?

		test_create_file /tmp/$NAND/f_$FILE
		test_append_file /tmp/$NAND/f_$FILE

		echo -ne "                                                    \r"
		echo -ne "[$CNT] Appending to file /tmp/$NAND/f_$FILE\r"
		let CNT=CNT+1

		let "ERASE=$CNT%5"
		if [ $ERASE -eq 0 ]; then
			test_erase_file	/tmp/$NAND $LIMIT
		fi
	done

	test_umount_flash $NAND
	echo "Mounting $NAND dirty partition" >> /tmp/logg
	test_mount_flash $NAND 1
done

echo "Test END."
echo "FLASH test END @ `date`" >> /tmp/logg
