#!/import/misc/bin/perl

#  Copyright (c) 1991, 1992, 1993 Xerox Corporation.  All Rights Reserved.  
#  
#  Unlimited use, reproduction, and distribution of this software is
#  permitted.  Any copy of this software must include both the above
#  copyright notice of Xerox Corporation and this paragraph.  Any
#  distribution of this software must comply with all applicable United
#  States export control laws.  This software is made available AS IS,
#  and XEROX CORPORATION DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
#  INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS FOR A PARTICULAR PURPOSE, AND NOTWITHSTANDING ANY OTHER
#  PROVISION CONTAINED HEREIN, ANY LIABILITY FOR DAMAGES RESULTING FROM
#  THE SOFTWARE OR ITS USE IS EXPRESSLY DISCLAIMED, WHETHER ARISING IN
#  CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, EVEN IF
#  XEROX CORPORATION IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#  
#  $Id: tim.dist,v 1.6 1994/04/15 02:34:36 janssen Exp $

sub random_filename {
  join ('', time, int(rand(65536)), getpgrp);
}

sub tim2texinfo {

  local($input, $inputfh) = @_;
  local($intermediate_filename, $included_filename);

  $inputfh++;
  unless (open ($inputfh, $input)) {
    print STDERR "Can't open input file $input.\n";
    return 1;
  };

  if ($level == 0)
    { print STDOUT "\\input texinfo @c -*-texinfo-*-\n"; };
  while ( $_ = <$inputfh> ) {
    s/\@\@/hIgHlYuNlIkElYjUnK/g;
    s/\@var/\@code/g;
    s/\@metavar/\@var/g;
    s/\@C{/\@code{/g;
    s/\@C\+\+{/\@code{/g;
    s/\@command/\@code/g;
    s/\@constant/\@code/g;
    s/\@codeexample/\@example/g;
    s/\@end codeexample/\@end example/g;
    s/^\@document\s*\{([^\}]*)\}\s*\{([^\}]*)}\s*\{([^\}]*)}\s*$/\@setfilename $3\n\@settitle $1\n\@ifclear largerdoc\n\@titlepage\n\@title $1\n\@author $2\n\@sp\n\@today\{\}\n\@sp\nCopyright \@copyright\{\} 1993 Xerox Corporation\@*\nAll Rights Reserved.\n\@end titlepage\n\@ifinfo\n\@node Top, ,(dir),(dir)\n\@top $1\n\@end ifinfo\n\@end ifclear\n\n/;
    s/\@cl{/@code{/g;
    s/\@class/\@code/g;
    s/\@exception/\@code/g;
    s/\@fn/\@code/g;
    s/\@interface/\@code/g;
    s/\@isl/\@code/g;
    s/\@kwd/\@code/g;
    s/\@language/\@b/g;
    s/\@m3{/\@code{/g;
    s/\@macro/\@code/g;
    s/\@message/\@code/g;
    s/\@method/\@code/g;
    s/\@module/\@code/g;
    s/\@parm/\@var/g;
    s/\@program/\@code/g;
    s/\@protocol/\@code/g;
    s/\@system/\@b/g;
    s/\@transcript/\@example/g;
    s/\@transport/\@code/g;
    s/\@end transcript/\@end example/g;
    s/\@type/\@code/g;
    s/\@userinput/\@t/g;
    s/hIgHlYuNlIkElYjUnK/\@\@/g;
    if ( /^\@include (.*).tim/ && ( $filename = $1 )) {
      print STDERR "Including $filename\n";
      $level++;
      &tim2texinfo ("$filename.tim", $inputfh);
      $level--;
    }
    else { print STDOUT ( $_ ); };
  };
  if ($level == 0) { print STDOUT "\@bye\n"; };
  return (0);
};

sub usage {
  print STDERR "Usage:  tim -x INPUTFILE.tim >OUTPUTFILE.texinfo\n";
  print STDERR "   or   tim -i INPUTFILE.tim >OUTPUTFILE.info\n";
  print STDERR "   or   tim -t INPUTFILE.tim >OUTPUTFILE.ps\n";
  print STDERR "   or   tim -t4050 INPUTFILE.tim >OUTPUTFILE.4050ps\n";
};

if ($#ARGV < 1) { &usage; exit 1; };

$processing_opt = $ARGV[0];
shift;

foreach $arg (@ARGV)
{
  open (SAVEOUT, ">&STDOUT");
  $intermediate_file = join ('-', "tim", &random_filename);
  if (!(open (STDOUT, ">$intermediate_file"))) {
    print STDERR "Can't open \"$intermediate_file\" for stdout.\n";
    exit 1;
  }
  $level = 0;
  if (&tim2texinfo($arg, "ifh00") == 0)
    {
      close (STDOUT);
      open(STDOUT, ">&SAVEOUT");      
      if ($processing_opt eq "-x" && open (INTERMEDIATE, $intermediate_file)) {
	while ( <INTERMEDIATE> ) { print; };
	close (INTERMEDIATE);
	unlink $intermediate_file;
      } elsif ($processing_opt eq "-t" && (! (-z $intermediate_file))) {
	open (INTERMEDIATE, "texinfo2ps $intermediate_file |");
	while ( <INTERMEDIATE> ) { print; };
	close (INTERMEDIATE);
	unlink $intermediate_file;
      } elsif ($processing_opt eq "-t4050" && (! (-z $intermediate_file))) {
	open (INTERMEDIATE, "texinfo24050ps $intermediate_file |");
	while ( <INTERMEDIATE> ) { print; };
	close (INTERMEDIATE);
	unlink $intermediate_file;
      } elsif ($processing_opt eq "-i" && (! (-z $intermediate_file))) {
	open (INTERMEDIATE, "texinfo2info $intermediate_file |");
	while ( <INTERMEDIATE> ) { print; };
	close (INTERMEDIATE);
	unlink $intermediate_file;
      } else {
	&usage;
	unlink $intermediate_file;
	exit 1;
      }
      exit 0;
    }
  else
    {
      print STDERR "Error processing file \"$arg\".\n";
      exit 1;
    }
};
