#!/usr/bin/env perl
#
# rename map files from one date to another date.
#
use strict;
use Getopt::Long;

sub usage {
    die <<EOF;
SYNOPSIS
     renamemapfiles [options]

OPTIONS
     -newdate <datestring>  New creation date string to use
     -oldate  <datestring>  Old creation date on files to rename
EOF
}
my %opts = ( oldate => "130927", 
             newdate => "130928",
            );
GetOptions(
             "o|oldate=s"   => \$opts{'oldate'},
             "n|newdate=s"  => \$opts{'newdate'},
)  or usage();

# Give usage message.
usage() if $opts{'help'};
#
# Check for unparsed arguments
#
if (@ARGV) {
    print "ERROR: unrecognized arguments: @ARGV\n";
   usage();
}

my $wildcard = "map_*_c".$opts{'oldate'}.".nc";
my @files = glob($wildcard);
print @files;

foreach my $file ( @files ) {
   if ( $file =~ /^(map_.+)_c([0-9]+)\.nc$/ ) {
      my $newfile = "${1}_c".$opts{'newdate'}.".nc";
      my $cmd = "mv $file $newfile";
      print "$cmd\n";
      system( $cmd );
   }
}
