#!/bin/csh -f

# Usage: latcs fileroot [key]

# Fileroot is the given file name or the part preceding ".tex.e" or
# ".tex" where applicable.  So you get the same behavior from "latcs
# foo" or "latcs foo.tex.e" or "latcs foo.tex".

# The optional crypt key is prompted for if omitted on the command line..

# This command creates a slightly modified version of fileroot.tex.e
# on filerootsoln.tex.e by changing "\solutionsfalse" to
# "\solutionstrue".  Then it invokes latc on the modified .tex.e file.

umask 66

set inputbase = `echo $1 | sed 's+\.e$++' | sed 's+\.tex$++'`

shift
set key = "$*"

if ("$key" == "") then
  echo "Enter key:"
  set key = "$<"
endif

del . ${inputbase}soln.tex.e

crypt "$key" <$inputbase.tex.e \
  | sed 's+\\solutionsfalse+\\solutionstrue+\
         s+\\ifsolutions\([^a-zA-Z].*\)\\else[^a-zA-Z].*\\fi+\1+' \
  | crypt "$key"\
  > ${inputbase}soln.tex.e

latc ${inputbase}soln "$key"
