#!/bin/bash 

NBFRAMES=60
INTERVAL=3
DEVICE=/dev/video0
GRABOPT="-S -o png -D 5  -i sif -d $DEVICE"

DEPLACE=5

if [ $# == 1 ]
then
	JOB=$1
else
	JOB=cplus
fi

export LANG=C

for frame in $(seq 1 $NBFRAMES)
do
	echo
	figlet -W "$NBFRAMES - $frame"
	negatif=/tmp/photo_${JOB}_$(printf %03d $frame).png
	vgrabbj $GRABOPT > $negatif
	sleep $INTERVAL
done

# export TGA_OUTILS_VERBOSE=yes

for frame in $(seq 1 $NBFRAMES)
do
	echo
	photo=/tmp/photo_${JOB}_$(printf %03d $frame).tga
	negatif=/tmp/photo_${JOB}_$(printf %03d $frame).png
	pngtopnm $negatif | ppmtotga -rgb -norle > $photo
	echo Photo = $photo
	# tga_effects $photo updown $photo
	# tga_effects $photo mirror $photo
	# tga_equalize $photo std $photo
	tga_resize $photo percent $photo 72 1 0
	
	### traitement de la photo

	dst=$(printf "/tmp/tga-$JOB-a-%03d.tga" $frame)
	tga_television $photo proto $dst $DEPLACE
	tga_filtres $dst lowass $dst
	tga_cadre $dst sweep $dst 200 50 50 30

	dst=$(printf "/tmp/tga-$JOB-b-%03d.tga" $frame)
	tga_equalize $photo square $dst
	tga_television $dst proto $dst $DEPLACE
	tga_filtres $dst lowass $dst
	tga_cadre $dst burp3 $dst

	dst=$(printf "/tmp/tga-$JOB-c-%03d.tga" $frame)
	tga_equalize $photo sqroot $dst
	tga_television $dst proto $dst $DEPLACE
	tga_filtres $dst lowass $dst
	tga_cadre $dst bruit $dst 0 0 0 8

	dst=$(printf "/tmp/tga-$JOB-d-%03d.tga" $frame)
	tga_applymap 1 $photo volcano.map $dst
	tga_television $dst proto $dst $DEPLACE
	tga_filtres $dst lowass $dst
	tga_cadre $dst bruit $dst 0 0 0 8


	rm $photo
	echo "==============================================="
	
done

figlet  "almost done"

echo creating GIF, please wait...
set -v

PREFIX=pson
convert -delay 10 /tmp/tga-${JOB}-a-*.tga ${PREFIX}_${JOB}_a.gif
convert -delay 10 /tmp/tga-${JOB}-b-*.tga ${PREFIX}_${JOB}_b.gif
convert -delay 10 /tmp/tga-${JOB}-c-*.tga ${PREFIX}_${JOB}_c.gif


