OS=__$(shell uname)
BUILD=.

include $(BUILD)/make.inc

LIB_INC=-I$(BUILD)/gpklib -I$(BUILD)
CC=$(CCLIB)


.DELETE_ON_ERROR:

.PRECIOUS: $(MYLIBD)/libgpk.a $(BUILD)/libgpk.a

OBJS= intalloc.o douballoc.o floatalloc.o shortalloc.o round.o split.o \
longalloc.o amoeba.o amoeba_helper.o tboolalloc.o root.o GPK_assert.o \
ran1.o amoeba_anneal.o \
invert.o monotonic.o medianI.o medianF.o median.o gpkerr.o	\
ran2.o proplist.o avparse.o chomp_newline.o nfold_vec.o expint.o	\
print_av.o safe_ato.o dict.o gpkieee754.o avg.o

STATIC_OBJS = eqn_solve.o

HDRFILES1 = gpklib.h gpk_std.h
HDRFILES2 = numerics.h NaN.h allocate.cod innerloop2.h innerloop4.h err.h	\
	assert.h ieee754.h doublet.h avg.h

LHDRS=$(HDRFILES2:%=$(BUILD)/gpklib/%) $(BUILD)/gpklib.h
HEADERS=$(HDRFILES1:%=$(INC)/%) $(HDRFILES2:%=$(INC)/gpklib/%)


SUBDIRS = romberg image m22 bulsto
SBUILD = $(SUBDIRS:%=%/all)
SINSTALL = $(SUBDIRS:%=%/install)

.PHONY: all

all : $(SBUILD) $(LHDRS) $(BUILD)/libgpk.a  $(BUILD)/libgpk.so

# all: $(BUILD)/libgpkmisc.a

install_hdrs : $(HEADERS)

install: install_hdrs $(MYLIBD)/libgpk.a $(INC)/gpklib/make.inc $(SINSTALL)
install: $(MYLIBD)/libgpk.so

$(MYLIBD)/% : $(BUILD)/%
	$(INSTALL) $(BUILD)/$* $(MYLIBD)/$* -m 0444

$(MYLIBD)/libgpk.% : $(BUILD)/libgpk.%
	$(INSTALL) $(BUILD)/libgpk.$* $(MYLIBD)/libgpk.$* -m 0444

$(BUILD)/libgpk.a	:	$(BUILD)/libgpk.a($(OBJS))

$(BUILD)/libgpk.a : $(BUILD)/libgpk.a(misc.o)

$(BUILD)/libgpkmisc.a	:	$(BUILD)/libgpkmisc.a($(STATIC_OBJS))

$(BUILD)/libgpk.so	: $(BUILD)/libgpk.a
	rm -rf tmp; mkdir tmp
	cd tmp; $(AR) x ../$(BUILD)/libgpk.a
	$(LDSHARE) -o $(BUILD)/libgpk.so  tmp/*.o -lm -lc
	rm -rf tmp

$(INC)/%.h : %.h
	$(INSTALL) $*.h $(INC)/$*.h -m 0444

$(INC)/gpklib/%.h : %.h
	$(INSTALL) $*.h $(INC)/gpklib/$*.h -m 0444

$(BUILD)/gpklib/%.h : %.h
	$(INSTALL) $*.h gpklib/$*.h -m 0444

$(INC)/gpklib/%.h : gpk%.h
	$(INSTALL) gpk$*.h $(INC)/gpklib/$*.h -m 0444

$(BUILD)/gpklib/%.h : gpk%.h
	$(INSTALL) gpk$*.h gpklib/$*.h -m 0444

$(INC)/gpklib/%.cod : %.cod
	$(INSTALL) $*.cod $(INC)/gpklib/$*.cod -m 0444

$(BUILD)/gpklib/allocate.cod : allocate.cod
	$(INSTALL) allocate.cod gpklib/allocate.cod -m 0444

$(INC)/gpklib/make.inc : gpklib/make.inc
	$(INSTALL) gpklib/make.inc $(INC)/gpklib/make.inc -m 0444

gpklib/make.inc : make.inc all
	cp -p make.inc gpklib/make.inc && echo BASE=$(BASE) >>gpklib/make.inc


test_root : test_root.o root.o $(BUILD)/libgpk.a
	$(LD) -o test_root test_root.o root.o $(BUILD)/libgpk.a

amoeba%.o : numerics.h
a_x.o : numerics.h

douballoc.o floatalloc.o shortalloc.o intalloc.o : allocate.cod gpklib.h
longalloc.o : allocate.cod gpklib.h

$(OBJS) : gpklib.h
$(OBJS) : numerics.h
dict.o : gpklib/dict.h

misc.o : gpklib.h

misc.c gpklib.h : gpklib.in.h make_misc.sh
	sh ./make_misc.sh


avg.o : $(BUILD)/gpklib/avg.h $(BUILD)/gpklib/ieee754.h


romberg/all: FORCE $(LHDRS)
	echo "ENTERING romberg"
	cd romberg; $(MAKE) all

romberg/install: FORCE
	echo "ENTERING romberg"
	cd romberg; $(MAKE) install

image/all: FORCE
	echo "ENTERING image"
	cd image; $(MAKE) all

image/install: FORCE
	echo "ENTERING image"
	cd image; $(MAKE) install

m22/all: FORCE
	echo "ENTERING m22"
	cd m22; $(MAKE) all

m22/install: FORCE
	echo "ENTERING m22"
	cd m22; $(MAKE) install

bulsto/all: FORCE
	echo "ENTERING bulsto"
	cd bulsto; $(MAKE) all

bulsto/install: FORCE
	echo "ENTERING bulsto"
	cd bulsto; $(MAKE) install


FORCE:


clean:
	-rm *.o */*.o
	-rm *.a *.so
	-rm -rf gpklib
