#!/bin/sh                      -- # -*- perl -*- 
eval 'exec perl  -S $0 "$@"' 
 if 0;
#!/bin/perl
#                              -*- Mode: Perl -*- 
# mkinc -- 
# ITIID           : $ITI$ $Header $__Header$
# Author          : Ulrich Pfeifer
# Created On      : Wed Jun 12 12:15:39 1996
# Last Modified By: Ulrich Pfeifer
# Last Modified On: Mon Jul  1 17:00:43 1996
# Language        : CPerl
# Update Count    : 30
# Status          : Unknown, Use with caution!
# 
# (C) Copyright 1996, Universitt Dortmund, all rights reserved.
# 
# $Locker:  $
# $Log: mkinc,v $
# Revision 2.3  1997/02/06 09:31:08  pfeifer
# Switched to CVS
#
# Revision 2.2  1996/08/19 17:15:20  pfeifer
# perl5.003
#
# Revision 2.1.1.1  1996/07/16 16:40:25  pfeifer
# patch10: Script for building wais.h from the freeWAIS-sf source
# patch10: directory.
#
# 

use FileHandle;
use File::Basename;
use Cwd;

while ($ARGV[0] =~ /^-/) {
  if ($ARGV[0] =~ s/^-I//) {
    push @opt_I, $ARGV[0];
    shift;
  } else {
    warn "$0: Unknown option $ARGV[0]\n";
    shift;
  }
}

print <<"EOH"
/*                               -*- Mode: C -*- 
 * wais.h -- generated by $0
 * Author          : Ulrich Pfeifer
 * 
 * (C) Copyright 1996, Universitt Dortmund, all rights reserved.
 * 
 */
#ifndef WAIS_H
#define WAIS_H
EOH
  ;

my $cwd = getcwd;
for (@ARGV) {
  my ($name,$path) = fileparse($_);
  chdir($path) or "Dir could not chdir to $path: $!\n";
  #print STDERR "processing ($name,$path)\n";
  do_file($name);
  chdir($cwd);
}

print <<"EOT"
#endif /* WAIS_H */
EOT
  ;

exit(0);

sub search {
  my $file = shift;
  my $std  = shift;
  my @inc  = ($std)?@opt_I:('.', @opt_I);
  for (@inc) {
    my $path = "$_/$file";
    return $path if -f $path;
  }
}

sub do_file {
  my $file = shift;
  my $std  = shift;
  my $path = search($file, $std);

  return unless $path;
  $path =~ s:/./:/:g;
  return if $SEEN{$path}++;
  print STDERR "Processing $path ...\n";
  # print "/* --- $path --- */\n";
  my $fh   = new FileHandle "< $path";

 if (defined $fh) {
    while (<$fh>) {
      if (s/^\#\s*include "(.*?)"//) {
        print;
        do_file($1);
      } elsif (s/^\#\s*include <(.*?)>//) {
        print;
        do_file($1,1);
      } else {
        print;
      }
    }
    $fh->close;
  }
  1;
}

