#!/bin/sh
#
# mmd2pdf --- MultiMarkdown convenience script
#	<http://fletcherpenney.net/multimarkdown/>
#	Fletcher T. Penney
#
# Pass arguments on to the binary to convert text to LaTeX
# Then use latexmk to process into PDF.
# Requires a few extra passes of pdflatex to be sure all autorefs
# 	are managed.
# Then call latexmk with -c option to try and clean up some extra files.
#
#	NOTE: This file is included as a convenience for users - it's not 
#		likely to fail gracefully if there are any issues in your
#		LaTeX file.

# Be sure to include multimarkdown and latex in our PATH
unset CDPATH
SCRIPT="$0"
while
	DIR="$(cd -P -- "$(dirname -- "$SCRIPT")" && pwd)"
	test -h "$SCRIPT"
do
	SCRIPT="$(readlink "$SCRIPT")"
	[ "$SCRIPT" = "${SCRIPT#/}" ] && SCRIPT="$DIR/$SCRIPT"
done
PATH="$DIR:/usr/local/bin:/usr/texbin:/Library/TeX/texbin:$PATH"
export PATH

if ! command -v multimarkdown >/dev/null 2>&1
then
	echo multimarkdown executable not found! >&2
	exit 1
fi

if [ $# = 0 ]
then
	multimarkdown -t latex
else
until [ "$*" = "" ]
do
	multimarkdown -b -t latex "$1"

	file_name=`echo $1| sed 's/\.[^.]*$//'`
	
	# Check for XeLaTeX mode
	
	xelatex=`multimarkdown -e usexelatex "$1"`
	
	if [ "$xelatex" != "" ]
	then
		# Use XeLaTeX
		
		xelatex "$file_name.tex"

		if [ "$?" = "127" ]
		then
			echo "It doesn't appear that xelatex is installed properly." 1>&2
			echo "Be sure you have a working LaTeX installation." 1>&2
			exit 1
		fi

		xelatex "$file_name.tex"
		xelatex "$file_name.tex"
		xelatex "$file_name.tex"
		latexmk -c "$file_name.tex"
	
	else
		# Use LaTeX
		latexmk "$file_name.tex"
	
		if [ "$?" = "127" ]
		then
			echo "It doesn't appear that latexmk is installed properly." 1>&2
			echo "Be sure you have a working LaTeX installation." 1>&2
			exit 1
		fi

		makeglossaries "$file_name"
		pdflatex "$file_name.tex"
		pdflatex "$file_name.tex"
		latexmk -c "$file_name.tex"

	fi
	
	shift
done
fi
