#!/usr/local/bin/perl
# Author Tomasz Haupt  (haupt@npac.syr.edu) 1997
#===========================================================================
$makeout=">make_dagh";
open(MOUT,$makeout)  || die "cannot open $makeout: $! \n";

#fortran90 modules
#$makein="fmodules";
#open(MIN,$makein)  || die "cannot open $makein: $! \n";
#while(<MIN>) {
#    $modules=$_;
#}
#close MIN;

#fortran90 interfaces
$makein="finterfaces";
open(MIN,$makein)  || die "cannot open $makein: $! \n";
while(<MIN>) {
    $interface=$_;
}
close MIN;

#f_subroutines
$makein="ffiles";
open(MIN,$makein)  || die "cannot open $makein: $! \n";
while(<MIN>) {
    $ffiles=$_;
}
close MIN;

#application routines
$makein="appfiles";
open(MIN,$makein)  || die "cannot open $makein: $! \n";
while(<MIN>) {
    $appfiles=$_;
}
close MIN;

#$makestr=$modules.$interface.$ffiles.$appfiles;
$makestr=$interface.$ffiles.$appfiles;

$makestr =~ s/\.o/\.f90/g;
$makestr =~ s/\s//g;
$makestr =~ s/f90/f90 /g;
chop($makestr);

#format makestr
$mlist=$makestr;
$ml=length($mlist);
$space=0;

if($ml>51) {
 $newlist="";
 @maw=split(//,$mlist);
 for($i=0;$i<$ml+1;$i++) {
  $newlist=$newlist.$maw[$i];
  if($i>0 && $i%50 == 0) {
     $space=1;
  }
  if(($space==1) && ($maw[$i] eq " ")) {
       chop($newlist);
       $newlist=$newlist."\\\n        ";
       $space=0;
  }
 }
$makestr=$newlist;
} 

 

# ----------------------------------------------------
#start writting makefile
# ----------------------------------------------------
print MOUT "DAGH_HOME = /home/A6D/bh/DAGH/NDAGH\n\n";
print MOUT "# Include architecture specific makefile\n";
print MOUT "include \$(DAGH_HOME)/make.defn\n\n";

print MOUT ".SUFFIXES: .C .c .F .o .a .f .f90 .cpp\n\n";

print MOUT ".f90.o:\n";
print MOUT "\tf90  -c -64 -r8 \$<\n\n";

print MOUT "APPSRC = $makestr dataout.f90\n";
print MOUT "APPOBJ = \$(APPSRC:.f90=.o) \n";
print MOUT "CSRC =  adm.C Berger_Oliger.C Evolve0.C tstep.C InitData0.C\\\n";
print MOUT "        init.C TimeStep.C \n";
print MOUT "COBJ =  adm.o Berger_Oliger.o Evolve0.o tstep.o InitData0.o\\\n";
print MOUT "        init.o\\\n";
print MOUT "        TimeStep.o DataOut0.o java_ser.o\n\n";

print MOUT "# Application specific Flags \n";
print MOUT "C++APPFLAGS = \$(DEBUGFLAGS) \n";
print MOUT "CAPPFLAGS = \$(DEBUGFLAGS) \n";
print MOUT "F77APPFLAGS= \$(FDEBUGFLAGS) \n";
print MOUT "F90APPFLAGS = \$(FDEBUGFLAGS) \n\n";

print MOUT "# Application specific libraries \n";
print MOUT "APPLIB = -lbbhutil -lfvs -lutilio -lvutil\n";
print MOUT "LOADF = \$(C++LINK) -L/home/A11D/scott/lib/sgi64\n\n";

print MOUT "OUTPUT = adm\n";

print MOUT "all: makefile\n";
print MOUT "\tmake -f makefile Executable\n\n";

print MOUT "Executable: \$(OUTPUT)\n\n";

print MOUT "\$(OUTPUT): Makefile  \$(APPOBJ) \$(COBJ) \n";
print MOUT "\t \$(RM) \$(OUTPUT) \n";
print MOUT "\t \$(LOADF) \$(C++FLAGS) -o \$@ \\\n";
print MOUT "     \$(COBJ) \$(APPOBJ) \$(APPLIB) \$(LDLIBS) \n\n";
print MOUT "clean: \n";
print MOUT "\t \$(RM) *.o core \$(OUTPUT) \n";
print MOUT "\t \$(RM) -r \$(REPOSITORY) \n\n";

print MOUT "makefile: make_dagh \$(F90FILES)\n";
print MOUT "\tf90mkdepend -S make_dagh -D makefile -s .f90\n";




close(MOUT);
