#                              -*- Mode: Perl -*- 
# One-FIle.pm --- create a single tex file from their input files.
# Author          : John Verzani
# Created On      : Fri Jan  9 06:40:14 1998
# Last Modified By: John Verzani
# Last Modified On: Sat Feb 14 10:58:09 1998
# Update Count    : 72
# Status          : Unknown, Use with caution!
# 

# History 		
# 9-Jan-1998		John Verzani	
#    NEED to handle graphics in some manner. for latex2e there is a way

# 9-Jan-1998		John Verzani	
#    created. Inspired by K.P. Harts script but need recursion.

package       One_File;
require       Exporter;
@ISA        = qw(Exporter);
@EXPORT     = qw(one_file);
@EXPORT_OK  = qw();

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 TODO

=cut


use FileHandle;
use Carp;

####### testing
$main = "tutorial.tex";
$target = "one-file.tex";
($ret,$msg,$file) = one_file($main,$target);
print "ret,$msg,$file\n";

my $filename;
my $target;
my $main;


sub one_file {
  ### make a single tex file from a master file
  ### returns 
  ###   exit code 0 or 1
  ###   exit meassage
  ###   target filename. need to specigy the directory name

  ($main,$target) = @_;

  $debug = 0;
  $padding = "";		# for format of output
  ### find base, and filename
  $main =~ /(.*)\/(.*)/g;
  $base = $1 || ".";		# no trailing / global variable
##  $file = $2 || $main;

  unless (-e $main || -e $main.".tex") {
    return (0,"give me something to work with. I can't fine $main");
  } 
  unless ($target) {
    #make main the target.
    if (-e "$main.old") {
      unlink "$main.old";
    }
    link($main,$main.".old") ||carp "can't link $main with $main.old\n";
    $target = $main;
    unless ($main =~/\.tex$/) {
      $target .= ".tex";	# add in tex ending
    }
    $main =$main.".old";
  }
  ### rename old target
  if (-e $target) {
    rename ($target,$target."~");
  }

  $OUTFILE = new FileHandle ">$target" || 
    return (0,"couldn't open $target");

  print "****** making one file for $main. one-file is $target******\n" if $debug;

  $date = `date +%D`;

  if ($debug) {
    $OUTFILE->print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
    $OUTFILE->print("% $target\n");
    $OUTFILE->print("% generated by one-file.pl\n");
    $OUTFILE->print("% from $main on $date");
    $OUTFILE->print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n");
  }
   
  $padding = output_file($OUTFILE,$main,$padding);

  close($OUTFILE);
  ### end it
  return(1,"one-file.pl is complete",$target);
}


sub output_file {
  ($OUTFILE,$filename,$padding) = @_;
  
  ### first find the filename
  unless (-e $filename) {
    if (-e $filename.".tex") {
      $filename .= ".tex";
    } elsif (-e $base."/".$filename) {
      $filename = $base."/".$filename;
    } elsif (-e $base."/".$filename.".tex") {
      $filename = $base."/".$filename.".tex";
    } else {
      carp "can't find $filename anywhere\n";
    }
  }

  print "including $padding$filename.........\n" if $debug;
  
  ##adjust $padding
  $padding .= "  ";

  my $INFILE = new FileHandle "< $filename";

  while (<$INFILE>) {
    ### skip over comments
    if (/^\s*%/) {
      $OUTFILE->print($_);
      next;    
    } elsif (/%%% Local Variables/ ||
	/%%% mode:/ ||
	/%%% TeX-master/ ||
	/%%% End/) {
      ### skip over emacs/auctex stuff
      next;    ### catch various lines
    } elsif (/\\input\s+(\S+)/ || /\\include\s+(\S+)/) {
      $include_file = $1;
      ### check that it isn't a common file
      if ($include_file =~ /amstex/ ||
	  $include_file =~ /xypic/ ||
	  $include_file =~ /amsppt/ ||
	  $include_file =~ /diagrams.tex/) {
	$OUTFILE->print($_);
	next;
      }


      $padding = output_file($OUTFILE,$include_file,$padding);
      $padding =~ s/\s\s//;
    } elsif (/(\\includegraphics\[.*\])\{(.*)\}(.*)/) {
      ### include graphics command
      $graphics_file = $2;
      $latex_graphics_command = $1;
      $rest_of_line=$3;
    
      &move_graphics_file($graphics_file,$latex_graphics_command,$rest_of_line);
  } elsif (/(\\epsfig)\{file\s*=\s*(.*)\}(.*)/) {
    $graphics_file = $2;
    $latex_graphics_command = $1;
    $rest_of_line=$3;
    &move_graphics_file($graphics_file,$latex_graphics_command,$rest_of_line);
    
  } else {
      $OUTFILE->print($_);
    }


  }
  return $padding;
}

sub move_graphics_file {
  ($graphics_file,$latex_graphics_command,$rest_of_line)=@_;
  
  $fig_ext = "";		# could use ".fig", ".img" etc
  ### rename the file:
  $graph_ctr++;
  ## strip off $tex ending on  target
  $target_base = $target;
  $target_base =~ s/\.tex$//;
  print "making directory /$target_base$fig_ext\n" 
    || carp "can't make $target_base$fig_ext\n";;
###  mkdir($base."/$target_base$fig_ext",0777);
  mkdir("$target_base$fig_ext",0777); # $target contains  its own info
  $graphics_file =~ /(.*)\.(.*)/g;
  $graphics_filename_prefix = $1;
  $extension = $2;

  if (-e "$base/$graphics_file") {
    $graphics_file = "$base/$graphics_file";
  }

  print "graph_file is $graphics_file, base=$base\n";
  
  if (-e $graphics_file) {
    print "linking $graphics_file and $target_base$fig_ext/$graph_ctr.$extension\n";
###    link ($graphics_file,$base."$target_base$fig_ext/".$graph_ctr.".$extension");
##    link ($graphics_file,"$target_base$fig_ext/$graph_ctr.$extension") ||
    system ("cp $graphics_file $target_base$fig_ext/$graph_ctr.$extension");
  }
  ## need to make refernce in file relative to target-s name.
  @temp = split(/\//,$target_base);
  $target_name = pop @temp;
  $OUTFILE->print($latex_graphics_command);
  $OUTFILE->print("{$target_name$fig_ext/$graph_ctr.$extension}");
  $OUTFILE->print("$rest_of_line\n"); # include  newline
}

1; ## this is required
