#!/usr/bin/perl

use strict ;
use FindBin qw($RealBin);
use lib "$RealBin/pm" ;
use Getopt::Long ;
use Pod::Usage ;
use Pod::Text ;
use File::Basename ;
use Cwd ;
use Cwd 'abs_path' ;
use mis_dep ;
use html ;
use cp_cache ;
use MIS ;
use gte ;
use trick_print ;
use trick_version ;

my $wd ;
my (@all_files , $src_dir , @c_files , @cpp_files ) ;
my $num_files ;
my $contents ;
my $curr_file ;
my $size ;
my ( @inc_paths , $subme ) ;
my @all_lines ;
my %sim ;
my $help ;
my ($vt_ref, $icg_dep_ref, $mod_ref, $md_ref, $rcs_tags_ref) ;

# override the print format for help message
*Pod::Text::seq_i = sub { return "[04m" . $_[1] . "[00m" } ;

# set stdout to unbuffered so we see printout immediately.
$| = 1 ;

# set the default verbose level to 2
$sim{args}{v} = 2 ;
$sim{args}{l} = 0 ;

Getopt::Long::Configure ("bundling");
GetOptions ( "debug|d|g" => sub { $sim{args}{v} = 3 } ,
             "help|h|?" => \$help ,
             "library|l" => \$sim{args}{l} ,
             "outfile|o=s" => \$sim{args}{o} ,
             "verbose|v=i" => \$sim{args}{v}
           ) or pod2usage(1) ;

pod2usage(1) if $help ;

if ( $sim{args}{o} ne "" ) {

    my ($version, $thread) = get_trick_version() ;
    $thread =~ s/\d+\.// ;
    local *OUTFILE ;

    open OUTFILE , ">$sim{args}{o}" 
         or warn "ICG cannot open $sim{args}{o} for writing\n" ;

    $sim{fh} = *OUTFILE ;

    print OUTFILE "Output for $0 version $version-$thread at " .  localtime() . "\n\n" ;
}

$wd = abs_path(cwd()) ;

#if ( $ARGV[0] eq "-l" ) {
if ( $sim{args}{l} ) {
    open FILE , "S_library_list" or die "Could not open S_library_list\n" ;
    @all_lines = <FILE> ;
    @c_files = grep /\.c$/ , @all_lines;
    chomp @c_files ;
    $wd = "" ;

} elsif ( $#ARGV eq -1 ) {

    opendir thisdir, $wd or die "Could not open the directory $wd";
    @all_files = grep !/^\.\.\./ , readdir thisdir;
    $src_dir = grep /^src$/ , @all_files;
    @c_files = grep /\.c$/ , @all_files;
    @cpp_files = grep /\.C$|\.cc$|\.cxx$|\.cpp$|\.c\+\+$/ , @all_files;
    $num_files = scalar @c_files ;
    if ( $num_files ne 0 and $src_dir eq 1 ) {
        print "\nMIS ERROR: All source files must reside in the\n" ;
        print "             'src' directory. Either place all \n" ;
        print "             source files in the 'src' directory, \n" ;
        print "             OR move any source file out of 'src'\n" ;
        print "             into $wd and remove the 'src' directory." ;
        print "\n\n";
        exit(1);
    }

    if ( $src_dir eq 1 ) {
        opendir thisdir, $wd . "/src";
        @all_files = grep !/^\.\.\./ , readdir thisdir;
        closedir thisdir;

        @c_files = grep /\.c$/ , @all_files;
        @cpp_files = grep /\.C$|\.cc$|\.cxx$|\.cpp$|\.c\+\+$/ , 
                          @all_files;
        $wd .= "/src" ;
    }

    foreach (@c_files , @cpp_files ) {
        $_ = "$wd/$_"  if ( /^[^\/]/ ) ;
    }

} else {

    @c_files = grep /\.c$/ , @ARGV;
    @cpp_files = grep /\.C$|\.cc$|\.cxx$|\.cpp$|\.c\+\+$/ , @ARGV;
    foreach (@c_files , @cpp_files ) {
            $_ = "$wd/$_"  if ( /^[^\/]/ ) ;
    }
    $wd = "" ;
}

my @non_yacc_files ;
foreach (@c_files) {
    (my $test = $_) =~ s/c$/y/ ;
    if ( ! -e $test ) {
            push @non_yacc_files , $_ ;
    }
}
@c_files = @non_yacc_files ;


if ($#c_files eq -1 and $#cpp_files eq -1 )  {
    trick_print($sim{fh}, 
                "No files specified or found\n" , 
                "normal_yellow" , $sim{args}{v});
    exit(0);
}

trick_print($sim{fh}, "MIS-ing...", "title_cyan" , $sim{args}{v}) ;
trick_print($sim{fh}, "\n" , "title_white" , $sim{args}{v}) if ( $sim{args}{v} != 1 ) ;

@inc_paths = $ENV{"TRICK_CFLAGS"} =~ /-I\s*(\S+)/g ;
push @inc_paths , ("$ENV{\"TRICK_HOME\"}/trick_source" , "../include") ;
foreach (@inc_paths) {
    $subme = quotemeta (abs_path(dirname($_)) . "/" . basename($_)) ;
}

($vt_ref, $icg_dep_ref, $mod_ref, $md_ref, $rcs_tags_ref ) = read_cache() ;
%{$sim{icg_types}} = %$vt_ref ;
%{$sim{all_icg_depends}} = %$icg_dep_ref ;
%{$sim{module_entries}} = %$mod_ref ;
%{$sim{all_mis_depends}} = %$md_ref ;
%{$sim{rcs_tags}} = %$rcs_tags_ref ;

push @c_files , @cpp_files ;
foreach $curr_file (@c_files ) {

    open FILE, $curr_file or die "Couldn't find file: $curr_file\n";
    seek FILE , 0 , 2 ;
    $size = tell FILE ;
    seek FILE , 0 , 0 ;
    read FILE , $contents , $size;

    mis_c($curr_file , $contents, \%sim) ; 
}

#if ( $sim{args}{l} == 0 ) {
#    write_local_cat( $wd , \@inc_paths , \%sim ) ;
#}

if ( $sim{args}{v} == 1 ) {
    print " [32mComplete[00m\n" ;
}
trick_print($sim{fh}, "\nAll MIS complete\n\n" , "normal_green" , $sim{args}{v}) ;

__END__
   
=head1 NAME
   
MIS - Trick Module Interface Specification
   
=head1 SYNOPSIS

MIS [--debug] [-d] [-g] [-h] [--help] [-l] [--library] 
[-o I<outfile>] [--outfile=I<outfile>] 
[-v I<level>] [--verbose=I<level>] [B<file> ...]

=head1 DESCRIPTION

See the Trick User's guide for B<MIS>.
   
=head1 OPTIONS
   
=over 8

=item B<-d> | B<-g> | B<--debug>

Set verbose level = 3

=item B<-h> | B<-?> | B<--help>

This help screen

=item B<-l> | B<--library>

Use this option in a SIM directory.  MIS will execute on all files listed in the
S_library_list file

=item B<-o> I<outfile> | B<--outfile>=I<outfile>

Direct output of MIS to outfile. ( Note: Does not affect screen output ).

=item B<-v> I<level> | B<--verbose>=I<level>

et the verbose level.  Valid entries are 0-3.

=back

=cut

