########################################################
#
# User should make the appropriate changes below
#
########################################################

# Numerical Recipes home path 
NRC_PATH = /usr/local/src/recipes_c-ansi/recipes

# compiler
CC  = gcc
C++ = g++

#linker
LD  = g++

# Ansi compiler options
STRICTANSI = -Wconversion -Wmissing-prototypes -Wstrict-prototypes \
	-Wall -ansi -pedantic -fno-common 

# IEEE standard 
IEEE = 

# optimization level
OPTIMIZATION = -O2
#OPTIMIZATION =

# debugger flag
DEBUG = -g
#DEBUG = 

# machine architecture
MACH = -m486
#MACH = -msupersparc

# Set most checks inclusive index checks
#CHCK = -DCHECK
CHCK = 

# Set check for numerical differentiation
#NCHCK = -DNUM_CHECK
NCHCK =

# generate position-independent code, e.g., for R
PIC = -fpic 
#PIC = 

# Compilation for R
RFLAG = -DRFLAG
#RFLAG = 


########################################################


OPTIONS = $(STRICTANSI) $(IEEE) $(OPTIMIZATION) $(DEBUG) $(MACH) $(PIC)\
	 $(CHCK) $(NCHCK) $(DUCHCK) $(RFLAG)
LIBRARIES = -lstdc++ -lm


FFNET_R_HOME = `R RHOME`


PATCH_FILES = nr.c nr.c.orig 
NR_FILES = ran2.c gasdev.c lnsrch.c dfpmin.c mnbrak.c brent.c f1dim.c linmin.c \
	frprmn.c amotsa.c amebsa.c choldc.c


OBJS = intset.o neuro.o nrext.o nrutil.o utils.o linal.o nrlinal.o \
	str.o timer.o R_ffnet.o


LIB_NAME = ffnet


LIB_SHARED = $(LIB_NAME).so


all: $(LIB_SHARED)

test: example
	example result.new
	diff result.new result.orig
	rm result.new

example: example.o $(LIB_SHARED)
	$(C++) $(OPTIONS) -o example example.o -L./ -l$(LIB_NAME) $(LIBRARIES)

example.o: example.cc nrlinal.h linal.hh utils.hh neuro.hh intset.hh
	$(C++) $(OPTIONS) -c example.cc

$(LIB_SHARED): $(OBJS)
	$(LD) -shared -o $(LIB_SHARED) $(OBJS) $(LIBRARIES)

R_ffnet.o: R_ffnet.cc linal.hh neuro.hh intset.hh
	$(C++) $(OPTIONS) -c R_ffnet.cc

neuro.o: neuro.cc nrutil.h nrext.h nrlinal.h utils.hh str.hh linal.hh \
	neuro.hh intset.hh nr.c
	$(C++) $(OPTIONS) -c neuro.cc

linal.o: linal.cc nrutil.h nrlinal.h nrext.h utils.hh str.hh linal.hh
	$(C++) $(OPTIONS) -c linal.cc

intset.o: intset.cc utils.hh intset.hh nrutil.h
	$(C++) $(OPTIONS) -c intset.cc

str.o: str.cc utils.hh str.hh
	$(C++) $(OPTIONS) -I$(FFNET_R_HOME)/include/ -c str.cc

timer.o: timer.cc timer.hh
	$(C++) $(OPTIONS) -c timer.cc

utils.o: utils.cc utils.hh
	$(C++) $(OPTIONS) -I$(FFNET_R_HOME)/include/ -c utils.cc

nrlinal.o: nrlinal.c nrutil.h nrlinal.h nr.c
	$(CC) $(OPTIONS) -c nrlinal.c

nrutil.o: nrutil.c nrutil.h
	$(CC) $(OPTIONS) -I$(FFNET_R_HOME)/include/ -c nrutil.c

nrext.o: nrext.c nrutil.h nrlinal.h nrext.h nr.c
	$(CC) $(OPTIONS) -c nrext.c

nr.o: nr.c nrutil.h nrext.h
	$(CC) $(OPTIONS) -c nr.c

nr.c:  	
	for F in $(NR_FILES); do \
		cp $(NRC_PATH)/$${F} ./; \
	done
	cat $(NR_FILES) > nr.c
	patch -b nr.c nr.c.patch
	rm -f $(NR_FILES)

rmpatch:
	rm -f $(PATCH_FILES)

clean:
	rm -f *.o *.so example

realclean: distclean

distclean: rmpatch clean
	rm -f *~ *#*




