#!/usr/bin/perl

require 5.002;
use strict;
use FileHandle;
use Math::BigInt;

#use bignum;

#
# ITA include tree analyser
#
# Written by Vicente Esteve LLoret <viesllo@inf.upv.es>
# Copyright (C) Aug, 2003 OCERA Consortium.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
#

################################################################################
# FUNCTION   :  use_script
# ABSTRACT   :  displays error message in case of wrong parameters
# PARAMETERS :  
# TODO       :
# COMMENTS   :  
################################################################################
sub use_script
{
  print "Use:\n";
  print "  ita.pl file linux_include_dir\n";
  exit;
}

################################################################################
# GLOBAL VARIABLES

# PARAMETERS: 
# object file
# inputfile: file containing functions to be studied

my $filetoanalyze = shift || use_script;  
my $includedir = shift || use_script;  
my $nesting    = 0;
my @includedirectories;


###############################################################################
# FUNCTION   :  Indent_Line
# ABSTRACT   :  
# PARAMETERS :  
# TODO       :
# COMMENTS   :             
################################################################################
sub Indent_Line
{
 my $i;
 for ($i = 1; $i<$nesting;$i++) {
	 print "-"
 };
 print ">";
}

###############################################################################
# FUNCTION   :  Get_Include_Dir
# ABSTRACT   :  
# PARAMETERS :  
# TODO       :
# COMMENTS   :             
################################################################################
sub Get_Include_Dir
{
 my $path;
 my $includefile = $_[0];
 my $posiblepath;
 foreach $posiblepath (@includedirectories) {
	 if ((-l $posiblepath.$includefile)||(-e $posiblepath.$includefile)) {
	   return $posiblepath;
	 };
 };
 $path="./include/";
 return $path; 
}

###############################################################################
# FUNCTION   :  Analyze_File
# ABSTRACT   :  
# PARAMETERS :  
# TODO       :
# COMMENTS   :             
################################################################################
sub Analyze_Includes
{
 my $file = $_[0];
 my @words;
 my @includes;
 
 open(RES,"cat $file | ");
 while (<RES>)
 {
  chomp;
  @words=split(/\s+/);
  if ($words[0] eq "#include") {
          $_=@words[1];
	  if (/<.*/) { 
            s/.(.*)./$1/g;
            push(@includes,$_);
          };
  };
 }
 
 close(RES);
 
 $nesting = $nesting + 1;
 
 while (@includes!=0) 
 {
          $_=shift(@includes);
	  Indent_Line();
	  print $_;
	  print  "\n";
	  
	  Analyze_Includes(Get_Include_Dir($_).$_);
 }
 
 $nesting = $nesting - 1;
}




################################################################################
# FUNCTION   :  main
# ABSTRACT   :  calls to other subs and generates output 
# PARAMETERS :  
# TODO       :
# COMMENTS   :             
################################################################################


 push(@includedirectories,"./include/");
 push(@includedirectories,"./include/compat/");
 push(@includedirectories,"./include/posix/");
 push(@includedirectories,$includedir);

# open(HANDLER,"find . -name *.c |");

  
# while (<HANDLER>)
# {
  print "\n";
  print "FILE ",$filetoanalyze,"\n";	 
  Analyze_Includes($filetoanalyze);	 
# }

# close(HANDLER);



exit;

