#!/usr/bin/env bash
# Make sure the current directory is the location of this script to simplify matters
cd "$(dirname $(readlink -f $0))";
################
### Settings ###
################

# The name of this project
project_name="Example latext templates";

# The path to the lantern build engine git submodule
lantern_path="./lantern-build-engine";

###
# Custom Settings
###

# Put any custom settings here.


###############################################################################

# Check out the lantern git submodule if needed
if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi

source "${lantern_path}/lantern.sh";

if [[ "$#" -lt 1 ]]; then
	echo -e "${FBLE}${project_name}${RS} build script";
	echo -e "    by Starbeamrainbowlabs";
	echo -e "${LC}Powered by the lantern build engine, v${version}${RS}";
	echo -e "";
	echo -e "${CSECTION}Usage${RS}";
	echo -e "    ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ...";
	echo -e "";
	echo -e "${CSECTION}Available actions${RS}";
	echo -e "    ${CACTION}setup${RS}    - Perform initial setup";
	echo -e "    ${CACTION}render${RS}   - Render the LaTeX exmaples";
	echo -e "";
	
	exit 1;
fi

###############################################################################

task_setup() {
	task_begin "Setting up";
	
	check_command git true;
	check_command pdflatex true;
	check_command bibtex true;
	
	subtask_begin "Initialising submodules";
	git submodule update --init;
	subtask_end $?;
	
	task_end 0;
}

task_render() {
	_render-latex-pdf "report/report.tex";
	_render-latex-pdf "short-report/short-report.tex";
}

# $1 - Location of top-level LaTeX file
_render-latex-pdf() {
	if [[ ! -f "$1" ]]; then
		task_end 1 "Error: Couldn't find '$1'";
	fi
	
	task_begin "Entering directory";
	latex_filename="$(basename "$1")";
	latex_directory="$(dirname "$1")";
	
	execute cd "${latex_directory}";
	execute echo "${PWD}";
	task_end $? "Failed to enter directory (does it exist?)";
	
	task_begin "Cleaning up";
	find -iname "*.aux" -delete; # Ref: https://tex.stackexchange.com/q/381057
	find -iname "*.bbl" -delete;
	find -iname "*.blg" -delete;
	find -iname "*.out" -delete;
	task_end $? "Error: Failed to clean up after last build";
	
	# task_begin "Rendering images";
	# # FUTURE: Do this in paralell?
	# for svg_filename in $(find "images/" -type f -iname "*.svg"); do
	# 	execute inkscape -e ${svg_filename%%.svg}.png ${svg_filename};
	# 	exit_code=$?;
	# 	[[ "${exit_code}" -eq 0 ]] || break;
	# done
	# task_end "${exit_code}";
	
	task_begin "Building Report";
	set -e;
	execute pdflatex --output-directory=. "${latex_filename}";
	execute bibtex "${latex_filename%.*}";
	execute pdflatex --output-directory=. "${latex_filename}";
	execute pdflatex --output-directory=. "${latex_filename}";
	execute bibtex "${latex_filename%.*}";
	execute pdflatex --output-directory=. "${latex_filename}";
	execute pdflatex --output-directory=. "${latex_filename}";
	set +e;
	task_end $? "Error: Failed to build report";
	
	task_begin "Moving report";
	execute mv *.pdf ..;
	task_end $? "Failed to move report";
	
	cd -;
}


###############################################################################

tasks_run $@;