#!/bin/sh
##################################################
# Buildscript for Github-Action and Pandoc
#
# author: Sebastian Preisner <kreativmonkey@calyrium.org>
# date: 11.11.2021
##################################################

# Installing the neccessary tex packages in the action container
echo "Installing neccessary tex packages"
tlmgr update --self
tlmgr install lastpage \
              titling \
              || exit 1


# Creating the folders to build the files in
if [ ! -d output ]; then
	mkdir output	
fi
if [ ! -d output/pdf ]; then
	# Automated pdf generating in the pdf folder
	mkdir output/pdf
fi
if [ ! -d output/tex ]; then
	# Automated tex generating for debugging
	mkdir output/tex
fi

dir=$(echo $PWD)
template="template/wbh.tex" # Setting the template path
logo="static/logo-wbh.png" # setting the logopath for the template


# Build files by folder
pandoc_build_by_folder() {
folder=$1

cd "$dir/$folder"
echo "..... $folder.tex"
pandoc -s --template "../$template" -V logo="../${logo}" -o "../output/tex/$folder.tex" "$dir/$folder/meta.md" "$dir/$folder/README.md"
echo "..... $folder.pdf"
pandoc -s --template "../$template" -V logo="../${logo}" -o "../output/pdf/$folder.pdf" "$dir/$folder/meta.md" "$dir/$folder/README.md"
cd "$dir"
}

pandoc_build_by_filelist()
{
folder=$1

cd "$dir/$folder"
echo "..... $folder.tex"
pandoc -s --template "../$template" -V logo="../${logo}" -o "../output/tex/$folder.tex" "$dir/$folder/meta.md" 
echo "..... $folder.pdf"
pandoc -s --template "../$template" -V logo="../${logo}" -o "../output/pdf/$folder.pdf" "$dir/$folder/meta.md"
cd "$dir"
}


# Build the Files with Pandoc
echo "Creating Files"

pandoc_build_by_folder "Expose"
