#!/usr/bin/perl

($Dir, $FILES) = @ARGV;

if (!(-r $Dir)) {
  print "Error: <$Dir> is not readable.\n";
  exit;
}
elsif (!(-e $Dir)) {
  print "Error: <$Dir> is not exit.\n";
  exit;
}
else {
  opendir(FOLDER, $Dir) || warn "Unable to open $Dir\n";
  foreach $FILES (readdir(FOLDER)) {
    $FILES =~ /(\D+)\.(\d+)\.gz$/;
    $names = $1; $dates = $2;
    if ( $dates =~ /^9912\d+/ ) {
      print "cp $Dir/$FILES $Dir/1999/$FILES\n";
      print "mv $Dir/$FILES $Dir/$names\.19$dates.gz\n";
    }
    elsif ($dates =~ /^99\d+/ ) {
      print "mv $Dir/$FILES $Dir/1998/$FILES\n";
    }
    elsif ($dates =~ /^00\d+/ ) {
      print "mv $Dir/$FILES $Dir/$names\.20$dates\.gz\n";
    }
    elsif ($dates =~ /^98\d+/ ) {
      print "mv $Dir/$FILES $Dir/1998/$FILES\n";
    }
  }
  closedir(FOLDER);
}

exit0;
