#! /bin/bash
echo "Syntax Checking Latex Documents"
if bash -c 'for i in */*.tex; do lacheck $i; done' | grep -v 'possible unwanted space\|moneychange\|commaform\|[A-Z][A-Z]\.'
then
  echo "Failed Syntax Check"
  echo "Exiting"
  exit 1
fi

comment='\(^\|[^\\]\)%.*'
if grep -o '\\[a-z][A-Z]\w*[0-9]\w*' */*.tex
then
  echo "Gametex macro containes a number"
  echo "Exiting"
  exit 1
fi

macros=$(grep -Hn -o '\('"$comment"'\)\?\\NEW{.*}{\\.*}' Lists/*.tex | sed 's/\\NEW{.*}{\|}//g' | grep -v '%' |  sort)
nobrace=$(grep -Hn -o '\(\\update.*{\|'"$comment"'\)\?\\[a-z][a-z]\?[A-Z]\w*\b\([^{]\|$\)' */*.tex | grep -v '\\update.*{\|%' | grep -o '.*\\[a-z][a-z]\?[A-Z]\w*\b' | sort)
DIFF=$(diff  <(echo "$macros" ) <(echo "$nobrace"))
if [ "$DIFF" != "" ]
then
  echo "$DIFF"
  echo "GameTeX Macros missing braces"
  echo "Exiting"
  exit 1
fi
