NCLIB=../liblib/.libs/libnetcdf.a
NCINCL=-I ../liblib

LFLAG=-L/usr/local/lib

HDF5LIB=-lhdf5_hl -lhdf5
CURLLIB=-lcurl
ZLIB=-lz

OTHERLIBS=-lm

LIBS=${NCLIB} ${LFLAG} ${CURLLIB} ${HDF5LIB} ${ZLIB} ${OTHERLIBS}
INCL=-I. -I.. -I../include -I../libsrc -I../libsrc4 ${NCINCL} 
RPATH=-Wl,-rpath,${LFLAG}

# Might want to specify a particular C compiler with flags
CC=gcc
#CFLAGS=-g -O2 -Wall -DHAVE_CONFIG_H
CFLAGS=-g -O2 -DHAVE_CONFIG_H

GFLAGS=-g -O0
#############################################

TESTLIBS=${LIBS}
TESTINCL=${INCL}

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

SRC=\
dapcvt.c \
dapalign.c \
dapodom.c \
getvar3.c \
ncdaperr.c \
cdf3.c \
ncdap3.c \
dapdispatch3.c \
dispatch3.c \
dapdump.c \
dapdebug.c \
daputil.c \
nchashmap.c nclist.c ncbytes.c
 
HDRS=\
daprename.h \
dapalign.h \
dapodom.h \
getvar3.h \
dapnc.h \
daputil.h \
dispatch3.h \
dispatchdefs3.h \
dapdispatch3.h \
ncdap3.h \
dapdebug.h \
dapdump.h \
nchashmap.c nclist.h ncbytebuffer.h

OBJ=${SRC:%.c=%.o}

ALLSRC=${SRC} ${HDRS} dap.y ce.y

EXE=main.exe

DRNOLIB=libncdap.a

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

#all:: ncd octest

all: varm

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

${EXE}: ${DRNOLIB} main.c
	${CC} ${INCL} ${GFLAGS} -c main.c
	if test -e ./libs/${DRNOLIB} ; then \
	  ${CC} -o ${EXE} main.o ./.libs/${DRNOLIB} ./oc/.libs/${OCLIB} ${LIBS} ; \
	else \
	  ${CC} -o ${EXE} main.o ${DRNOLIB} ${OCLIB} ${LIBS} ; \
	fi

${DRNOLIB}: ${OBJ}
	ar r ${DRNOLIB} ${OBJ}

${OBJ}: ${SRC} ${HDRS}
	${CC} ${CFLAGS} ${GFLAGS} -c ${INCL} ${SRC}

##################################################
clean::
	rm -f *.o *.exe ${EXE} ${DRNOLIB}
	rm -fr *.tab.c *.tab.h *.output 
	rm -f *.stackdump
	rm -fr results

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

${OCLIB}:
	cp oc/${OCLIB} .

##################################################
UTILDIR=~/nc/utils

util:: ncbytes.h nclist.h nchashmap.h

ncbytes.h ncbytes.c: ${UTILDIR}/bytes_template.h ${UTILDIR}/bytes_template.c
	rm -f ncbytes.h ncbytes.c
	sed -e 's/<<>>/NC/g' -e 's/<>/nc/g' <${UTILDIR}/bytes_template.h >./ncbytes.h
	sed -e 's/<<>>/NC/g' -e 's/<>/nc/g' <${UTILDIR}/bytes_template.c >./ncbytes.c

nclist.h nclist.c: ${UTILDIR}/list_template.h ${UTILDIR}/list_template.c
	rm -f nclist.h nclist.c
	sed -e 's/<<>>/NC/g' -e 's/<>/nc/g' <${UTILDIR}/list_template.h >./nclist.h
	sed -e 's/<<>>/NC/g' -e 's/<>/nc/g' <${UTILDIR}/list_template.c >./nclist.c

nchashmap.h nchashmap.c: ${UTILDIR}/hashmap_template.h ${UTILDIR}/hashmap_template.c
	rm -f nchashmap.h nchashmap.c
	sed -e 's/<<>>/NC/g' -e 's/<>/nc/g' <${UTILDIR}/hashmap_template.h >./nchashmap.h
	sed -e 's/<<>>/NC/g' -e 's/<>/nc/g' <${UTILDIR}/hashmap_template.c >./nchashmap.c

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

XDRSRC=xdr.c xdr_float.c xdr_mem.c xdr_stdio.c

librpc.a: ${XDRSRC}
	${CC} ${CFLAGS} ${GFLAGS} -c ${XDRSRC}
	ar r librpc.a ${XDRSRC:.c=.o}


##################################################
#	for f in $$OCFILES; do 

