#! /bin/bash
# Last edited on 2025-09-09 23:22:56 by stolfi

#==================================================
# Runs TeX with stolfi's Stanford format
usage="$0 [-nofilter] [-fontsize size] [-family fam] file"
#  where "size" is main font size in points (10, 11 or 12 )
#  and "fam" selects CMR fonts (cm) or some other family.
#  Default is -fontsize 11 -family cm
#==================================================

nofilter=0

flags=""
fontsize="11"
family="cm"

while [[ $# -gt 1 ]]; do
  if [[  "/$1" == "/-nofilter"  ]]; then
    nofilter=1
    shift
  elif [[ "/$1" == "/-fontsize" ]]; then
    if [[ ("/$2" != "/10") && ("/$2" != "/11") && ("/$2" != "/12")  ]]; then
      echo "$0: ** invalid point size = $2" 1>&2
      exit 1
    fi
    fontsize=$2
    shift; shift
  elif [[ "/$1" == "/-family" ]]; then
    if [[ ("/$2" != "/am") && ("/$2" != "/cm")  ]]; then
      echo "$0: ** invalid family = $2" 1>&2
      exit 1
    fi
    family=$2
    shift; shift
  else
    echo "Usage: ${usage}" 1>&2
    exit 1
  fi
done

if [[ $# -ne 1 ]]; then
  echo "Usage: ${usage}" 1>&2
  exit 1
fi

fname="$1"; shift

name=${fname#.*}

if [[ ( ! ( $?TEXINPUTS ) ) || ( "/$TEXINPUTS" == "/" )  ]]; then
  export TEXINPUTS=".:${STOLFIHOME}/tex/su/:${STOLFIHOME}/tex/su/::"
fi
echo "TEXINPUTS = $TEXINPUTS" 1>&2 

if [[  ! ( -s $name.tex ) ]]; then
  echo "${0}: ** cannot find $name.tex" 1>&2
  exit 1
fi
sudir=.
if [[ ${nofilter} -ne 0 ]]; then
  tex --fmt ${sudir}/${family}${fontsize}fmt.fmt $name  \
    | tex_error_filter.gawk \
  && ~/lib/thesis_update_tags $family $fontsize $name
else
  tex --fmt ${sudir}/${family}${fontsize}fmt.fmt $name \
  && ~/lib/thesis_update_tags $family $fontsize $name
fi

# END



