#  Copyright (c) 2018
#  Charles Jordan (Hokkaido University, Sapporo, Japan),
#  Michael Joswig, Lars Kastner (Technische Universitaet Berlin, Germany)
# 
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 3, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;

my ($name,$dir,$fsuffix) = fileparse($ARGV[0],".cc");
my $f = "$dir$name";
print STDERR "checking $f\n";

my $path = "/store/marvin/datastore/kastner/repos/mptopcom_tmp/build/Opt";

my $cmd = 'clang++ -DPACKAGE_NAME=\"TOPCOM\" -DPACKAGE_TARNAME=\"topcom\" -DPACKAGE_VERSION=\"0.17.8\" -DPACKAGE_STRING=\"TOPCOM\ 0.17.8\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"topcom\" -DVERSION=\"0.17.8\" -DGMPRATIONAL  -I/usr/local/include -I/store/marvin/datastore/kastner/repos/mptopcom_tmp/lib-src -I/store/marvin/datastore/kastner/repos/mptopcom_tmp/lib-src-reg -I/store/marvin/datastore/kastner/repos/mptopcom_tmp/wrap-gmp-gmpxx -DPOLYMAKE_VERSION=302 -fPIC -pipe -std=c++14 -Wno-logical-op-parentheses -Wno-shift-op-parentheses -Wno-mismatched-tags -Wno-unused-local-typedef -fopenmp -O3 -DPOLYMAKE_DEBUG=0 -I/homes/combi/kastner/polymake_install/include -I/homes/combi/kastner/polymake_install/include/polymake/external -lcddgmp -L/homes/combi/kastner/polymake_install/lib -fopenmp -Wl,-rpath,/homes/combi/kastner/polymake_install/lib -I/usr/include/cdd -o /dev/null ';

my $suffix = " -fopenmp -lmpfr -lgmp -lpthread -lgmpxx -ldl -lpolymake -lpolymake-apps -lmpfr -lgmp -lpthread  -ldl -lxml2 -L/lib64 -lz -llzma -lm -ldl";

my $i=0;

my @allobjs = glob "$path/lib*/*.o";
push @allobjs, "$path/src-mp/mts.o";
push @allobjs, "$path/src-mp/mtslib.o";
# foreach my $fn (@allobjs){
#    $fn =~ s/$path\///;
# }


if ($f eq "src-mp/mptopcom") {
   $cmd = '/homes/combi/kastner/ompi/bin/mpicxx -DPACKAGE_NAME=\"TOPCOM\" -DPACKAGE_TARNAME=\"topcom\" -DPACKAGE_VERSION=\"0.17.8\" -DPACKAGE_STRING=\"TOPCOM\ 0.17.8\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"topcom\" -DVERSION=\"0.17.8\" -DGMPRATIONAL  -I/usr/local/include -I/store/marvin/datastore/kastner/repos/mptopcom_tmp/lib-src -I/store/marvin/datastore/kastner/repos/mptopcom_tmp/lib-src-reg -I/store/marvin/datastore/kastner/repos/mptopcom_tmp/wrap-gmp-gmpxx -DPOLYMAKE_VERSION=302 -fPIC -pipe -std=c++14 -Wno-logical-op-parentheses -Wno-shift-op-parentheses -Wno-mismatched-tags -Wno-unused-local-typedef -fopenmp -O3 -DPOLYMAKE_DEBUG=0 -I/homes/combi/kastner/polymake_install/include -I/homes/combi/kastner/polymake_install/include/polymake/external -lcddgmp -L/homes/combi/kastner/polymake_install/lib -fopenmp -Wl,-rpath,/homes/combi/kastner/polymake_install/lib -I/usr/include/cdd -o /dev/null ';
   @allobjs = grep($_ !~ m/ComputeTriangs/, @allobjs);
   @allobjs = grep($_ !~ m/SymmetricBFS/, @allobjs);
} else {
   @allobjs = grep($_ !~ m/mts/, @allobjs);
}


my @currobjs = @allobjs;

while ($i < @currobjs) {
   # print "   testing $i -- ",$currobjs[$i],"\n";
   my @newobjs = grep {!/$currobjs[$i]/} @currobjs;
   $i++;
   my $objs = join(" ",@newobjs);
   # print "$cmd $path/$f.o $objs $suffix\n\n\n";
   my $err = `$cmd $path/$f.o $objs $suffix 2>&1`;
   if ($? == 0) {
      $i = 0;
      @currobjs = @newobjs;
      print STDERR "newcount: ",scalar(@currobjs),"\n";
   } else {
#      print STDERR $err;
   }
}
foreach my $file (@currobjs){
   $file =~ s/$path\///;
}
print "$f: ",join " ",@currobjs,"\n";
print STDERR "$f: ",join " ",@currobjs,"\n";
