#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
# Creation Date: Thu Dec 21 20:46:27 PST 2000
# Last Modified: Thu Dec 21 20:46:27 PST 2000
# Filename:      /disk/linux7/musedata/data/bach/bg/canon/1080/info/makefullref
# Syntax:        perl 5
#
# Description: Generates a full reference record out of partial
#               reference records for work/movement.
#


use strict;

if (@ARGV != 2) {
   print "Usage: $0 work-reference movement-reference\n";
   exit(1);
}

my $workfile = $ARGV[0];
my $mvmtfile = $ARGV[1];

my %workinfo = getWorkInfo($workfile);
my %mvmtinfo = getMvmtInfo($mvmtfile);

my $top = generateTop();
my $bottom = generateBottom();


#print "WORKTOP:\n";
#print $workinfo{'TOP'};
#print "\n\n\nWORKBOTTOM:\n";
#print $workinfo{'BOTTOM'};
#print "\n\n\nMVMTTOP:\n";
#print $mvmtinfo{'TOP'};
#print "\n\n\nMVMTBOTTOM:\n";
#print $mvmtinfo{'BOTTOM'};

print "TOP:\n";
print "$top";
print "\n\n\nBOTTOM:\n";
print "$bottom";


exit(1);

#########################################################################


##############################
##
## generateTop --
##

sub generateTop {
   my $wtop = $workinfo{'TOP'};   
   my $mtop = $mvmtinfo{'TOP'};

   $wtop =~ s/\@MOVEMENT-TOP/$mtop/m;
   $wtop =~ s/\n+/\n/gm;
 
   return $wtop;
}



##############################
##
## generateBottom --
##

sub generateBottom {
   my $wbot = $workinfo{'BOTTOM'};   
   my $mbot = $mvmtinfo{'BOTTOM'};

   $wbot =~ s/\@MOVEMENT-BOTTOM/$mbot/m;
   $wbot =~ s/\n+/\n/gm;
 
   return $wbot;
}



##############################
##
## getWorkInfo --
##

sub getWorkInfo {
   my ($file) = @_;
   my @contents;
   open (INFILE, "$file") || die;
   my $line;
   while ($line = <INFILE>) {
      if ($line =~ /^\s*$/) {
         next;
      }
      if ($line !~ /^\@/ && $line !~ /^\!/) {
         next;
      }
      $contents[@contents] = $line;
   }
   close INFILE;

   my $count = 0;
   my $size = @contents;
   my $top = "";
   my $bottom = "";

   if ($contents[$count] =~ /^\@TOP\s*$/ || $contents[$count] =~ /^\!/) {
      if ($contents[$count] =~ /^\@TOP\s*$/) {
         $count++;
      } 
      while ($count < $size && $contents[$count] !~ /^\@BOTTOM/) {
         $top .= $contents[$count];
         $count++;
      }
   }
   if ($count < $size && $contents[$count] =~ /\@BOTTOM\s*$/) {
      $count++;
      while ($count < $size) {
         $bottom .= $contents[$count];
         $count++;
      }
   }

   my %output;
   $output{'TOP'} = $top;
   $output{'BOTTOM'} = $bottom;
   return %output;
}



##############################
##
## getMvmtInfo --
##

sub getMvmtInfo {
   my ($file) = @_;
   my @contents;
   open (INFILE, "$file") || die;
   my $line;
   while ($line = <INFILE>) {
      if ($line =~ /^\s*$/) {
         next;
      }
      if ($line !~ /^\@/ && $line !~ /^\!/) {
         next;
      }
      $contents[@contents] = $line;
   }
   close INFILE;

   my $count = 0;
   my $size = @contents;
   my $top = "";
   my $bottom = "";

   if ($contents[$count] =~ /^\@TOP\s*$/ || $contents[$count] =~ /^\!/) {
      if ($contents[$count] =~ /^\@TOP\s*$/) {
         $count++;
      } 
      while ($count < $size && $contents[$count] !~ /^\@BOTTOM/) {
         $top .= $contents[$count];
         $count++;
      }
   }
   if ($count < $size && $contents[$count] =~ /\@BOTTOM\s*$/) {
      $count++;
      while ($count < $size) {
         $bottom .= $contents[$count];
         $count++;
      }
   }

   my %output;
   $output{'TOP'} = $top;
   $output{'BOTTOM'} = $bottom;
   return %output;
}

