#!/bin/bash

#
#	encodage de la vidz du Lieu Inconnu
#
# ---------------------------------------------------------------

set -e				# abort on error

# ---------------------------------------------------------------

source ./configuration.sh
source ./do_effects.sh

EFFECT="none"

if [ $# -eq 1 ] ; then
	EFFECT=$1
	figlet $EFFECT
	sleep 2
fi

log_event "start encode $EFFECT"

SRCDIR="frames/indoor"
DSTDIR="frames/spool/indo"
TMPNG="/dev/shm/tmpencode.png"
TMPGA="/dev/shm/tmpencode.tga"

export TGA_OUTILS_VERBOSE="no"

IDX=0

rm -vf $DSTDIR/*.png

tga_tools version

# ---------------------------------------------------------------
#
operation ()
{
echo  $1 ' => ' $EFFECT ' => '$2

case ${EFFECT} in
	"none")		cp $1 $2			;;
	"blur")		do_blur $1 $2			;;
	"pixelx")	do_pixel_x $1 $2		;;
	"pixely")	do_pixel_y $1 $2		;;
	"warp0")	do_warp0 $1 $2			;;
	"rastarec")	do_rastarecurse	$1 $2		;;
	"canalplus")	do_canalplus $1 $2		;;
	"rotulacion")	do_rotulacion $1 $2		;;
	"palettes")	do_palettes $1 $2		;;
	"binsobel")	do_binary_sobel $1 $2		;;
	"combine")	do_combination $1 $2		;;
	"decompose")	do_decomp_burp $1 $2		;;
	"brotched")	do_brotched $1 $2		;;
	"contour2x2")	do_contour_2x2 $1 $2		;;
	"mosaic")	do_mosaic_blablanux $1 $2	;;
	"doubletv")	do_double_television $1 $2	;;
	"waterlopass")	do_water_lopass $1 $2		;;
	"indoor")	cp -v $1 $2			;;
	*)		echo "EPICFAIL" ; exit 1	;;
esac

}
# ---------------------------------------------------------------
#

# ---------------------------------------------------------------
#	insert a start sequence
#
sequence_intro ()
{
if [ "$EFFECT" = "none" ] ; then
	echo "=== intro disabled ==="
	return
fi
figlet "INTRO"

texte="Dans un\nlieu inconnu."

operation frames/indoor/0666.png $TMPNG
mogrify -brightness-contrast -30x-30 $TMPNG

for foo in $(seq 0 170)
do
	dst=$( printf "%s/%05d.png" $DSTDIR $IDX)
	echo intro $dst

	pointsize=$(( ( foo * 2 ) + 10 ))
	convert		$TMPNG				\
			-gravity	Center		\
			-font 		Palatino-Roman	\
			-pointsize	$pointsize	\
			-fill		$COLTEXT_B	\
			-annotate +0+0  "$texte"	\
			-fill		$COLTEXT_A	\
			-annotate +5+5  "$texte"	\
			$dst
	IDX=$(( IDX + 1 ))

done
}

# ---------------------------------------------------------------
#	the real pictures...
#
sequence_movie ()
{
figlet "MOVIE"

for img in $SRCDIR/*.png
do
	dst=$( printf "%s/%05d.png" $DSTDIR $IDX)
	operation $img $dst
	IDX=$(( IDX + 1 ))
	# display $img &
	# display $dst &
	# exit
done
sync
}
# ---------------------------------------------------------------
#	and the final...
#
sequence_final_bonus ()
{
count=$1

if [ "$EFFECT" = "none" ] ; then
	echo "=== final disabled ==="
	return
fi
figlet "FINAL"

tdate=$(date +'Made on %F %H:%M\nby tTh and Konrad')
operation WS/finalindoor.png $TMPNG

bar=$(( $count/ 2 ))

for foo in $(seq 0 $count)
do
	dst=$( printf "%s/%05d.png" $DSTDIR $IDX)

	cp $TMPNG $dst	

	line=$(( -15 + foo ))
	size=$((  40  + foo/4 ))
	convert	$TMPNG					\
		-gravity north				\
		-pointsize 48 -fill $COLTEXT_B		\
		-font $TEXT_BF				\
		-kerning 8				\
		-annotate +0+${line} "$tdate"		\
		$dst

	if [ $foo -gt  $bar ] ; then
		convert	$dst					\
			-pointsize 118 -fill $COLTEXT_A		\
			-font $TEXT_BF				\
			-gravity south				\
			-annotate +0+190 'And now, the Bonus'	\
			$dst 		
	fi

	IDX=$(( IDX + 1 ))
done
}
# ---------------------------------------------------------------
#
#	la sequence qui fait de l'incrustation
#
sequence_bonus_timelapse ()
{
local grabdir="frames/snapshot"
local basetga="/dev/shm/base-incrust.tga"

if [ "$EFFECT" = "none" ] ; then
	echo "=== timelapse disabled ==="
	return
fi

figlet "TIMELAPSE $1"
echo "timelapse/incrust pictures are from " $grabdir

operation frames/indoor/0666.png "WS/foo.png"
mogrify -brightness-contrast -30x-30 WS/foo.png

GOTO_TGA  "WS/foo.png"              $basetga
echo "making $basetga     [done]"

liste=$( ls -rt1 $grabdir/*.png | tail -$1 )

for foo in $liste
do
	dst=$( printf "%s/%05d.png" $DSTDIR $IDX)
	echo poking $foo to $dst

	do_incruste $basetga $foo $dst
	IDX=$(( IDX + 1 ))
	echo
done
}
# ---------------------------------------------------------------
sequence_bonus_epicfail ()
{

if [ "$EFFECT" = "none" ] ; then
	echo "=== epicfail  disabled ==="
	return
fi
figlet "BONUS EPIC"

operation frames/indoor/0666.png $TMPNG
mogrify -brightness-contrast -30x-30 WS/foo.png

for foo in $(seq 0 $1)
do
	dst=$( printf "%s/%05d.png" $DSTDIR $IDX)
	line=$((     10 + ($RANDOM % 1290) ))
	column=$(( -200 + ($RANDOM % 1920) ))

	if [ 50 -gt $(( $RANDOM % 100)) ] ; then
		color="$COLTEXT_A"
	else
		color="$COLTEXT_B"
	fi
	printf "+%s " $color
	convert	$TMPNG						\
		-font $TEXT_IT					\
		-pointsize 56					\
		-kerning 8					\
		-fill $color					\
		-annotate +${column}+${line} 'Yo tTh !'		\
		$TMPNG
	cp $TMPNG $dst

	IDX=$(( IDX + 1 ))
done
echo
}
# ---------------------------------------------------------------
#                    _       
#    _ __ ___   __ _(_)_ __  
#   | '_ ` _ \ / _` | | '_ \ 
#   | | | | | | (_| | | | | |
#   |_| |_| |_|\__,_|_|_| |_|
#                            

sequence_intro
sequence_movie
sequence_final_bonus     190
sequence_bonus_timelapse 280
sequence_bonus_epicfail  200

# ---------------------------------------------------------------
figlet 'ENCODE'

outname="Films/pproc-${EFFECT}.mp4"
if [ "$EFFECT" = "none" ] ; then
	outname="livecoding.mp4"
fi

echo === saving vidz to $outname ===

ffmpeg  -nostdin                                                \
	-hide_banner						\
        -y -r 30 -f image2 -i $DSTDIR/%05d.png                  \
	-metadata artist='---[ tTh ]---'			\
	-metadata title='---[ Dans un lieu inconnu... ]---'	\
 	-preset veryslow					\
       -c:v libx264 -pix_fmt yuv420p				\
        $outname

figlet '+++ END +++'

log_event "fin encode $EFFECT"

### wc -c *.mp4 |	sort -n | grep -v ' total$' |		\
###		awk '{ $1/=(2^20); printf "%-25s %8.1f Mo\n", $2, $1; }'

#
