#Makefile to build libpreact.a
#
SHR = ../share/Make.flags
ifneq ("$(wildcard $(CODESYSDIR)/source/misc/makeflags.mk)","")
	SHR = $(CODESYSDIR)/source/misc/makeflags.mk
endif
-include ../share/Make.local
include $(SHR)

ifndef $(PERL)
	PERL = /usr/bin/perl
endif
 
# use f90

ifdef DEBUG
	FFLAGS = $(DFFLAGS)
	CXXFLAGS = $(DCXXFLAGS)
endif

ifeq ("${USEFC}","Y")
	LINK = $(FC)
	FMAIN =
else
	FMAIN = $(F_MAIN)
	LINK = $(CXX) 
	CLIBS = $(FORTLIBS)
endif
ifeq ($(MACHINE),SGI)
	NOARC=Y
	LINK = $(CXX) -LANG:std -Wl,-woff84 -G 0 
endif

ifdef CODESYSDIR
	OBJ = ${LOCAL}
	ARC = $(OBJ)/lib/preact.a
else
	ARC = $(OBJ)/lib/libpreact.a
endif

ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
ifeq ("${OBJ}",".")
	override OBJ=..
endif
endif
 
OBJDIR = $(OBJ)/obj/preact

# for INSTALL:
ifndef PREFIX
	LIBDIR=$(PREACTDIR)/lib
	BINDIR=$(PREACTDIR)/bin
	MANDIR=$(PREACTDIR)/man
	ETCDIR=$(PREACTDIR)/etc
	INCLDIR=$(PREACTDIR)/include
endif

# do not rebuild library if using installed one in $PREFIX 
ifeq ($(MAKELEVEL),0)
	THISLIB=$(ARC)
endif

srcdir = $(shell pwd)

# c++ sources
CSRCA=$(subst .cpp,.o,$(wildcard *.cpp))
CSRC = $(filter-out test%,$(filter-out preact_%.o, $(filter-out %tables.o,$(CSRCA))))
CMEM = $(foreach file,$(CSRC),$(ARC)($(file)))

# fortran archive members
FSRCA  = $(subst .for,.o, $(wildcard *.for)) $(subst .f,.o, $(wildcard *.f)) $(subst .f90,.o, $(wildcard *.f90))
FSRC = $(filter-out test%, $(filter-out %test.o, $(filter-out %init.o, $(FSRCA))))
FMEM = $(foreach file,$(FSRCA),$(ARC)($(file)))
#ifeq ($(MACHINE),CRAY)
	HH = $(notdir $(wildcard ../include/preact/*.h))
	H =  $(foreach file,$(HH),$(OBJDIR)/$(file))
#endif

MEM = $(CMEM) $(FMEM)

LDLIBSA = -L$(OBJ)/lib -lpreact $(LLOC) -lsigsub -laladdinsub -lezcdf -luflib -lvaxonly -lufhdf -lureadsub -lportlib -L$(PSPLINE_HOME)/lib -lpspline
LDLIBS = -L$(OBJ)/lib -lpreact $(LLOC) -lsigsub -laladdinsub -ladpak -lezcdf -lcppsub -lcomput -luflib -lufhdf -lvaxonly -lportlib -L$(PSPLINE_HOME)/lib -lpspline
LDLIBS2 = -L$(OBJ)/lib -lfpreact -lpreact $(LLOC) -lsigsub -laladdinsub -ladpak -lcppsub -lezcdf -lcomput -luflib -lufhdf -lvaxonly -lportlib -L$(PSPLINE_HOME)/lib -lpspline
LDLIBS3 = -L$(OBJ)/lib -lplot_fpreact -lfpreact -lpreact $(LLOC) -lsigsub \
-laladdinsub -ladpak -lcppsub -ltrgraf -lureadsub -lsg -ljc \
-lezcdf -lcomput -luflib -lufhdf -lvaxonly -lportlib \
-L$(PSPLINE_HOME)/lib -lpspline

.PHONY: clean realclean tests

libs: libpreact

libpreact: FORCE $(H) $(ARC)
 
all: FORCE libpreact exec
 
$(ARC): $(MEM)
ifndef NOARC
	@$(RANLIB) $@ > /dev/null
else
	(cd $(OBJDIR); $(CXX) -ar -o ../../lib/libpreact.a *.o)
endif

FORCE:
	@test -d $(OBJ)/lib || mkdir -p $(OBJ)/lib
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)
	@if test ! -f $(LIBDIR)/libfpreact.a; then \
	if test -f ../fpreact/Makefile; then \
	(cd ../fpreact; $(MAKE) libs); fi; fi

$(OBJDIR)/%.h: ../include/preact/%.h
	cat $< | $(PERL) -ne \
	's/F77NAME(\([a-zA-Z0-9_ ]+)\)*/F77NAME\U$$1)/g; print;' > $@ 

$(ARC)(%.o): %.cpp
	$(cxx_proc)
ifndef NOARC
	$(make_ar)
endif

$(ARC)(%.o): %.f
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
ifndef NOARC
	$(make_ar)
endif

$(ARC)(%.o): %.for
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
ifndef NOARC
	$(make_ar)
endif

$(ARC)(%.o): %.f90
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
ifndef NOARC
	$(make_ar)
endif

exec: ckdirs $(OBJ)/test/testpreact $(OBJ)/test/testf77layer \
	$(OBJ)/test/preact_change $(OBJ)/test/preact_init $(OBJ)/test/preact_list \
	$(OBJ)/test/preact_test $(OBJ)/test/fpreact_test $(OBJ)/test/adas_test \
	$(OBJ)/test/sigtab $(OBJ)/test/adas_init tables
	@cp preactinit $(OBJ)/test/ 
	@cp adasinit $(OBJ)/test/ 


ckdirs: FORCE
	@test -d $(OBJ)/test || mkdir -p $(OBJ)/test
	@$(TSTLNK) $(OBJ)/test/fpreact_test.output || ln -s $(srcdir)/fpreact_test.output $(OBJ)/test/

$(OBJDIR)/%.o: %.f
	$(FC)  $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $@ $<

$(OBJDIR)/%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(DEFS) -I./ $(INCFLAGS) -o $@ $<

$(OBJDIR)/%.o: %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) $(OUT) $@ $<

