#------------------------------------------------------------------------------
# Copyright 1996-2013 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#
#  NSSDC/CDF						CDF tools makefile.
#
#  Version 1.5a, 17-Feb-97, Hughes STX.
#
#  Modification history:
#
#   V1.0  25-Jan-94, J Love	Original version.
#   V1.0a 23-Feb-94, J Love	Added `CC='.
#   V1.0b  6-Apr-94, J Love	Finished `CC=', Solaris using Gnu C compiler.
#   V1.1  28-Nov-94, J Love	CDF V2.5.
#   V1.2  30-Dec-94, J Love	Update dependencies.
#   V1.2a 19-Jan-95, J Love	IRIX 6.x.
#   V1.3  16-Mar-95, J Love	Multi-compiler support, etc.
#   V1.4   3-Apr-95, J Love	POSIX.
#   V1.4a 18-Jul-95, J Love	Added CDFexport.
#   V1.5  12-Aug-96, J Love	CDF V2.6.
#   V1.5a 17-Feb-97, J Love	Added `libcdf.a' to dependencies for `cdfinq'.
#   V1.6  23-Aug-06, M Liu      Added cdfirdump and cdfmerge.
#   V2.0  20-Feb-16, M Liu      Mac OS to build both 32 and 64-bit universal
#                               binary.
#
#------------------------------------------------------------------------------

.SILENT:

SHELL=/bin/sh

CURSES=
CCx=
COPTIONS=
COPTIONSld=
SYSLIBS=
CURSESINC=
CURSESLIB=
SRCDIR=
BUILDDIR=

INCLUDEcdf= $(SRCDIR)/../include

CC=$(CCx)
CFLAGS=$(COPTIONS) -I$(INCLUDEcdf)
CFLAGSld=$(COPTIONSld)

TORPATH=0
WHICHOS=$(shell uname -a | cut -f1 -d" ")
ifeq ("$(WHICHOS)","Darwin")
   LIBCDFa=../lib/libcdf.dylib
   LIBCDFb=-L../lib -Wl,-rpath,@loader_path/../lib -lcdf
   TORPATH=1
else
  WHICHOS=$(shell uname -a | cut -f7 -d" ")
  ifeq ("$(WHICHOS)","Cygwin")
    LIBCDFa=../lib/libcdf.dll
    LIBCDFb=../lib/libcdf.dll
  else
    ifeq ("$(WHICHOS)","Msys")
      LIBCDFa=../lib/libcdf.dll
      LIBCDFb=../lib/libcdf.dll
    else
      LIBCDFa=../lib/libcdf.a
      LIBCDFb=../lib/libcdf.a
    endif
  endif
endif

LIBs1=$(LIBCDFb) $(SYSLIBS)
LIBs2=$(LIBCDFb) $(CURSESLIB) $(SYSLIBS)

BINsCURSES=cdfxp cdfedit 
BINsNOTCURSES=cdfcvt cdfstats cdfcmp cdf2skt skt2cdf cdfinq cdfdump cdfirsdump cdfmerge cdfvalidate cdfleapsecondsinfo

CONFIGh=$(INCLUDEcdf)/cdfconfig.h
CDFh=$(INCLUDEcdf)/cdf.h
CDFDISTh=$(INCLUDEcdf)/cdfdist.h $(CDFh) $(CONFIGh)
CDFLIBh=$(INCLUDEcdf)/cdflib.h $(INCLUDEcdf)/cdflib64.h $(CDFDISTh)
CDFTOOLSh=$(INCLUDEcdf)/cdftools.h $(CDFDISTh)

