CC = gcc 
#FC = g95 
FC = gcc

# If Big endian machine use the following
#IN_CFLAGS = -m32 -DBIG_ENDIAN_MACHINE
# If little endian machine use the following
#IN_CFLAGS = -m32
#IN_CFLAGS = -g -DGDS16 -Wall -DCONVERT_GRIB
IN_CFLAGS = -g -Wall -DCONVERT_GRIB

E2FSPROGS = e2fsprogs/e2fsprogs-1.41.14
NETCDF = netcdf/netcdf-4.1.2
HDF5 = netcdf/hdf5-1.8.7
ZLIB = netcdf/zlib-1.2.5

INCLUDE = -I. -I$(PWD)/netcdf/include -I$(E2FSPROGS)/lib/uuid
MYLIBS =  -L$(PWD)/netcdf/lib -lnetcdf -lhdf5_hl -lhdf5 -lm -lz -static -L$(E2FSPROGS)/lib -luuid

OBJECTS = makeGHRSST-level2p-DW_newday.o read_grib.o writeNetCDF.o utils.o diurnal_gentemann.o

all: $(OBJECTS)  
	$(FC) $(IN_CFLAGS) -o $(HOME)/mcidas/bin/makeGHRSST-2.0-level2p-DW_newday.x $(OBJECTS) $(MYLIBS)

CURDIR=$(PWD)

.PHONY: distclean
distclean: 
	rm -f *.o $(HOME)/mcidas/bin/makeGHRSST-2.0-level2p-DW_newday.x	
	cd $(E2FSPROGS) ; make clean ; ./configure --prefix=$(CURDIR)/netcdf ; make ; make install
	cd $(ZLIB) ; make clean ; ./configure --prefix=$(CURDIR)/netcdf ; make ; make install
	cd $(HDF5) ; make clean ; ./configure --prefix=$(CURDIR)/netcdf --with-zlib=$(CURDIR)/netcdf --disable-shared ; make ; make install
	cd $(NETCDF) ; make clean ; ./configure --enable-netcdf-4 --prefix=$(CURDIR)/netcdf --with-zlib=$(CURDIR)/netcdf --with-hdf5=$(CURDIR)/netcdf --disable-shared --disable-dap ; make ; make install

makeGHRSST-level2p-DW_newday.o : makeGHRSST-level2p-DW_newday.c read_grib.h utils.h writeNetCDF.h diurnal_gentemann.h
	$(CC) $(CFLAGS) $(IN_CFLAGS) $(INCLUDE) -c makeGHRSST-level2p-DW_newday.c
writeNetCDF.o : writeNetCDF.c writeNetCDF.h read_grib.h
	$(CC) $(CFLAGS) $(IN_CFLAGS) $(INCLUDE) -c writeNetCDF.c
read_grib.o : read_grib.c read_grib.h utils.h	
	$(CC) $(CFLAGS) $(IN_CFLAGS) $(INCLUDE) -c read_grib.c
utils.o : utils.c utils.h
	$(CC) $(CFLAGS) $(IN_CFLAGS) $(INCLUDE) -c utils.c
diurnal_gentemann.o : diurnal_gentemann.c diurnal_gentemann.h 
	$(CC) $(CFLAGS) $(IN_CFLAGS) $(INCLUDE) -c diurnal_gentemann.c

,PHONY : clean
clean:
	rm -f *.o $(HOME)/mcidas/bin/makeGHRSST-2.0-level2p-DW_newday.x