function processDir
{
for a in `ls $1/*.jpg 2>/dev/null`
do
fname=`echo $a | sed -e s:$2/:: | sed -e s:/:_: | sed -e s/\\\./_/`
printTemplate $fname $a >> $3
done;
for a in `ls $1/*.png 2>/dev/null`
do
fname=`echo $a | sed -e s:$2/:: | sed -e s:/:_: | sed -e s/\\\./_/`
printTemplate $fname $a >> $3
done;
}

function printTemplate
{
filtering="NONE"
mode="FULL"
xc=-1;
yc=-1;
width=0;
height=0;

echo $2 | grep "SMOOTH" >/dev/null
if [ $? == 0 ]
then
filtering="SMOOTH"
else
filtering="NONE"
fi

echo $2 | grep "CELL" >/dev/null
if [ $? == 0 ]
then
mode="CELL"
xc=`echo $2 | cut -f2 -d'_'`
yc=`echo $2 | cut -f3 -d'_'`
width=`identify -format "%[fx:w/$xc]" $a`
height=`identify -format "%[fx:h/$yc]" $a`
else
mode="FULL"
fi

echo "new t2dImageMapDatablock($1) { 
      imageName = \"./$2\";
      imageMode = \"$mode\";
      frameCount = \"-1\";
      filterMode = \"$filtering\";
      filterPad = \"1\";
      preferPerf = \"1\";
      cellRowOrder = \"1\";
      cellOffsetX = \"0\";
      cellOffsetY = \"0\";
      cellStrideX = \"0\";
      cellStrideY = \"0\";
      cellCountX = \"$xc\";
      cellCountY = \"$yc\";
      cellWidth = \"$width\";
      cellHeight = \"$height\";
      preload = \"0\";
      allowUnload = \"0\";
      force16Bit = \"0\";
   };"
}

if [ -z $3 ]
then
echo "Usage: $0 <template file> <output file> <image dir>"
exit 1
fi

cat $1 > $2

for a in `find_unix $3 -type d`
do
processDir $a $3 $2
done

echo "};">>$2
