#!/usr/bin/perl
#
#  - - - - - - -
#   s o f a _ c
#  - - - - - - -
#
#  Insert license statements into SOFA C code.
#
#  Command:
#
#    sofa_c.pl  internal  text_1  text_2  external
#
#  where:
#
#    internal   SOFA working copy of the C code
#
#    text_1     license text substituted here:
                   $searchString1 = "\\*\\*  Copyright1";
#
#    text_2     license text substituted here:
                   $searchString2 = "/\\* Copyright2 \\*/";
#
#    external   SOFA code to be distributed
#
#  2009 January 29
#

# Verify all arguments are present.

if(@ARGV!=4)
{
  print "\nArguments are mandatory:\n\n";
  print "  1. Input file\n";
  print "  2. File to replace \"$searchString1\"\n";
  print "  3. File to replace \"$searchString2\"\n";
  print "  4. Output file\n\n";

} else {

# Read the input file.
  $source = &readFile($ARGV[0]);

# Substitute Part 1 of the license text.
  $text = &readFile($ARGV[1]);
  $source =~ s/$searchString1/$text/g;

# Substitute Part 2 of the license text.
  $text = &readFile($ARGV[2]);
  $source =~ s/$searchString2/$text/g;

# Write out the result.
  &writeFile($ARGV[3],$source."\n");
}
exit();

#-----------------------------------------------------------------

sub writeFile
{
  open(WRITE2FILE,">".@_[0]) or die "Can't open file ".@_[0];
  flock (WRITE2FILE, 2);
  print WRITE2FILE @_[1];
  close(WRITE2FILE);
}

#-----------------------------------------------------------------

sub readFile
{
  my $fileName  = @_[0];
  $msg="";
  open MSG,"<$fileName" or die "Failed to open $fileName: $!";
  while($line = <MSG>) {$msg.= $line};
  close MSG;
  $msg =~ s/\s+$//;
  return $msg;
}

#-----------------------------------------------------------------
