#!@WHICHPERL@ -w

use strict;
use warnings;

use Getopt::Long qw(:config posix_default);
use Pod::Usage;

=head1 NAME

texit - creates a ps file from a tex file.

=head1 SYNOPSIS

texit <tex file>

=cut

sub check {
  my ($prog) = @_;
  if ($? == -1) {
    # system writes out a message
  } elsif ($? & 127) {
    printf("$prog died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without');
  } else {
    printf("$prog exited with value %d\n", $? >> 8);
  }
  exit(1) if $? != 0;
}

sub main {
  # configuration
  my $help = 0; # FALSE
  GetOptions(
    "help|?" => \$help 
  ) or pod2usage(2);

  # display help
  pod2usage(1) if $help;

  my ($file) = @ARGV;
  pod2usage("Error: no input name.") unless (defined($file));

  # remove tex extension if it exists
  if ($file =~ m/(.*)\.tex/) {
    $file = $1;
  }
  pod2usage("Error: could not find tex file $file.tex") unless (-e "$file.tex");

  # run latex to create PDF
  #system('pdflatex', '-shell-escape', $file.'.tex');
  # run latex to create dvi
  system('latex', $file.'.tex');
  check('latex');

  # not sure why, but the original program echos the name so I do the same
  #print $file, "\n"

  # convert dvi to ps
  system('dvips', '-o', $file.'.ps', $file);
  check('dvips');

  #chmod(644, $file.'.ps');
  system('chmod', '644', $file.'.ps');
  check('chmod');

  # clean up
  unlink($file.'.dvi', $file.'.log');
}
main();
1;