WINDOZh=$(INCLUDEcdf)/windoz.h $(CDFTOOLSh)
WIDGETSh=$(INCLUDEcdf)/widgets.h $(WINDOZh)
CDFCVTh=$(INCLUDEcdf)/cdfcvt.h $(CDFTOOLSh)
CDFEDITh=$(INCLUDEcdf)/cdfedit.h $(WIDGETSh)
CDFXPh=$(INCLUDEcdf)/cdfxp.h $(WIDGETSh)
CDFINQh=$(INCLUDEcdf)/cdfinq.h $(CDFTOOLSh)
CDFSTATSh=$(INCLUDEcdf)/cdfstats.h $(CDFTOOLSh)
CDFCMPh=$(INCLUDEcdf)/cdfcmp.h $(CDFTOOLSh)
CDF2SKTh=$(INCLUDEcdf)/cdf2skt.h $(CDFTOOLSh)
SKT2CDFh=$(INCLUDEcdf)/skt2cdf.h $(CDFTOOLSh)
CDFDUMPh=$(INCLUDEcdf)/cdfdump.h $(CDFTOOLSh) $(INCLUDEcdf)/cdflib.h 
CDFIRsDUMPh=$(INCLUDEcdf)/cdfirsdump.h $(CDFTOOLSh) $(INCLUDEcdf)/cdflib.h
CDFMERGEh=$(INCLUDEcdf)/cdfmerge.h $(CDFTOOLSh) $(INCLUDEcdf)/cdflib.h
CDFVALIDATEh=$(INCLUDEcdf)/cdfvalidate.h $(CDFTOOLSh) $(INCLUDEcdf)/cdflib.h $(INCLUDEcdf)/cdflib64.h
CDFLEAPSECONDSINFOh=$(INCLUDEcdf)/cdfleapsecondsinfo.h $(CDFTOOLSh) $(INCLUDEcdf)/cdflib.h

CDFEDITobjs=cdfedit.o cdfedit1.o cdfedit2.o cdfedit3.o windoz.o widgets.o \
toolbox1.o toolbox3.o
CDFXPobjs=cdfxp.o cdfxp1.o cdfxp2.o windoz.o widgets.o toolbox1.o toolbox3.o
CDFCVTobjs=cdfcvt.o toolbox1.o toolbox3.o
CDF2SKTobjs=cdf2skt.o toolbox1.o toolbox3.o
SKT2CDFobjs=skt2cdf.o toolbox1.o toolbox3.o
CDFINQobjs=cdfinq.o toolbox1.o toolbox3.o
CDFSTATSobjs=cdfstats.o cdfstat1.o cdfstat2.o toolbox1.o toolbox3.o
CDFCMPobjs=cdfcmp.o toolbox1.o toolbox3.o
CDFDUMPobjs=cdfdump.o toolbox1.o toolbox3.o
CDFIRsDUMPobjs=cdfirsdump.o toolbox1.o toolbox3.o
CDFMERGEobjs=cdfmerge.o toolbox1.o toolbox3.o
CDFVALIDATEobjs=cdfvalidate.o toolbox1.o toolbox3.o
CDFLEAPSECONDSINFOobjs=cdfleapsecondsinfo.o toolbox1.o toolbox3.o

#------------------------------------------------------------------------------
# Compile/link.
#------------------------------------------------------------------------------

all: curses.$(CURSES) $(BINsNOTCURSES)

curses.yes: $(BINsCURSES)
curses.no:

