use strict;
my @args;
my $i = 0;

## ce script recupere l'id de code en argument et fait "rotationner'
## l'id stockee dans 'nextcode.txt'

foreach my $a (@ARGV) {
    $args[$i] = $a;
    $i ++;
}

my $file = 'noms-des-codes.txt';
my $flag = 0;

open(FILE, ">nextcode.txt") || die "Erreur E/S : $!\n";

open my $source, "<", $file;

while (defined (my $line = <$source>)) {
    if ($flag == 0) {
	if ($line !~ m/$args[0]/) {
	    next;
	} else {
	    $flag = 1;
	    next;
	}
    }

    if ($flag == 1) {
	$line =~ s/^(.*)\:.*\:.*\:.*\:.*$/$1/;
	print FILE $line;
	last;
    }
}

close($file);
close(FILE);

__END__

