#!/bin/bash


while [[ -f /data/lock ]]
	do
		sleep 1
	done


source /data/CONF/config
cd /data/IN

Date=$(date +%Y-%m-%d)

for File in $(find . -name "*.md")
	do

		InFile=$(basename ${File})
		BaseFile=$(echo $(basename ${File}) | sed 's/.md//g')
		OutFile=$(echo $(basename ${File}) | sed 's/.md//g').pdf


		Title=$(echo $OutFile | sed 's/_/ /g' | sed 's/.pdf//g')
		echo -n "$Title : "

	        dos2unix ./${InFile} &> /dev/null
		cat  ../CONF/header | sed "s/___NAME___/${Name}/g" | sed "s/___TITLE___/${Title}/g" | sed "s/___DATE___/${Date}/g" >  ./${InFile}.better
		cat ./${InFile}  >> ./${InFile}.better
		pandoc --from markdown --pdf-engine=xelatex --template "${ThemePDFNameCours}" --toc-depth 2   --listings --toc -o ../OUT/${OutFile} ./${InFile}.better &> /dev/null
		[[ $? -eq 0 ]] && echo OK || echo FAILED
	done

echo -n "Clean environement : "
rm -f ./*.better &> /dev/null
[[ $? -eq 0 ]] && echo OK || echo FAILED