makeoc::
	-mkdir ./oc
	rm -f ./oc/*.[chy]
	for f in ${HOME}/nc/oc/*.[chy]; do base=`basename $$f` ; \
		cat $$f | tr -d '\r' >./oc/$$base; done
	cd ./oc; rm -f i.* apitest.* getoc.* imain.* main.*
	cd ./oc; rm -f config.h dap.tab.c dap.tab.h ce.y ceparselex.*
	cd ./oc; rm -f ocshadow.*
	cd ./oc; rm -f octest.c
	cd ./oc; rm -f ocinternal.h
	sed -e 's|/[*]#undef OC_DISK_STORAGE[*]/|#undef OC_DISK_STORAGE|g' \
	    < ${HOME}/nc/oc/ocinternal.h | tr -d '\r' >./oc/ocinternal.h
	

#	cd ./oc; rm -f ocdebug.h
#	sed -e 's|#undef DUMPTEMP|#define DUMPTEMP|g' \
	    -e 's|#undef OCCATCHERROR|#define OCCATCHERROR|g' \
		<${HOME}/nc/oc/ocdebug.h >./oc/ocdebug.h

#	echo '#undef HAVE_RPC_XDR_H' >./oc/ocinternal.h
#	echo '#define HAVE_RPC_XDR_H' >>./oc/ocinternal.h
#	cat ${HOME}/nc/oc/ocdebug.h >>./oc/ocdebug.h


diffoc::
	for f in /home/dmh/nc/oc/*.[chy] ; do \
	    x=`basename $$f|tr -d '
' ` ; \
	    if test "x$$x" = "xdaptab.c"; then continue ; fi ; \
	    if test -e oc/$$x ; then diff -qw oc/$$x $$f ; fi; \
	done
	for f in /home/dmh/nc/oc/*.[chy] ; do \
	    x=`basename $$f|tr -d '
' ` ; \
	    if test "x$$x" = "xdaptab.c"; then continue ; fi ; \
	    if test -e oc/$$x ; then \
		if ! diff -qw oc/$$x $$f > /dev/null ; then \
		echo diff -w oc/$$x $$f ;\
		diff -w oc/$$x $$f ; \
		fi; \
	    fi; \
	done

octest::
	cd oc; make -f Make0 all
	cp oc/octest.exe ./octest

##################################################
TESTPARAMS=[unlimitedsequence][limit=10]
#TESTDEBUG=-d

ALLTESTS=synth1 synth2 synth3 synth4 synth5 synth6
TESTURL=file:///home/dmh/nc/netcdf-3/ncdap_test/testdata3

tests::
	rm -fr results
	mkdir results
	for t in ${ALLTESTS} ; do (\
	  echo "-------------------------" ; \
	  echo "$${t}:" ; \
	  echo "-------------------------"; \
	  if ! ./${EXE} "${TESTPARAMS}${TESTURL}/$${t}${TESTCONS}" > results/$${t}.dmp ; then \
		echo "$${t} failed"; \
	  fi; \
	  echo; \
	) done

td:: ${TESTSRC} ${TEST}.c
	${CC} ${CFLAGS} ${TESTINCL} ${GFLAGS} -c ${TEST}.c ${TESTSRC}
	${CC} -o ${TEST}.exe ${TEST}.o ${TESTOBJ} ${TESTLIBS}
	./${TEST}.exe -v ${TEST}

URL=${TESTPARAMS}${TESTURL}/${TEST}${TESTCONS}

t:: ${EXE}
	${VALGRIND} ./${EXE} -v ${TESTDEBUG} "${URL}"

cpp::
	rm -f junki
	cc -E -DHAVE_CONFIG_H -I. -I.. -I../fortran -I../libsrc -I../libncdap/oc ${GFLAGS} drno.c >junk

##################################################
# ncd
NCDUMPC= ../ncdump/ncdump.c ../ncdump/vardata.c ../ncdump/dumplib.c ../ncdump/indent.c ../ncdump/nctime0.c ../ncdump/nciter.c ../ncdump/utils.c

NCDUMPOBJ=${NCDUMPC:../ncdump/%.c=%.o}

#${NCDUMPC} ${NCDUMPH} ${NCLIB}
ncd:: 
	cd ..; make 
	${CC} -I.. ${CFLAGS} ${GFLAGS} -c ${INCL} ${NCDUMPC}
	${CC} -o ncd ${NCDUMPOBJ} ${LIBS}


##################################################
# ncc
NCCOPYC=../ncdump/nccopy.c ../ncdump/nciter.c
NCCOPYH=
NCCOPYOBJ=${NCCOPYC:%.c=%.o}

#${NCDUMPC} ${NCDUMPH} ${NCLIB}
ncc:: 
	cd ../libsrc; make 
	${CC} -I.. ${CFLAGS} ${GFLAGS} -c ${INCL} ${NCCOPYC}
	${CC} -o ncc ${NCCOPYOBJ} ${NCLIB} ${CURLLIB} ${HDFLIBS} ${OTHERLIBS}

##################################################
vpt:: 
	${CC} -g -O0 ${CFLAGS} -c ${INCL} vpt.c
	${CC} -o vpt.exe vpt.o ${NCLIB} ${CURLLIB} ${HDFLIBS} ${OTHERLIBS}
	date; ./vpt.exe "[compile]http://oceanwatch.pfeg.noaa.gov/opendap/GLOBEC/GLOBEC_vpt" vpt.year; date

##################################################
do::
	pushd ..; make; popd ; $(MAKE) -f Make0 ncd
##################################################
FF=cache constraints3 dapattr3 dapdebug daputil ncdap3 ncdap3a
fix::
	for f in ${FF} ; do \
	rm -f ./$${f}.c ;\
	sed -e 's/oc_log/nclog/g' -e 's/OCLOG/NCLOG/g' <ckp1/$${f}.c >./$${f}.c ; \
	done

##################################################
E=common34
e::
	cc -E -g -c ${E}.c ${INCL} -I../oc2 >${E}.txt

##################################################
T=test_nstride_cached

v::
	cc -g -c ${T}.c ${INCL}
	cc -g -o v ${T}.o ${LIBS} ${RPATH}

