#!/bin/bash
for D in listings/*; do
    if [ -d "${D}" ]; then
			# Compile without cropping.
    	for F in ${D}/*_page.tex; do
    		if [ -f "${F}" ]; then
					echo "###### Compiling ${F} ######"
    			pdflatex --output-directory "${D}" "${F}"
		    fi
	    done
			# Compile with cropping.
	    for F in ${D}/*_crop.tex; do
	    	if [ -f "${F}" ]; then
	    		echo "###### Compiling and cropping ${F} ######"
	    		pdflatex --output-directory "${D}" "${F}"
	    		pdfcrop "${D}/$(basename -s .tex ${F}).pdf" \
	    				"${D}/$(basename -s .tex ${F}).pdf"
	    	fi
	    done
			# Compile with BibTeX and cropping.
			for F in ${D}/*_bib.tex; do
	    	if [ -f "${F}" ]; then
	    		echo "###### Compiling and cropping ${F} ######"
	    		pdflatex --output-directory "${D}" "${F}"
	    		bibtex "${F%.tex}.aux"
	    		pdflatex --output-directory "${D}" "${F}"
	    		pdflatex --output-directory "${D}" "${F}"
	    		pdfcrop "${D}/$(basename -s .tex ${F}).pdf" \
	    				"${D}/$(basename -s .tex ${F}).pdf"
	    	fi
	    done
    fi
done