=head1 DISCLAIMER and WARNING

This software is released into the public domain. The author assumes
no responsibility for any damage caused by its use. You are advised to
back up any and all files, especially the one you plan to use with
this program, before making use of it.

Using this program is an implicit agreement to not pursue legal action
against its author for any damage caused.

=cut

use strict;
use warnings;
use FileHandle;
use File::Basename;

my $infile = shift or die 'must supply infile' ;

my ($basename,$path,$suffix) = fileparse($infile, '.tex');
#warn  "($name,$path,$suffix)";

my $intex = $suffix ? $infile : "$infile.tex" ;

my $fh = FileHandle->new("< $intex") or die $!;

my @infile = <$fh> ;

my @out = qw(questions answersheet answers) ;

#warn @infile;

for my $out (@out) {
  my $outfile = "$basename-$out.tex";
  my $oh = FileHandle->new("> $outfile") or die $!;
  my @infile_copy = @infile;
  for my $line (@infile_copy) {

    $line =~ s!%%\s*stype\s*%%!$out!i ;    
    $oh->print($line);

  }
  $oh->close;

  { 
    my $syscmd = "pdflatex $outfile" ;
    warn $syscmd;
    print `$syscmd`;
  }
}

=head1 NAME

  gensheets.pl - program to generate question, answersheet and answers
                 for a urmathtest.sty LaTeX answers file

=head1 SYNOPSIS

  perl gensheets.pl TestTemplate

or

  perl gensheets.pl file-answers.tex

=head1 DESCRIPTION

urmathtest.sty is a LaTeX style file designed to help prepare 3 versions 
of the same document - a question sheet, an answersheet (to write
answers on), and an answers sheet (which has the questions and their
answers). Because you have to alter the source document each time you
want to produce one or the other, you might find this time-consuming.

So, the purpose of this Perl program is to allow you to create a
single template document and have the program generate all three
output documents.

=head1 USAGE

The way you use this program is you alter the usepackage statement
slightly. Normally your usage package statement looks like one of these
three: 

 \usepackage[questions]{urmathtest}[2001/05/12]
 \usepackage[answers]{urmathtest}[2001/05/12]
 \usepackage[answersheet]{urmathtest}[2001/05/12]

When you want this program to generate 3 different documents, the line
should look like this:

 \usepackage[answers]{urmathtest}[2001/05/12]

And then you simply run gensheets.pl as shown in the SYNOPSIS (either
way of calling it works) and you have your three output documents.

=head2 WHY THE CHOSEN usepackage LINE?

In my first development of the program, the usepackage line looked
like this:

 \usepackage[%% stype %%]{urmathtest}[2001/05/12]

but then I realized that people might want to run latex on a document
just to see how things work out without producing all 3. The most
comprehensive of the 3 documents is the C<answers> document. So
this is the one that C<gensheets> expected.

=head3 But then...

After using 

 \usepackage[answers]{urmathtest}[2001/05/12]

as the template line, I realized two things.

=over 4

=item * People (myself) tend to forget to generate all 3 files
consistently. That is, if you keep generating only one, you might
forget to keep all 3 synchronized with updates to the template
file. By forcing oneself to use a template file with C<%% stype %%> as
an option, you will get an error if you try to run LaTeX on the
file. Requiring one to always use <gensheets.pl> improves the
possibility of keeping your documents synchronized.

=item * After I read the docs and realized that there are many
more options (such as multipart) and that expecing the C<usepackage>
line to look like

  \usepackage[answers]{urmathtest}[2001/05/12]

prohibits others from using such options.

=head2 DEBUGGING a LaTeX problem

C<gensheets.pl> writes the files to disk before running LaTeX on them,
so you can always manually run one by hand after C<gensheets.pl> is
done with it, if you want to interactively debug a particular one. The
reason I say this is that I use Emacs' C<M-x compile> option and it is
not interactive, so when there is a problem with the file, I have to
go to the shell and run LaTeX manually.

=head1 NOTE

Even though your main/template file will be a file with both questions
and answers, it is best to name it with a neutral name such as 
"addition-test" and have C<gensheets.pl> create

=over 4

=item * addition-test-questions

=item * addition-test-answersheet

=item * addition-test-answers

=back


=head1 LINKS

=over 4

=item * http://www.math.rochester.edu/people/faculty/cmlr/Latex-exam-ur/

=item * http://hg.metaperl.com/misc

=back

=head1 AUTHOR

  Terrence Brannon <metaperl@gmail.com>
