#!/usr/bin/perl

# make sure we have the configure scripts already run

print "You need to install CVCLite and the OCamlMakefile before you can proceed.\nPlease note where you put these, as well as where the ocaml libraries are.\nAlso please use absolute path names, not relative ones.\nCTRL-C to exit\n\n";


$continue = 1;


while ($continue) {
	$continue = 0;
	print "Enter CVCL source directory: [$ENV{HOME}/cvcl-20050310]\n";
	$dir = <STDIN>;
	chomp $dir;
	$dir =~ s/\/$//;
	if (length $dir == 0) {
	  $dir = "$ENV{HOME}/cvcl-20050310";
	}
	$config = "$dir/config.status";
	if (stat $config == ()) {
		print "$config does not appear to exist.\n  Please try again or install cvcl!\n";
		$continue = 1;
	}
}
print "Found $config\n\n";

$continue = 1;
while ($continue) {
	$continue = 0;
	print "Enter CVCL library install directory: [$dir/lib/linux-i686]\n";
	$cvclibdir = <STDIN>;
	chomp $cvclibdir;
	$cvclibdir =~ s/\/$//;
	if (length $cvclibdir == 0) {
	  $cvclibdir = "$dir/lib/linux-i686";
	}
	if (stat "$cvclibdir/libcvclite.so" == ()) {
		print "$cvclibdir/libcvclite.so does not appear to exist.\n  Please try again or install cvcl!\n";
		$continue = 1;
	}
}
print "Found $cvclibdir/libcvclite.so\n\n";

$continue = 1;
while ($continue) {
	$continue = 0;
	print "Enter CVCL include install directory: [$dir/src/include]\n";
	print "(Note this may end in include/cvcl.)\n";
	$cvcincldir = <STDIN>;
	chomp $cvcincldir;
	$cvcincldir =~ s/\/$//;
	if (length $cvcincldir == 0) {
	  $cvcincldir = "$dir/src/include";
	}
	if (stat "$cvcincldir/vc.h" == ()) {
		print "$cvcincldir/vc.h does not appear to exist.\n  Please try again or install cvcl!\n";
		$continue = 1;
	}
}
print "Found $cvcincldir/vc.h\n\n";

$ocamldir = `ocamlc -where`;
chomp $ocamldir;

$continue = 1;
while ($continue) {
	$continue = 0;
	print "Enter your Ocaml library directory: [$ocamldir]\n";
	$ocamldir = <STDIN>;
	chomp $ocamldir;
	$ocamldir =~ s/\/$//;
	if (length $ocamldir == 0) {
	  $ocamldir = `ocamlc -where`;
	  chomp $ocamldir;
	}
	if (stat "$ocamldir/caml/mlvalues.h" == ()) {
		print "$ocamldir/caml/mlvalues.h does not appear to exist.\nPlease check your Ocaml installation and try again!\n";
		$continue = 1;
	}
}
print "Found $ocamldir/caml/mlvalues.h\n\n";

$continue = 1;
while ($continue) {
	$continue = 0;
	print "Enter OCamlMakefile directory: [$ocamldir/ocaml-make]\n";
	$ocmfdir = <STDIN>;
	chomp $ocmfdir;
	$ocmfdir =~ s/\/$//;
	if (length $ocmfdir == 0) {
	  $ocmfdir = "$ocamldir/ocaml-make";
	}
	$ocmf = "$ocmfdir/OCamlMakefile";
	if (stat $ocmf == ()) {
		print "$ocmf does not appear to exist.\nPlease install the OCamlMakefile!\n";
		$continue = 1;
	}
}
print "Found $ocmf\n\n";

print "If you know your CVCL hashtable compilation flags, enter them here\n[or press enter to scan the CVCL config file]:\n";

$cvcflags = <STDIN>;
chomp $cvcflags;
if (length $cvcflags == 0) {
  $line = `grep CPPFLAGS $config`;
  $cvcflags = "";
  if ($line =~ /CPPFLAGS@,(.*),;t/) {
        $cvcflags = $1;
  } else {
        die "Could not find CPPFLAGS in config.status\n";
  }
}
print "Using $cvcflags\n\n";

print "Are you using GCC4.0 or later?[N]\n";

$gcc4flag = <STDIN>;
if ($gcc4flag =~ /y/i) {
  $cvcflags = "$cvcflags -DGCC4";
}


$mypwd = `pwd`;
chomp $mypwd;

print "Creating Makefile.in...\n";
#open INFILE, "$mypwd/cvcl/interface/Makefile.in" or die "Can't open $mypwd/cvcl/interface/Makefile.in";
open OUTFILE, ">$mypwd/cvcl/interface/Makefile.in" or die " Can't open $mypwd/cvcl/interface/Makefile";

#replace the first 3 lines
#$line = <INFILE>;
#$line = <INFILE>;
#$line = <INFILE>;

print OUTFILE "OCAML_LIB_DIR = $ocamldir\n";
print OUTFILE "CVCL_INCLUDE_DIR = $cvcincldir\n";
print OUTFILE "CVCL_FLAGS = $cvcflags\n";

#while ($line = <INFILE>) {
#	print OUTFILE $line;
#}
#close OUTFILE;
#close INFILE;

#open INFILE, "$mypwd/cvcl/smt/Makefile.in" or die "Can't open $mypwd/cvcl/smt/Makefile.in";
#open OUTFILE, ">$mypwd/cvcl/smt/Makefile" or die "Can't open $mypwd/cvcl/smt/Makefile";

#replace the first 2 lines
#$line = <INFILE>;
#$line = <INFILE>;

print OUTFILE "OCAMLMAKEFILE = $ocmf\n";
print OUTFILE "CVCL_LIB_DIR = $cvclibdir\n";
print OUTFILE "SATBOX_TARGET_DIR = .\n";

print OUTFILE "export OCAML_LIB_DIR\n";
print OUTFILE "export CVCL_INCLUDE_DIR\n";
print OUTFILE "export CVCL_FLAGS\n";
print OUTFILE "export OCAMLMAKEFILE\n";
print OUTFILE "export CVCL_LIB_DIR\n";
print OUTFILE "export SATBOX_TARGET_DIR\n";

#while ($line = <INFILE>) {
#	print OUTFILE $line;
#}
close OUTFILE;
#close INFILE;


print "Success.\n";