cdfcvt: $(CDFCVTobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFCVTobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFCVTobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdf2skt: $(CDF2SKTobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDF2SKTobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDF2SKTobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

skt2cdf: $(SKT2CDFobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(SKT2CDFobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(SKT2CDFobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfinq: $(CDFINQobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFINQobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFINQobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfstats: $(CDFSTATSobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFSTATSobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFSTATSobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfcmp: $(CDFCMPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFCMPobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFCMPobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfdump: $(CDFDUMPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFDUMPobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFDUMPobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfleapsecondsinfo: $(CDFLEAPSECONDSINFOobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFLEAPSECONDSINFOobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFLEAPSECONDSINFOobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfirsdump: $(CDFIRsDUMPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFIRsDUMPobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFIRsDUMPobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfmerge: $(CDFMERGEobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFMERGEobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFMERGEobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfvalidate: $(CDFVALIDATEobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFVALIDATEobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFVALIDATEobjs) $(LIBs1)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfedit: $(CDFEDITobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFEDITobjs) $(LIBs2)
	      $(CC) $(CFLAGSld) -o $@ $(CDFEDITobjs) $(LIBs2)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

cdfxp: $(CDFXPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFXPobjs) $(LIBs2)
	      $(CC) $(CFLAGSld) -o $@ $(CDFXPobjs) $(LIBs2)
	if [ $(TORPATH) -eq 1 ] ; then \
	  echo install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	  install_name_tool -change "libcdf.dylib" "@executable_path/../lib/libcdf.dylib" $@ ; \
	fi

.c.o:
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $<
	      $(CC) $(CFLAGS) $(CURSESINC) -c $<

cdf2skt.o: $(SRCDIR)/cdf2skt.c $(CDF2SKTh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdf2skt.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdf2skt.c
cdfcmp.o: $(SRCDIR)/cdfcmp.c $(CDFCMPh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcmp.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcmp.c
cdfcvt.o: $(SRCDIR)/cdfcvt.c $(CDFCVTh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcvt.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcvt.c
cdfdump.o: $(SRCDIR)/cdfdump.c $(CDFDUMPh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfdump.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfdump.c
cdfirsdump.o: $(SRCDIR)/cdfirsdump.c $(CDFIRsDUMPh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfirsdump.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfirsdump.c
cdfmerge.o: $(SRCDIR)/cdfmerge.c $(CDFMERGEh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmerge.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmerge.c
cdfedit.o: $(SRCDIR)/cdfedit.c $(CDFEDITh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit.c
cdfedit1.o: $(SRCDIR)/cdfedit1.c $(CDFEDITh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit1.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit1.c
cdfedit2.o: $(SRCDIR)/cdfedit2.c $(CDFEDITh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit2.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit2.c
cdfedit3.o: $(SRCDIR)/cdfedit3.c $(CDFEDITh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit3.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfedit3.c
cdfinq.o: $(SRCDIR)/cdfinq.c $(CDFINQh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfinq.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfinq.c
cdfstats.o: $(SRCDIR)/cdfstats.c $(CDFSTATSh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstats.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstats.c
cdfstat1.o: $(SRCDIR)/cdfstat1.c $(CDFSTATSh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstat1.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstat1.c
cdfstat2.o: $(SRCDIR)/cdfstat2.c $(CDFSTATSh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstat2.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstat2.c
cdfxp.o: $(SRCDIR)/cdfxp.c $(CDFXPh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfxp.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfxp.c
cdfxp1.o: $(SRCDIR)/cdfxp1.c $(CDFXPh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfxp1.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfxp1.c
cdfxp2.o: $(SRCDIR)/cdfxp2.c $(CDFXPh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfxp2.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdfxp2.c
skt2cdf.o: $(SRCDIR)/skt2cdf.c $(SKT2CDFh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/skt2cdf.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/skt2cdf.c
cdfvalidate.o: $(SRCDIR)/cdfvalidate.c $(CDFVALIDATEh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalidate.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalidate.c
cdfleapsecondsinfo.o: $(SRCDIR)/cdfleapsecondsinfo.c $(CDFLEAPSECONDSINFOh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfleapsecondsinfo.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/cdfleapsecondsinfo.c
toolbox1.o: $(SRCDIR)/toolbox1.c $(CDFTOOLSh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/toolbox1.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/toolbox1.c
toolbox3.o: $(SRCDIR)/toolbox3.c $(CDFTOOLSh)
	@echo $(CC) $(CFLAGS) -c $(SRCDIR)/toolbox3.c
	      $(CC) $(CFLAGS) -c $(SRCDIR)/toolbox3.c
widgets.o: $(SRCDIR)/widgets.c $(WIDGETSh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/cdf2skt.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/widgets.c
windoz.o: $(SRCDIR)/windoz.c $(WINDOZh)
	@echo $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/windoz.c
	      $(CC) $(CFLAGS) $(CURSESINC) -c $(SRCDIR)/windoz.c

#------------------------------------------------------------------------------
# Clean/purge.
#------------------------------------------------------------------------------

clean:
	@-rm -f *.o
	@-rm -f *.exe
	@-rm -f cdfedit
	@-rm -f cdfxp
	@-rm -f cdfcvt
	@-rm -f skt2cdf
	@-rm -f cdf2skt
	@-rm -f cdfinq
	@-rm -f cdfstats
	@-rm -f cdfcmp
	@-rm -f cdfdump
	@-rm -f cdfirsdump
	@-rm -f cdfmerge
	@-rm -f cdfvalidate
	@-rm -f cdfleapsecondsinfo
	@-rm -f core

purge:
	@-rm -f *~
	@-rm -f *#
