#!/bin/bash
# generate moverani4.gif	9. Dezember 2000	by eddy
#
# used source files:   header.lego   car1.lego   car2.lego 
###
NoImgs=43    # number of images for merging a movie #
Brems=2
LenRoom=$(grep [Rr][ao][uo]m header.lego | awk '{ print int($3) }')
LenExt=2
LenCar1=$(grep [Rr][ao][uo]m car1.lego | awk '{ print int($3) }')
LenCar2=$(grep [Rr][ao][uo]m car2.lego | awk '{ print int($3) }')
PosCar1=$(($LenRoom + $LenExt))
PosCar2=$((0 - $LenExt - $LenCar2))
Way=$(($LenRoom + $LenExt + $LenExt + $LenCar1))  # way to go #
Way=$(($Way + ($Brems-1) * ($LenCar1 + $LenCar2) / 2 )) # no comment #
Step=$(echo "$Way / ($NoImgs -1)" | bc -l)
# there are more lego specific parameters in header.lego #
count=0
RimAngle=30
while true ; do
    if [ $count -ge $NoImgs ] ; then break ; fi
    fname=$( echo "$count" | awk '{ printf "%04d\n", $1 }' )	
    fname=img$fname
    echo creating $fname...
#
### inbetween this stripes all stdout goes to ~/bin/lego ###
#
    (   echo "############################################"
        echo "### this data stream is created by machine #"
        echo "### and is to be piped to ~/bin/lego       #"
	echo -n "### "; date
        cat header.lego
	echo Nullpunkt $PosCar1 0.01 0
	cat car1.lego |
	grep -v ignatur |
	grep -v [Rr][ao][uo]m |
	sed -e "s/ANGLE/$RimAngle/" |
	sed -e "s/ANGLE/$RimAngle/"
	echo Nullpunkt $PosCar2 0 0
	cat car2.lego |
	grep -v ignatur |
	grep -v [Rr][ao][uo]m
    ) |
#   cat > lego_in.log
#   cat   lego_in.log |
#
### inbetween this stripes all stdout goes to ~/bin/lego ###
#
    if test -s $fname.ppm ; then
    	echo $fname.ppm already exists, no need to rebuild.
    else 
#	~/bin/lego 2> lego_out.log > $fname.ppm
	~/bin/lego 2> /dev/null > $fname.ppm
        # display currently rendered image as background job #
        (
            tmpfname=tmp${fname}.ppm
            nice pnmscale -xysize 571 300 < $fname.ppm > $tmpfname
            xv -geometry +400+0 $tmpfname
            rm -f $tmpfname
            unset tmpfname
        ) &
    fi
    # a new ppm appeared (or already existed) #
    # increment / decrement:
    dif1=$(echo "$PosCar1 - $PosCar2 - $LenCar2" | bc -l)
    dif2=$(echo "$PosCar2 - $PosCar1 - $LenCar1" | bc -l)
    dif1=$(echo $dif1 | awk '{ print int($1) }' )
    dif2=$(echo $dif2 | awk '{ print int($1) }' )
    if   [ $dif1 -gt 0 ] ; then MyStep=$Step
    elif [ $dif2 -gt 0 ] ; then MyStep=$Step
    else MyStep=$(echo "$Step / $Brems" | bc -l)
    fi
    PosCar1=$(echo "$PosCar1 - $MyStep" | bc -l)
    PosCar2=$(echo "$PosCar2 + $MyStep" | bc -l)
    RimAngle=$(($RimAngle+4))
    count=$(($count+1))
done
# this is to save the result of long computing time #
tar -cvzf rawppm.tgz img*.ppm
# and now form all img????.ppm a gif animated movie is merged #
#~/bin/gifani movani4
./gifani movani4
###
