#!/bin/sh

TEST_DEV="/dev/md0"
TEST_MP="/mnt"

MY_PATH=${PWD}

run_fstorture() # ${1} => TEST_DIR_PATH
{
	if [ ! -f ./fstorture ]; then
		echo "Can not find test binary in ${PWD}"
		exit 1
	fi

	mkdir ${1}/root0 ${1}/root1 
	cd ${1} && ./fstorture root0 root1 4 -c 1000
}

echo " =========== Test notmal case without gd_csum to avoid regression..."
mkfs.ext3 -F ${TEST_DEV}
mount -t ext2fs ${TEST_DEV} ${TEST_MP}
cp ./fstorture ${TEST_MP}/fstorture
run_fstorture ${TEST_MP}
cd ${MY_PATH}

sleep 2

umount ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "Can not unmount..."
	exit 1
fi

e2fsck -f -n ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "e2fsck fail"
	exit 1
fi

echo " =========== Test feature in normal block size..."
mkfs.ext3 -F -O uninit_bg ${TEST_DEV}
mount -t ext2fs ${TEST_DEV} ${TEST_MP}
cp ./fstorture ${TEST_MP}/fstorture
run_fstorture ${TEST_MP}
cd ${MY_PATH}

sleep 2

umount ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "Can not unmount..."
	exit 1
fi

e2fsck -f -n ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "e2fsck fail"
	exit 1
fi

echo " =========== Test feature in block size 1K..."
mkfs.ext3 -F -O uninit_bg -b 1k ${TEST_DEV}
mount -t ext2fs ${TEST_DEV} ${TEST_MP}
cp ./fstorture ${TEST_MP}/fstorture
run_fstorture ${TEST_MP}
cd ${MY_PATH}

sleep 2

umount ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "Can not unmount..."
	exit 1
fi

e2fsck -f -n ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "e2fsck fail"
	exit 1
fi

echo " =========== Test feature in block size 2K..."
mkfs.ext3 -F -O uninit_bg -b 2k ${TEST_DEV}
mount -t ext2fs ${TEST_DEV} ${TEST_MP}
cp ./fstorture ${TEST_MP}/fstorture
run_fstorture ${TEST_MP}
cd ${MY_PATH}

sleep 2

umount ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "Can not unmount..."
	exit 1
fi

e2fsck -f -n ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "e2fsck fail"
	exit 1
fi

echo " =========== Test feature in block size 64K..."
mkfs.ext3 -F -O uninit_bg -b 64k ${TEST_DEV}
mount -t ext2fs ${TEST_DEV} ${TEST_MP}
cp ./fstorture ${TEST_MP}/fstorture
run_fstorture ${TEST_MP}
cd ${MY_PATH}

sleep 2

umount ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "Can not unmount..."
	exit 1
fi

e2fsck -f -n ${TEST_DEV}
if [ "$?" -ne "0" ]
then 
	echo "e2fsck fail"
	exit 1
fi


