#!/usr/bin/perl -w
#
# Zsolt's magical feedback merging script (6 Feb 2006)
#
# Merge experiment header files with allsession.hdr,
# end get rid of the 'give feedback' related lines
#

print "\n\nRun this program in the directory where the experiment feedback\n";
print "files are (*.hdr). The all session feedback will be added to these,\n";
print "while the original *.hdr files will be copied to the orighdr directory.\n\n"; 
print "Archive the newly created station feedback files by typing\n\n"; 
print "'archive -feed -s <session>'.\n\n";

unless (-e "orighdr")
  {mkdir("orighdr",0777) || die "\n\nCannot create orgihdr directory: $!";
   system("cp *.hdr orighdr");}
chdir("orighdr");
my @hdrfiles = glob("*.hdr");
chdir("..");
foreach (@hdrfiles){
  unless ($_ =~ /allsession/){
    $hdr = $_;
    print "$hdr \n";
    open(NEWHDR, ">./$hdr");
    open(OLDHDR, "orighdr/$hdr");
    while (<OLDHDR>){
# Taking out the line containing HREF 
      unless ($_ =~ /HREF/){print NEWHDR "$_"}
    }
    close(OLDHDR);
    print NEWHDR "<P>\n";
    open(SESS, "orighdr/allsession.hdr");
    while(<SESS>){
# Taking out line containing HREF  
      unless ($_ =~ /HREF/){print NEWHDR "$_"}
    }
    close(SESS);
    close(NEWHDR);
  }
}

