#!/usr/bin/perl

# check if mpiCC or mpicxx exist

$command = system("which mpiCC 2>/dev/null 1>/dev/null");

if($command) {
	$command = system("which mpicxx 2>/dev/null 1>/dev/null");
	if($command) {
		print "mpiCC or mpicxx does not exist in your system";
		print "\n";
		exit(0);
	}
	$mpi = "mpicxx";
}
else { $mpi = "mpiCC"; }

use Cwd;
$cdir = getcwd;

chdir("src");

open(SPEL_T, "SPEL.pl_template") or die "cannot open SPEL.pl_template\n";
open(SPEL, ">SPEL.pl") or die "cannot write to file SPEL.pl\n";
while(<SPEL_T>) {

	if(/^\$command_dir/) {
		$_ = "\$command_dir = \"".$cdir."/bin\";\n";
	}
	print SPEL $_;
}

close(SPEL_T);
close(SPEL);
`cp SPEL.pl ../`;

open(MAKEFILE_T, "Makefile_template") or die "cannot open Makefile_template\n";
open(MAKEFILE, ">Makefile") or die "cannot write to Makefile\n";

while(<MAKEFILE_T>) {
	if( ($mpi eq "mpiCC") and (/#MPICC=mpiCC/) ) { $_ =~ s/#//; }
	if( ($mpi eq "mpicxx") and (/#MPICC=mpicxx/) ) { $_ =~ s/#//; }
	print MAKEFILE $_;
}
close(MAKEFILE);
close(MAKEFILE_T);

chdir("..");
