: # *-*-perl-*-*
    eval 'exec perl -S  $0 "$@"'
    if $running_under_some_shell;  

# Rewrite of texexpand.  Handles style files and \endinput at least
# approximately right.

# Copyright Robert Thau, MIT AI lab, 1993.
# Unlimited reproduction is permitted as long as this notice is preserved.

@texinputs = (".");
foreach $dir (split(/:/,$ENV{'TEXE_INPUTS'})) { push (@texinputs, $dir); }
foreach $dir (split(/:/,$ENV{'TEXINPUTS'})) { push (@texinputs, $dir); }

@dont_include = split(/:/,$ENV{'TEXE_DONT_INCLUDE'});

while ($ARGV[0] =~ /^-/ && (! ($ARGV[0] =~ /^--$/)))
{
  $_ = shift;
  if (/^-dont_include$/) { push(@dont_include, shift); }
  elsif (/^-w$/) {}                     ### Compatibility hack.
  elsif (/^-debug$/) { $debug++; }
  else { warn "% --- WARNING:  Unrecognized option: $_ \n"; }
}

if ($debug)
{
  print "%TeX inputs are in:\n";
  foreach $dir (@texinputs) { print "%--- $dir\n"; }

  print "\n%Special names (not to be input or included):\n";
  foreach $name (@dont_include) { print "%--- $name\n"; }

  print "\n%Args:\n";
  foreach $arg (@ARGV) { print "%--- $arg\n"; }

  print "\n";
}

while (<>)
{
  if (/^\s*\\(input|include)\W/)
  { 
    print ("%Found include at top-level: $_") if $debug;
    &include_line ($_); 
  }
  elsif (/(\\documentstyle\s*\[)(.*)(\]\s*\{)/)
  {
    local ($before, $styles, $after) = ($`.$1, $2, $3.$');
    local (@styles, @print_styles, @inc_styles);

    @styles = split(/,/, $styles);

    print "%Found styles: --- $styles \n" if $debug;

    foreach $style (@styles)
    {
      if (&should_include ($style . ".sty")) { push (@inc_styles, $style); }
      else { push (@print_styles, $style); }
    }

    print $before . (join (',', @print_styles)) . $after;

    foreach $style (@inc_styles) { &do_include ($style . ".sty"); }
  }
  else
  {
    print;
  }
}

sub include_line
{
    local ($_) = @_;
    local ($filename, $found);

    if (/\\input\s*\{([^}]*)\}/) { $filename = $1 }
    elsif (/\\input\s+(\S*)\s/) { $filename = $1 }
    elsif (/\\include\s*\{([^}]*)\}/) { $filename = $1 }
    else { warn "texexpand --- COULDN'T FIND FILENAME in $_" }

    if ($filename && &should_include($filename)) { &do_include ($filename) }
    else { print; }
}

sub do_include
{
  local ($filename) = @_;
  local ($fname);

  $filename .= ".tex" if ($filename !~ /\.[^\/]*$/);
  print "%--- including $filename:  $_\n" if $debug;;

  $found = 0;

  if ($filename =~ "^/")
  {
    $fname = $filename; $found = 1;
  }
  else
  {
    foreach $dir (@texinputs)
    {
      $fname = join ('/', $dir, $filename);
      print "%--- checking for $fname\n" if $debug;

      if (-f $fname) { $found = 1; last; }
    }
  }

  if (! $found)
  {
    warn "texexpand --- COULDN'T FIND $filename!!!\n"
  }
  else
  {
    local (*incfile, $_);

    if (!open (incfile, $fname))
    {
      warn "texexpand --- COULDN'T OPEN $fname --- $! !!!\n"
    }
    else
    {
      while (<incfile>)
      {
	last if /\\endinput/;
	
	if (/^\s*\\(input|include)\W/) { &include_line ($_); }
	else { print; }
      }

      close (incfile);
    }
  }
}

sub should_include
{
  local ($_) = @_;

  # Recognize standard style options ...

  print "% --- Checking $_ for standard style\n" if $debug;

  if (/^(twoside|twocolumn|titlepage|openbib|leqno|fleqn|\d+pt)\.sty$/)
  { return 0; }

  s/.*\///; s/\..*//;          ### Get base name ...

  print "% --- Checking $_ for special style\n" if $debug;

  local ($name) = $_;
  return (!grep (/^$name$/, @dont_include));
}

