#------------------------------------------------------------------------------
# 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=yes
CCx=gcc
COPTIONS=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -O2
COPTIONSld=
SYSLIBS=-lgcc
CURSESLIB=
MAKE=make
SRCDIR=
BLDDIR=

INCLUDEcdf= $(SRCDIR)/../include

WHICHOS=$(shell uname -a | cut -f1 -d" ")
LIBCDFb=
ifeq ("$(WHICHOS)","Darwin")
  LIBCDFa=$(BLDDIR)/../lib/libcdf.dylib
  LIBCDFb=-Wl,-rpath,@loader_path/../lib -L$(BLDDIR)/../lib
  LIBCDFc=-lcdf
  CFLAGS2=-I$(SRCDIR)/../../macosx/ncurses/include
else
  CFLAGS2=-I/usr/include/ncurses
  WHICHOS=$(shell uname -a | cut -f7 -d" ")
  ifeq ("$(WHICHOS)","Cygwin")
    LIBCDFa=$(BLDDIR)/../lib/libcdf.dll
    LIBCDFb=$(BLDDIR)/../lib/libcdf.dll
    LIBCDFc=-lcdf
  else
    ifeq ("$(WHICHOS)","Msys")
      LIBCDFa=$(BLDDIR)/../lib/libcdf.dll
      LIBCDFb=$(BLDDIR)/../lib/libcdf.dll
      LIBCDFc=-lcdf
    else
      LIBCDFa=$(BLDDIR)/../lib/libcdf.a
      LIBCDFb=$(BLDDIR)/../lib/libcdf.a
      LIBCDFc=
    endif
  endif
endif

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

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

BINsCURSES=$(BLDDIR)/cdfxp $(BLDDIR)/cdfedit 
BINsNOTCURSES=$(BLDDIR)/cdfcvt $(BLDDIR)/cdfstats $(BLDDIR)/cdfcmp \
              $(BLDDIR)/cdf2skt $(BLDDIR)/skt2cdf $(BLDDIR)/cdfinq \
              $(BLDDIR)/cdfdump $(BLDDIR)/cdfirsdump $(BLDDIR)/cdfmerge \
              $(BLDDIR)/cdfvalidate $(BLDDIR)/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=$(BLDDIR)/cdfedit.o $(BLDDIR)/cdfedit1.o $(BLDDIR)/cdfedit2.o \
            $(BLDDIR)/cdfedit3.o $(BLDDIR)/windoz.o $(BLDDIR)/widgets.o \
            $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFXPobjs=$(BLDDIR)/cdfxp.o $(BLDDIR)/cdfxp1.o $(BLDDIR)/cdfxp2.o \
          $(BLDDIR)/windoz.o $(BLDDIR)/widgets.o $(BLDDIR)/toolbox1.o \
          $(BLDDIR)/toolbox3.o
CDFCVTobjs=$(BLDDIR)/cdfcvt.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDF2SKTobjs=$(BLDDIR)/cdf2skt.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
SKT2CDFobjs=$(BLDDIR)/skt2cdf.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFINQobjs=$(BLDDIR)/cdfinq.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFSTATSobjs=$(BLDDIR)/cdfstats.o $(BLDDIR)/cdfstat1.o $(BLDDIR)/cdfstat2.o \
             $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFCMPobjs=$(BLDDIR)/cdfcmp.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFDUMPobjs=$(BLDDIR)/cdfdump.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFIRsDUMPobjs=$(BLDDIR)/cdfirsdump.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFMERGEobjs=$(BLDDIR)/cdfmerge.o $(BLDDIR)/toolbox1.o $(BLDDIR)/toolbox3.o
CDFVALIDATEobjs=$(BLDDIR)/cdfvalidate.o $(BLDDIR)/toolbox1.o \
                $(BLDDIR)/toolbox3.o
CDFLEAPSECONDSINFOobjs=$(BLDDIR)/cdfleapsecondsinfo.o $(BLDDIR)/toolbox1.o \
                       $(BLDDIR)/toolbox3.o

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

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

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


$(BLDDIR)/cdfcvt: $(CDFCVTobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFCVTobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFCVTobjs) $(LIBs1)

$(BLDDIR)/cdf2skt: $(CDF2SKTobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDF2SKTobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDF2SKTobjs) $(LIBs1)

$(BLDDIR)/skt2cdf: $(SKT2CDFobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(SKT2CDFobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(SKT2CDFobjs) $(LIBs1)

$(BLDDIR)/cdfinq: $(CDFINQobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFINQobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFINQobjs) $(LIBs1)

$(BLDDIR)/cdfstats: $(CDFSTATSobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFSTATSobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFSTATSobjs) $(LIBs1)

$(BLDDIR)/cdfcmp: $(CDFCMPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFCMPobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFCMPobjs) $(LIBs1)

$(BLDDIR)/cdfdump: $(CDFDUMPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFDUMPobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFDUMPobjs) $(LIBs1)

$(BLDDIR)/cdfleapsecondsinfo: $(CDFLEAPSECONDSINFOobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFLEAPSECONDSINFOobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFLEAPSECONDSINFOobjs) $(LIBs1)

$(BLDDIR)/cdfirsdump: $(CDFIRsDUMPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFIRsDUMPobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFIRsDUMPobjs) $(LIBs1)

$(BLDDIR)/cdfmerge: $(CDFMERGEobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFMERGEobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFMERGEobjs) $(LIBs1)

$(BLDDIR)/cdfvalidate: $(CDFVALIDATEobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFVALIDATEobjs) $(LIBs1)
	      $(CC) $(CFLAGSld) -o $@ $(CDFVALIDATEobjs) $(LIBs1)

$(BLDDIR)/cdfedit: $(CDFEDITobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFEDITobjs) $(LIBs2)
	      $(CC) $(CFLAGSld) -o $@ $(CDFEDITobjs) $(LIBs2)

$(BLDDIR)/cdfxp: $(CDFXPobjs) $(LIBCDFa)
	@echo $(CC) $(CFLAGSld) -o $@ $(CDFXPobjs) $(LIBs2)
	      $(CC) $(CFLAGSld) -o $@ $(CDFXPobjs) $(LIBs2)

$(BLDDIR)/%.o: %.c 
	@if [ "$<" = "windoz.c" ] ; then \
	 echo $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ ; \
	      $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ ; \
	else \
	 echo $(CC) $(CFLAGS) -c $< -o $@ ; \
	      $(CC) $(CFLAGS) -c $< -o $@ ; \
	fi

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

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

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

purge:
	@-rm -f $(BLDDIR)/*~
	@-rm -f $(BLDDIR)/*#
