use strict;
use warnings;

foreach my $dir (glob("*")){
   if(-d $dir && $dir !~ m/sg/){
      filter_dir($dir);
   }
}

sub filter_dir {
   my($dir) = @_;
   my $targetdir = $dir."_sg";
   if(!-e $targetdir){
      `mkdir -p $targetdir`;
   }
   foreach my $test (glob("$dir/*.test")){
      my $entire = `cat $test`;
      if($entire =~ m/--small-group/){
         move_test($test, $dir, $targetdir);
      } else {
         print "Not moving $test\n";
      }
   }
}

sub move_test {
   my($test, $sourcedir, $targetdir) = @_;
   print "Will move $test to $targetdir\n";
   my $entire = `cat $test`;
   my($input) = $entire =~ m/"INPUTFILE" : "([^"]*)"/;
   my($result) = $entire =~ m/"RESULT" : "([^"]*)"/;
   defined($input) or die "Input not found for $test";
   defined($result) or die "Output not found for $test";
   print "$input $result\n";
   `git mv $test $targetdir`;
   `cp $sourcedir/$input $targetdir`;
   `git add $targetdir/$input`;
   `git mv $sourcedir/$result $targetdir`;
}
