#!/bin/csh -f

# Usage: makesoln fileroot

# You can actually include an extension in fileroot, but you get the
# same behavior from "makesoln foo.tex" or "makesoln foo.dvi" as from
# "makesoln foo".

# This command creates a slightly modified version of fileroot.tex on
# fileroot-soln.tex by changing "\solutionsfalse" to "\solutionstrue".
# It also extracts the "if" portion of an \ifsolutions construct that
# has \else and \fi on the same line so as to set up handout numbers
# correctly for lastho.

umask 66

set tempfile = /tmp/makesoln.$user.$$

set inputbase = $1:r

sed 's+\\solutionsfalse+\\solutionstrue+\
     s+\\solutionsnull++\
     s+\\ifsolutions\([^a-zA-Z].*\)\\else[^a-zA-Z].*\\fi+\1+' $inputbase.tex \
  > $tempfile

diff $tempfile $inputbase.tex > /dev/null
if ($status == 1) then
  del . ${inputbase}-soln.tex
  echo "makesoln: Creating new ${inputbase}-soln.tex"
  mv $tempfile ${inputbase}-soln.tex
else
  echo "makesoln: Could not convert ${inputbase}.tex to ${inputbase}-soln.tex"
  rm -f $tempfile
endif
