#!/usr/bin/env perl

use strict;

my $usage = <<EOM;
Usage:
  fmriqa_updatexml input.xml input2.xml

Input is an event file generated by an old version of fmriqa_generate.pl.
It will be updated (i.e. rewritten) to follow new conventions.
If you aren't sure if you need to use this, you probably don't.
EOM

if (scalar(@ARGV) < 2 || grep { $_ =~ /^--help$/ } @ARGV) {
  die $usage;
}

while (scalar(@ARGV) > 0) {
  my $inputfile = shift;
  open(IFH, '<', $inputfile) || die "Error opening $inputfile for reading: $!\n";
  my @lines = <IFH>;
  close IFH;

  @lines = map {
    if (/<value name="([^"]*)"/) {
      my $valname = $1;
      $valname =~ s/-/_/g;
      s/<value name="([^"]*)"/<value name="$valname"/;
    }
    $_;
  } @lines;

  open(OFH, '>', $inputfile) || die "Error opening $inputfile for writing: $!\n";
  print OFH join('', @lines);
  close OFH;
}

# $Log: In-line log eliminated on transition to SVN; use svn log instead. $
# Revision 1.3  2005/09/19 16:31:54  gadde
# Documentation and help message updates.
#
# Revision 1.2  2005/03/29 18:34:36  gadde
# Allow multiple input files.
#
# Revision 1.1  2005/03/04 16:17:34  gadde
# Initial import.
#
