#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper; 

my $dir = "./data/";

opendir(D, "$dir") || die "Can't open directory $dir: $!\n";
my @list = readdir(D);
closedir(D);
my $len = 1;
foreach my $index (@list) {
  if (($index ne '.') && ($index ne '..')) {
    my @fn1 = split("\\." , $index);
    my @fn2 = split('-' , $fn1[0]);
    if ($len lt length($fn2[1]) ){
      $len = length($fn2[1]);
    }
  }
}

print "length : " . $len . "\n";

my $formatstring = "%0" . $len . "d";

foreach my $index (@list) {
  if (($index ne '.') && ($index ne '..')) {
    my @fn1 = split("\\." , $index);
    my @fn2 = split('-' , $fn1[0]);
    my $nfn = $fn2[0] . '-' . sprintf($formatstring , $fn2[1] ). '.' . $fn1[1] ;
    if ($index ne $nfn) {
      my $cmd = 'mv ' . $dir . $index . ' ' .  $dir . $nfn ;
     `$cmd`;
    }
  }
}