$(OBJ)/test/preact_change: $(OBJDIR)/preact_change.o $(THISLIB)
	$(CXX) $(LDFLAGS) -o $@ $<  $(LDLIBS) $(NETCDF) $(FORTLIBS)

$(OBJ)/test/preact_init: $(OBJDIR)/preact_init.o $(THISLIB)
	$(CXX) $(LDFLAGS) -o $@ $< -L$(OBJ)/lib -lpreact $(LLOC) -lportlib $(FORTLIBS)

$(OBJ)/test/adas_init: $(OBJDIR)/adas_init.o $(THISLIB)
	$(LINK) $(LDFLAGS) -o $@ $< $(FMAIN) -L$(OBJ)/lib $(LLOC) -lezcdf -lportlib $(NETCDF) $(CLIBS)

$(OBJ)/test/preact_list: $(OBJDIR)/preact_list.o $(THISLIB)
	$(CXX) $(LDFLAGS) -o $@ $<  $(LDLIBS) $(NETCDF) $(FORTLIBS)

$(OBJ)/test/testpreact: $(OBJDIR)/testpreact.o $(THISLIB)
	$(CXX) $(LDFLAGS) -o $@ $< $(LDLIBS) $(NETCDF) $(MDSLIB) $(FORTLIBS)

$(OBJ)/test/testf77layer: $(OBJDIR)/testf77layer.o $(THISLIB) 
	$(LINK) $(LDFLAGS) -o $@ $< $(FMAIN) $(LDLIBS) $(NETCDF) $(MDSLIB) $(CLIBS)
 
$(OBJ)/test/fpreact_test: $(OBJDIR)/fpreact_test.o $(THISLIB) 
	$(LINK) $(LDFLAGS) $(MODFLAGS) -o $@ $< $(FMAIN) $(LDLIBS2) $(NETCDF) $(MDSLIB) $(CLIBS)
 
$(OBJ)/test/preact_test: $(OBJDIR)/preact_test.o $(THISLIB) 
	$(LINK) $(LDFLAGS) -o $@ $< $(FMAIN) $(LDLIBS) $(NETCDF) $(MDSLIB) $(CLIBS)

