#!/bin/sh

# PHP implementation of TPC-W
# 
# Copyright (c) 2004, Jan Wieck
# 
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose, without fee, and without a
# written agreement is hereby granted, provided that the above
# copyright notice and this paragraph and the following two
# paragraphs appear in all copies.
# 
# IN NO EVENT SHALL THE AUTHORS BE LIABLE TO ANY PARTY FOR DIRECT,
# INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
# INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
# AND ITS DOCUMENTATION, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# 
# THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED
# HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHORS HAVE NO
# OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
#
# generate_images.sh
#
# Shell script to drive the tpcwIMG program 

# Change directory to image_generation
cd `dirname $0`

tpcwIMG=./ImgGen/ImgFiles/tpcwIMG
basedir=../html/tpcw/images

# ----
# Check commandline 
# ----
if test $# -ne 1 ; then
	echo "usage: $0 num_items" >&2
	exit 1
fi

# ----
# Check that the image generator program exists
# ----
if test ! -x $tpcwIMG ; then
	echo "program $tpcwIMG does not exist or is not executable" >&2
	exit 1
fi

if test ! -d $basedir ; then
	echo "$basedir is not a directory" >&2
	exit 1
fi
rm -r $basedir/items
rm -r $basedir/thumbnails
mkdir -p $basedir/items || exit 2
mkdir -p $basedir/thumbnails || exit 2

img_no=1
num_loops=`expr $1 / 100`

echo "will run $num_loops loops creating groups of 100 images+thumbnails each"

loop=0
while [ $loop -lt $num_loops ] ; do
	grpno=`expr $loop / 10 + 1`

	loop=`expr $loop + 1`
	echo "loop $loop"

	item_grp="$basedir/items/group_$grpno"
	thumb_grp="$basedir/thumbnails/group_$grpno"
	mkdir -p $item_grp || exit 2
	mkdir -p $thumb_grp || exit 2

	$tpcwIMG 5 $img_no >/dev/null; mv $img_no $thumb_grp/item_$img_no.jpg
	$tpcwIMG 250 $img_no >/dev/null; mv $img_no $item_grp/item_$img_no.jpg
	img_no=`expr $img_no + 1`
	
	for i in 1 2 3 4 ; do
		$tpcwIMG 5 $img_no >/dev/null; mv $img_no $thumb_grp/item_$img_no.jpg
		$tpcwIMG 100 $img_no >/dev/null; mv $img_no $item_grp/item_$img_no.jpg
		img_no=`expr $img_no + 1`
	done

	for i in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 ; do
		$tpcwIMG 5 $img_no >/dev/null; mv $img_no $thumb_grp/item_$img_no.jpg
		$tpcwIMG 50 $img_no >/dev/null; mv $img_no $item_grp/item_$img_no.jpg
		img_no=`expr $img_no + 1`
	done

	for i in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 ; do
		$tpcwIMG 5 $img_no >/dev/null; mv $img_no $thumb_grp/item_$img_no.jpg
		$tpcwIMG 10 $img_no >/dev/null; mv $img_no $item_grp/item_$img_no.jpg
		img_no=`expr $img_no + 1`
	done

	for i in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 ; do
		$tpcwIMG 5 $img_no >/dev/null; mv $img_no $thumb_grp/item_$img_no.jpg
		$tpcwIMG 5 $img_no >/dev/null; mv $img_no $item_grp/item_$img_no.jpg
		img_no=`expr $img_no + 1`
	done

done
