#!/bin/sh
DIR=/scratch3
NUMBER=100
#NUMBER=100

hsm_wait()
{
	while [ 1 ]; do
		ACTIONS=$(pdsh -w mds03 lctl get_param mdt.scratch3-MDT0000.hsm.actions | grep -v SUCCEED | grep -v mdt.scratch3-MDT0000.hsm.actions=)
		if [ "$ACTIONS" != "" ]; then
			echo "Still some actions, waiting for 1 second"
			echo "$ACTIONS"
			sleep 1
		else
			break
		fi
	done
}

if [ "$CREATE" = "yes" ]; then
	rm $DIR/file-$i -f
	for i in `seq 0 $NUMBER`; do
	        dd if=/dev/zero of=$DIR/file-$i bs=1M count=10
	done
fi

for i in `seq 0 $NUMBER`; do
	if [ ! -f $DIR/file-$i ]; then
		echo "$DIR/file-$i is not found"
		exit 1
	fi
done

echo "Starting archive"
for i in `seq 0 $NUMBER`; do
        lfs hsm_archive --archive 5 $DIR/file-$i
	if [ $? -ne 0 ]; then
		echo "Failed to archive file"
		exit 1
	fi
done
echo "Archive completed"

hsm_wait

echo "Starting release"
for i in `seq 0 $NUMBER`; do
	if [ "$PARALLEL" = "yes" ]; then
		lfs hsm_release $DIR/file-$i &
	else
	        lfs hsm_release $DIR/file-$i
		if [ $? -ne 0 ]; then
			echo "Failed to release file"
			exit 1
		fi
	fi
	wait
done
echo "Release completed"

if [ "$MD5SUM" = "yes" ]; then
	for i in `seq 0 $NUMBER`; do
		if [ "$PARALLEL" = "yes" ]; then
			md5sum $DIR/file-$i &
		else
			md5sum $DIR/file-$i
		fi
	done
	wait
fi

