#!/usr/bin/perl

#$ARGV[0]: The address of sequences of applications that you want to layer.
#$ARGV[1]: The target address which you want to save the result.

my @queue;
my %countForThread;
my $counter;
if(@ARGV != 2){
	die "Error!\n";
}

@queue = glob "$ARGV[0]/*";

foreach (@queue){
	$countForThread{$_} = 0;
}

for($counter = 0; $counter < @queue; $counter++){
	print "$queue[$counter] = $countForThread{$queue[$counter]}\n";
	open INPUT, "<$queue[$counter]";
	while(<INPUT>){
		chomp;
		if($_ =~ /clone.+(\d\d\d)$/){
			$countForThread{"$ARGV[0]/zygote.$1"} = $countForThread{$queue[$counter]} + 1;
		}
	}	
	close INPUT;
	$queue[$counter] =~ /(zygote.\d\d\d)/;
	system("cp $queue[$counter] $ARGV[1]/$1.$countForThread{$queue[$counter]}");
}