$(OBJ)/test/adas_test: $(OBJDIR)/adas_test.o $(THISLIB)
	$(LINK) $(LDFLAGS) -o $@ $<  $(FMAIN) $(LDLIBSA) $(NETCDF) $(MDSLIB) $(CLIBS)

$(OBJ)/test/sigtab: $(OBJDIR)/sigtab.o $(THISLIB) 
	$(LINK) $(LDFLAGS) -o $@ $< $(FMAIN) $(LDLIBS3) $(NETCDF) $(MDSLIB) $(CLIBS) $(XLIBS) $(EDITLIBS)
 
chkins:
	@test -d $(BINDIR) ||  mkdir -p $(BINDIR)
	@test -d $(ETCDIR) ||  mkdir -p $(ETCDIR)
	@test -d $(INCLDIR)/preact || mkdir -p $(INCLDIR)/preact
	@test -d $(LIBDIR) ||  mkdir -p $(LIBDIR)
	@test -d $(MANDIR)/man3 || mkdir -p $(MANDIR)/man3

install: chkins tables
ifneq "$(PREACTDIR)" ""
	@echo "   >>>>> installing libraries in $(LIBDIR)"
	@umask 133; cp $(OBJ)/lib/libpreact.a $(LIBDIR)/
	@echo "   >>>>> installing .h files in $(INCLDIR)/preact"
	@umask 133; cp ../include/preact/*.h $(INCLDIR)/preact/
	@echo "   >>>>> installing binaries in $(BINDIR)"
	@umask 022; cp $(OBJ)/test/preact* $(BINDIR)/
	@umask 022; cp $(OBJ)/test/fpreact_test $(BINDIR)/
	@umask 022; cp preactinit $(ETCDIR)/
	@umask 022; cp adasinit $(ETCDIR)/
	@echo "   >>>>> installing man pages in $(MANDIR)"
	@umask 133; cp preact.3 $(MANDIR)/man3/	
else
	@echo "   >>>>> -or-  setenv PREACTDIR /usr/ntcc/preact"
endif

tables:
ifneq "$(PREACTDIR)" ""
	@./preactinit PATH $(OBJ)/test
else
	@echo ">>>>>      You must define PREACTDIR and       <<<<<"
	@echo ">>>>> manually run preactinit before execution <<<<<"
endif
ifneq "$(ADASDIR)" ""
	@./adasinit PATH $(OBJ)/test
endif

clean:
	@rm -f $(OBJDIR)/*
 
realclean: clean
	@rm -f $(OBJ)/lib/*preact.a
	@rm -f $(OBJ)/test/preact*
	@rm -f $(OBJ)/test/test*
ifneq "$(PREACTDIR)" ""
	@rm -r $(PREACTDIR)/tables
	@rm -r $(PREACTDIR)/data
endif
ifneq "$(ADASDIR)" ""
	@rm -r $(ADASDIR)/tables
	@rm -r $(ADASDIR)/data
endif

uninstall:
ifneq "$(PREFIX)" ""
	rm -f $(LIBDIR)/libpreact.a
	rm -f $(BINDIR)/preact_init
	rm -f $(BINDIR)/preact_list
	rm -f $(BINDIR)/preact_change
	rm -f $(ETCDIR)/preactinit
	rm -f $(ETCDIR)/adasinit
endif
ifneq  "$(PREACTDIR)" ""
	rm -r $(PREACTDIR)/tables
	rm -r $(PREACTDIR)/data
endif
ifneq  "$(ADASDIR)" ""
	rm -r $(ADASDIR)/tables
	rm -r $(ADASDIR)/data
endif

#
# build and include dependency files $(OBJDIR)/*.d
# for this bit of magic see the gnu make info page under 'Automatic Prequesites'
#
$(OBJDIR)/%.d: %.c
	gcc -MM $(CFLAGS) -I./ $(INCFLAGS) $(CDEFS)  $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,$(OBJDIR)/\1.o $(ARC)(\1.o) $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

$(OBJDIR)/%.d: %.cpp
	@$(CXX) -MM $(CXXFLAGS) -I./ $(INCFLAGS) $(CDEFS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,$(OBJDIR)/\1.o $(ARC)(\1.o) $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

CSRCS=$(wildcard *.cpp)
-include $(foreach file, $(CSRCS:.cpp=.d), $(OBJDIR)/$(file))
 
