## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
##
## 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 of the License, or (at your option) any later
## version.
##
## 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.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.


## -*- texinfo -*-
## function for internal usage only
## @end deftypefn

function output = generate_mf_makefile (typename, path)

STRING ="\n\
DIR = @@PATH@@\n\
CPPFLAGS+=@@FF_CPPFLAGS@@ -g\n\
LIBS+=@@FF_LIBS@@\n\
MKOCTFILE = mkoctfile\n\
FFC = ffc\n\
\n\
OCTFILES = MeshFunction_@@TYPENAME@@.oct save_@@TYPENAME@@.oct\n\
\n\
all : $(OCTFILES)\n\
\n\
MeshFunction_@@TYPENAME@@.oct: MeshFunction_@@TYPENAME@@.cc \
                               meshfunction_@@TYPENAME@@.h\n\
	CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o $@ -I$(DIR) -I. $(LDFLAGS) $(LIBS)\n\
\n\
save_@@TYPENAME@@.oct: save_@@TYPENAME@@.cc meshfunction_@@TYPENAME@@.h mkmfdir\n\
	CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o @meshfunction_@@TYPENAME@@/save.oct\
 -I$(DIR) -I. $(LDFLAGS) $(LIBS)\n\
\n\
mkmfdir:\n\
	mkdir -p @meshfunction_@@TYPENAME@@\n\
\n\
.PHONY: clean\n\
\n\
clean:\n\
	$(RM) meshfunction_@@TYPENAME@@.h save_@@TYPENAME@@.o\n\
	$(RM) MeshFunction_@@TYPENAME@@.o MeshFunction_@@TYPENAME@@.cc\n\
	$(RM) save_@@TYPENAME@@.cc\n\
	$(RM) Makefile_@@TYPENAME@@\n\
";

STRING = strrep (STRING, "@@TYPENAME@@", typename);
STRING = strrep (STRING, "@@PATH@@", path);
STRING = strrep (STRING, "@@FF_CPPFLAGS@@", get_vars ("CPPFLAGS"));
STRING = strrep (STRING, "@@FF_LIBS@@", get_vars ("LIBS"));

fid = fopen (["Makefile_", typename], "w");
if (is_valid_file_id (fid))
  fputs (fid, STRING);
  output = fclose (fid);
else
  error ("cannot open file");
  output = 1;
endif

endfunction
