#!/bin/bash

printf "============== assemblage %d ==========\n" $$

source ./config.sh
source ./fonctions.sh

FONTE="Century-Schoolbook-L-Roman"

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

nextpic=0		# index of next pic to be linkfarmed

# ---------------------------------------------------------
init_linkfarm ()
{
message 0 "init linkfarm"

}
# ---------------------------------------------------------
#
#	this func add a _symlink_ to the link farm
#	use it for static or pre-generated files
#
add_to_linkfarm ()
{
local picz="$1"
local linkname

linkname=$(printf "%s/%05d.png" $LINKFARM $nextpic)
# echo "  adding link " $linkname

ln -s -f  $picz  $linkname

nextpic=$(( nextpic+1 ))
}
# ---------------------------------------------------------
#
#	this func _copy_ an image file to the linkfarm
#	use it for dynamicaly generated files
#
copy_to_linkfarm ()
{
local src="$1"
local linkname

linkname=$(printf "%s/%05d.png" $LINKFARM $nextpic)

# message 1 "copy $src to $linkname"

cp $src $linkname

nextpic=$(( nextpic+1 ))

}
# ---------------------------------------------------------
generique_intro ()
{
local texte="$1"
local i color

local src="WS/plan_de_coupe.png" dst

printf "Generique intro    [%s]\n" "$texte"

for i in $(seq 0 255)
do

	color=$(printf "#%02x%02x%02x" $i $i $i)
	dst="/dev/shm/generique.png"
	# printf "%4d    %s\n" $i  $color

	convert  $src						\
		-font		"$FONTE"			\
		-fill		"$color"			\
		-pointsize	204				\
		-gravity	Center				\
		-annotate +0+0 "${texte}"			\
		$dst

	copy_to_linkfarm $dst

done
}
# ---------------------------------------------------------
generique_fin ()
{
local texte="$1"
local i size dst
local src="WS/plan_de_coupe.png"

printf "Generique fin      [%s]     %d\n" "$texte" $nombre

for i in $(seq 0 255)
do

	size=$( printf "%d" $((i+10)) )
	dst="/dev/shm/generique.png"
	# printf "%4d    %d\n" $i  $size

	convert  $src						\
		-font		"$FONTE"			\
		-fill		"$COLTXT_A"			\
		-pointsize	$size				\
		-gravity	Center				\
		-annotate +0+0 "${texte}"			\
		$dst

	copy_to_linkfarm $dst

done
}
# ---------------------------------------------------------
add_sequence ()
{
local srcdir="$1"
local png

local nbre=$(ls -1 $srcdir/*.png | wc -l)
printf "Add sequence %-20s  %d\n" $srcdir $nbre

for png in $srcdir/*.png
do
	target=$(printf "../%s" $png)
	# echo "adding " $target
	add_to_linkfarm $target

done
}
# ---------------------------------------------------------
plan_de_coupe ()
{
local nbpass="$1"
local title="$2"

local src="WS/plan_de_coupe.png" dst
local idx txt ypos dst

message 1 "plan de coupe $title"

for idx in $(seq 0 255)
do

	txt=$( printf "(%s)" $title  )
	# echo "++++ $txt"
	dst="/dev/shm/coupe.png"
	ypos=$(( 10 + (idx * 5) ))
	convert  $src						\
		-font		"$FONTE"			\
		-fill		"$COLTXT_A"			\
		-pointsize	122				\
		-gravity	Center				\
		-annotate +${ypos}+0 "${txt}"			\
		$dst

	copy_to_linkfarm $dst
done
}

# ---------------------------------------------------------
#
#		MAIN PROGRAM
#
init_linkfarm

nbc=150
generique_intro "Champs d'altitudes"

plan_de_coupe $nbc "altitudes"	;	add_sequence "frames/altitudes/"
plan_de_coupe $nbc "timelapse"	;	add_sequence "frames/timelapse/"
plan_de_coupe $nbc "hilevel"	;	add_sequence "frames/hilevel/"
plan_de_coupe $nbc "gamma"	;	add_sequence "frames/gamma/"
plan_de_coupe $nbc "triplane"	;	add_sequence "frames/triplane/"
plan_de_coupe $nbc "colmap"	;	add_sequence "frames/colmap/"

generique_fin   "C'est fini..." 
echo

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