#!/bin/sh

# styles 4e documents with latex.
# input is file(s) to put on page.

# suggested usage: ./4e2latex.sh file1.4e file2.4e > encounter.tex && pdflatex encounter.tex

tex_head() {
	cat <<-endHead
		%\documentclass{article}
		\documentclass[8pt]{extarticle}
		\usepackage{color}
		\usepackage{fullpage}
		\usepackage{multicol}

		\addtolength{\oddsidemargin}{-.475in}
		\addtolength{\evensidemargin}{-.475in}
		\addtolength{\textwidth}{0.75in}
		\addtolength{\topmargin}{-.475in}
		\addtolength{\textheight}{1.75in}

		\parindent0em
		\setlength{\parindent}{0pt} 

		\definecolor{keywords}{RGB}{100,50,00}
		\definecolor{atwill}{RGB}{000,100,050}
		\definecolor{recharge}{RGB}{125,000,125}
		\definecolor{encounter}{RGB}{150,000,000}

		\begin{document}
		\begin{multicols}{2}


	endHead
}

tex_foot() {
	cat <<-endFoot
		\end{multicols}
		\end{document}
	endFoot
}

texify() {
	TMP=$(mktemp)
	cat $1 > $TMP

	_MINIPAGE='\\\begin{minipage}[t]{0.48\\textwidth}'

	KEYWORDS="\(Speed\|Level\|XP\|Initiative\|HP\|AC\|Speed\|Fortitude\|Reflex\|Will\|Reach\|dominated\|stunned\|weakened\|dazed\|slowed\|Senses\|darkvision\|Resist\|Saving\|Throws\|fly\|teleport\|Action\|Points\|prone\|Close\|burst\|blast\|Petrified\|Marked\|Unconscious\|Surprised\|Immobilized\|Helpless\|Deafened\|Blinded\|Restrained\)"
	KEYWORDS_HI="\\\textcolor\{keywords\}\{\1}"

	ENCOUNTER="\(^.*\(.*[^\/]encounter.*\).*$\)" # should only appear in parens.  not sure if that's true.
	ENCOUNTER_HI="\\\colorbox\{encounter\}{$_MINIPAGE\\\textcolor\{white\}\{\1\}\\\end{minipage}}"

	ATWILL="\(^.*at-will.*$\)"
	ATWILL_HI="\\\colorbox\{atwill\}{$_MINIPAGE\\\textcolor\{white\}\{\1\}\\\end{minipage}}"

	RECHARGE="\(^.*recharges\? .*$\)"
	RECHARGE_HI="\\\colorbox\{recharge\}{$_MINIPAGE\\\textcolor\{white\}\{\1\}\\\end{minipage}}"

	ACTION="\(standard\|move\|minor\|free\)"
	ACTION_HI="\\\underline\{\1\}"

	TAILFLUFF="^\(Alignment\|Skills\|Str\|Con\|Published\).*$"
	TAILFLUFF_HI=""

	XP_UNDERLINE="\(.*XP.*\)$"
	XP_UNDERLINE_HI="\\\underline\{\1\} "

	FIRSTLINE="^.*$" # 1s/THIS/THAT replaces on first line
	FIRSTLINE_HI="\\\paragraph\{\1\}\n"

	sed -i "s/$/\n/"  $TMP
	sed -i "s/'//g" $TMP
	sed -i "s/\($ENCOUNTER\)/$ENCOUNTER_HI/g" $TMP
	sed -i "s/\($RECHARGE\)/$RECHARGE_HI/g" $TMP
	sed -i "s/\($ATWILL\)/$ATWILL_HI/g" $TMP
	sed -i "s/\($ACTION\)/$ACTION_HI/g" $TMP
	sed -i "s/\($ACTION_NEWLINE\)/$ACTION_NEWLINE_HI/g" $TMP
	sed -i "s/\($XP_UNDERLINE\)/$XP_UNDERLINE_HI/g" $TMP
	sed -i "1s/\($FIRSTLINE\)/$FIRSTLINE_HI\\\\/" $TMP
	sed -i "s/\($KEYWORDS\)/$KEYWORDS_HI/g" $TMP
	sed -i "s/\($TAILFLUFF\)/$TAILFLUFF_HI/m" $TMP 
	cat $TMP

	rm $TMP
}

tex_head

while [ "$#" -gt 0 ]
do
	texify $1
	shift
done

tex_foot

#   this script should take multiple enemies as args and put them on a single page.


