Add getopt based option handling

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Signed-off-by: Richard Weinberger <richard@nod.at>
diff --git a/fsstress.sh b/fsstress.sh
index c0c65a2..3b112b0 100755
--- a/fsstress.sh
+++ b/fsstress.sh
@@ -12,7 +12,6 @@
 # The tests are currently rather archaic due to limitations of Busybox.
 #
 # TODO:
-# - getopt support
 # - report results from UBI stats interface (needs rewrite in C)
 #
 
@@ -122,6 +121,46 @@
 	done
 }
 
+usage()
+{
+	SELF=`basename "$0"`
+
+	echo "Usage: $SELF [arguments]"
+	echo "  -b, --bulk              If set, perform bulk write test."
+	echo "  -w, --writemuch         If set, perform write stress test."
+	echo "  -r, --runs <count>      Specify the number of test iterations."
+	echo "  -n, --files <count>     Specify the number of small files to create."
+	echo "  -N, --big-files <count> Specifiy the number of large files to create."
+	echo "  -h, --help              Display this text and exit."
+	echo "  --min-mb <count>        The minimum size (MiB) of small files."
+	echo "  --max-mb <count>        The maximum size (MiB) of small files."
+	echo "  --min-bulk-mb <count>   The minimum size (MiB) of large files."
+	echo "  --max-bulk-mb <count>   The maximum size (MiB) of large files."
+	exit
+}
+
+
+O=`getopt -l bulk,writemuch,runs:,files:,big-files:,max-mb:,min-mb:,max-bulk-mb:,min-bulk-mb:,help -- bwr:n:N:h "$@"`
+eval set -- "$O"
+
+while [ $# -gt 0 ]; do
+	case "$1" in
+		-b|--bulk)		BULK_WRITE=1;;
+		-w|--writemuch)	WRITE_MUCH=1;;
+		-r|--runs)		RUN_NUM=$(($2 - 1)); shift;;
+		-n|--files)		FILE_NUM=$(($2 - 1)); shift;;
+		-N|--big-files)	BIG_FILE_NUM=$(($2 - 1)); shift;;
+		-h|--help)		usage;;
+		--max-mb)		MAX_SIZE_MB="$2"; shift;;
+		--min-mb)		MIN_SIZE_MB="$2"; shift;;
+		--max-bulk-mb)	MAX_BULK_SIZE_MB="$2"; shift;;
+		--min-bulk-mb)	MIN_BULK_SIZE_MB="$2"; shift;;
+		--)				shift; break;;
+	esac
+	shift
+done
+
+
 for r in $(seq 0 $RUN_NUM)
 do
 	drop_caches
@@ -141,17 +180,20 @@
 		read_files
 	done
 
-	for i in $(seq 1 20)
-	do
-		read_files
-		write_rand_file
-	done
+	if [ $FILE_NUM -gt 0 ]
+	then
+		for i in $(seq 1 20)
+		do
+			read_files
+			write_rand_file
+		done
 
-	for i in $(seq 1 20)
-	do
-		read_files
-		write_rand_file_sync
-	done
+		for i in $(seq 1 20)
+		do
+			read_files
+			write_rand_file_sync
+		done
+	fi
 
 	if [ $WRITE_MUCH -eq 1 ]
 	then