#!/usr/bin/env perl
#******************************************************************************
# MPIA - MIDI project
#
# "@(#) $Id: $"
#
# who        when       what
# ---------- ---------- ----------------------------------------------
# mathar     2003-08-05 created
#********************************************************************
#   NAME
#   mioInclude.pl - recursive expansion of cpp include directives
#
#   SYNOPSIS
#   mioInclude.pl incdir [incdir ...] file
#
#   DESCRIPTION
#   The perl script searches for the readable "file" in each of
#   the "incdir" directories (priority of the search given to the
#   leftmost fit). It recognizes three types of lines in this file:
#   - lines that start with white space followed by a double slash
#     (akin a comment in C++) are skipped
#   - lines that start with the hash (#) followed by white space and
#     the word include and a string in double quotes are supposed
#     to indicate some file in one of the "incdir" directories
#     and are expanded at this place by recursive call to
#     "mioInclude.pl"... this mimics the cpp(1) file inclusion mechanism.
#   - all other lines are simply copied to the standard output.
#
#   This means the functionality is similar to
#   cpp -P -x c++ -w -nostdinc -I incdir [-I incdir ...] file
#   but without any processing of #if..#else or #define-lines
#   and without any complaints on single quotes in lines as cpp would do.
#
#   EXAMPLES
#   mioInclude.pl ../CDT $INTROOT/CDT $VLTROOT/CDT osbControl.cdt
#
#   CAUTIONS
#   There is no default file search path. You must have at least one
#   argument, even if it is the dot to indicate the current working directory.
#
#   SEE ALSO
#   cpp(1) unifdef(1) perl(1) chkcCDTLoadProcess(3)
#
#---------------------------------------------------------

use strict ;
use FileHandle ;
sub expand
{
	sub expand ;
	my $argc = @_ ;
	my $fil = @_[$argc - 1] ; pop(@_) ;
	my $found = 0 ;		# not yet found the file
	my $optind = 0 ;	# index to the list of search directories
	while ( ! $found && $optind < $argc - 1 )
	{
		if ( -r @_[$optind] . "/" . $fil ) 	# if this file exists in the UNIX directory
		{
			$fil = @_[$optind] . "/" . $fil  ;
			$found =1 ;
			last ;
		}
		else
		{
			$optind++ ;
		}
	}
	if ( ! $found )
	{
		die "Cannot read or find $fil" ;
	}

	my $line ;
	my $F = new FileHandle("$fil") ;
	while ($line = <$F>)
	{
		if ( $line =~ m"^[\s]*//.*$" )	# skip those C++ style comment lines
		{
			# print "COMMENT " . $line . "\n" ;
		}
		elsif ( $line =~ m"#[\s]*include \"(.*)\"" )	 # work recursively through "include" files
		{
			my @incdir = @_[$argc - 1] ;
			print "\n// include $1\n" ;
			expand @_,$1 ;
			print "\n// __oOo__  $1\n" ;
		}
		else
		{
			print $line ;
		}
	}
	# print "EOF " . $fil . "\n" ;
}

my $argc = @ARGV ;	# number of command line arguments
my $f = $ARGV[$argc - 1] ;	# last command line argument is the file

print "// This file has been automatically produced. DO NOT EDIT THIS FILE!!!!!!\n" ;
print "// Input file: $f\n" ;
# print "// Formatter: $Id:$\n" ;
print "// Created: " . `date` . "\n" ;

expand @ARGV ;

# __oOo__

