#!/usr/bin/perl

@directories = ( "./idl/valid", "./idl/invalid" );
$date        = localtime(time);
$on_error    = "continue";
$result      = "success";

# Name
print "\n>>>CORBA 2.2 IDL validator<<<\n-----------------------------\n\n";

# Check for parameters, if no parameters then take defaults
if( ! $ARGV[0] )
{
	$idlcompname = "orbit-idl";
	$idlcompparam = "--noskels --nostubs --nocommon";
}
else
{
	$idlcompname = $ARGV[0];
	if( $ARGV[1] )
	{
		$idlcompparam = $ARGV[1];
	}
}
print "Using compiler: $idlcompname \nwith parameter: $idlcompparam\n\n";

print ">>>$date: Starting tests ...";

for ($i=0; $i < @directories; $i++)
{
   $directory  = $directories[$i];
   open( FILES, "ls -1 $directory/*.idl |" )
      or die "Error: Can't execute ls -1 $directory/*.idl\n"; 

   print "\n\n-------------------------------------------\n";
   print ">>>Compiling files in $directory ...\n";
   
   while( $file = <FILES> )
   {
      chop( $file );
      print "-------------------------------------------\n";
      print ">>>Compiling $file ...\n";

      if( $file =~ /invalid/ )
      {
         $invalid = "invalid";
      }
      if( $file =~ /(.*)\.idl/ )
      {
         $filepath = $1;

         # Show the first line from the file
         open( FILE, "<$file" )
            or die ">>>ERROR: Can't open file $file !!!!\n\n";
         $line = <FILE>;
         print $line;
         close( FILE );
   
         # compile 
         system( "echo $date: $idlcompname $idlcompparam $file > $filepath.out" );
         $cmdresult = system( "$idlcompname $idlcompparam $file 2>$filepath.err >>$filepath.out" );
         if( $cmdresult != "0" )
         {
            if( $invalid =~ "invalid" )
            {
               print ">>>OK, idl-compiler should complain about invalid IDL ($cmdresult)!!!\n\n";
            }
            else
            {
               $result = "error";
               system( "cat $filepath.out" );
               system( "cat $filepath.err" );
               if( on_error != "continue" )
               {
                  exit $cmdresult;
               }
            }
         }
         else
         {
            if( $invalid =~ "invalid" )
            {
               $result = "error";
               print ">>>ERROR, no error reported although compiling invalid IDL!!!\n\n";
               system( "cat $file" );
               if(on_error != "continue")
               {
                  exit -1;
               }
            }
            else
            {
               print ">>>OK!!!\n\n";
            }
         }
      }
   }

   close( FILES );
}

if( $result != "error" )
{
   print "\n\nFinished, IDL-Compiler has passed the tests successfully !!!!\n\n";
}
else
{
   print "\n\nThere have been ERRORS during the compilation !!!!\n\n";
}
exit 0;


