#!/bin/bash
# run	24. Juni 2000	by eddy
#
# uses source files src.lego

NoImages=8	# number of images for merging a movie #
ImageSize=523	# linear size of image quadrat #

tmp="/tmp/edy$$/"
echo "using $tmp"

###

function FxAlpha	# FxAlpha $i $NoImages
{
alpha=$(echo 360 \* "$1" / "$2" | bc -l )
echo $alpha
}

function FxBeta
{
echo 90
}

###
# setup
mkdir $tmp || exit 1
#
(
echo "Raum $((5*$ImageSize)) 1 1 1 0 0"
echo "Signature 2000 edy"
) | ~/bin/lego 2> /dev/null | \
pnmscale -xysize $ImageSize $ImageSize | \
cat > $tmp/blanco.ppm 
#
x=$(grep Raum src.lego | awk '{print $3}')
y=$(grep Raum src.lego | awk '{print $4}')
z=$(grep Raum src.lego | awk '{print $5}')
#echo x $x y $y z $z

###
i=0	# loop over images
while [ $i -lt $NoImages ]	
	do
	fname=$( echo "$i" | awk '{ printf "%04d\n", $1 }' )
	fname=img$fname.ppm
#	echo $fname
# todo: if file exists then skip #

	Alpha=$(FxAlpha $i $NoImages)
	Beta=$(FxBeta $i $NoImages)
	echo $fname Alpha $Alpha Beta $Beta

	(
	echo "Raum $((7*$ImageSize)) $x $y $z $Alpha $Beta"
	grep -v "[rR][aAoO][uUoO]m" src.lego | \
	grep -v "[sS]ignatur"
	) | \
	~/bin/lego 2> /dev/null | \
	pnmcrop -white 2> /dev/null | \
	pnmscale -xysize $ImageSize $ImageSize | \
	pnmcrop -right | \
	cat > $tmp$fname.tmp

	xdim=$(pnmfile $tmp$fname.tmp | cut -f 3 -d" ")
	ydim=$(pnmfile $tmp$fname.tmp | cut -f 5 -d" ")
	xpos=$((($ImageSize-$xdim)/2))
	ypos=$((($ImageSize-$ydim)/2))
	echo xdim $xdim ydim $ydim xpos $xpos ypos $ypos

	pnmpaste -replace $tmp$fname.tmp $xpos $ypos $tmp/blanco.ppm > $fname

	(
	nice pnmscale -xysize 397 397 $fname > $fname.show
	nice xv -geometry +0+20 $fname.show
	nice rm -f $fname.show
	) &

	i=$(($i+1))	# loop increment
	done

###
# cleanup
rm -f $tmp/blanco.ppm $tmp/img*.ppm.tmp
rmdir $tmp
###
tar -cvzf imgxxxx.tar.gz img[0-9][0-9][0-9][0-9].ppm
###
#~/bin/gifani ansicht
./gifani ansicht
###
