# REQUIRES GMAKE!!!!
#
# makefile for wgrib2
# 
# compiles every #@?! library needed by wgrib2
# then tries to compile wgrib2
#
# (1) must use gnu-make
# (2) the environment variable FC must be set to fortran-90 compiler or 
#        higher in order to compile netcdf and the optional IPOLATES
#        not needed if netcdf and IPOLATES is not used
# (3) the environment veriable CC must be set to the C compiler
#
#
# mod 1/07 M. Schwarb (libgrib2c name change)
# mod 4/09 W. Ebisuzaki (use config.h)
# mod 6/10 W. Ebisuzaki ipolates
# mod 8/11 W. Ebisuzaki support environment variable FC={fortran 90+ compiler}
#              needed by optional netcdf4 and ipolates
# mod 3/12 W. Ebisuzaki support openmp, gctpc
# mod 8/12 M. Schwarb  gunzip -n -f,  cd "$var"
# mod 10/12 W. Ebisuzaki
# mod 7/13 W. Ebisuzaki added got netcdf4 working again, added subdirectroy lib, bin, include, man
#
#   Optional modules:
#
# NETCDF3: link in netcdf3 library to write netcdf3 files
#    change: USE_NETCDF3=1 and USE_NETCDF4=0 in configuration below
#
# NETCDF4: link in netcdf4 library to write netcdf3/4 files
#    change: USE_NETCDF3=0 and USE_NETCDF4=1 in configuration below
#    need to download netcdf4 and hdf5 libraries and to put into grib2 directory
#    need to define environment variable FC to be the command for the fortran compiler
#
# IPOLATES: link in IPOLATES library to interpolate to new grids
#    change: USE_IPOLATES=1 in configuration below
#    need to define environment variable FC to be the command for the fortran compiler
#    need to modify makefile and perhaps source code
#
#  MYSQL: link in interface to MySQL to write to mysql database
#    change: USE_MYSQL=1 in configuration below
#    need to have mysql installed
#    may need to modify makefile
#
#  UDF: add commands for user-defined functions and shell commands
#    change: USE_UDF=1 in configuration below
#
#  REGEX: use regular expression library, on by default
#    change: USE_REGEX=0 to turn off (configuration below)
#
#  TIGGE: ability for TIGGE-like variable names, on by default
#    change: USE_TIGGE=0 to turn off (configuration below)
#
#  USE_PROJ4: right now the proj4 library is used to confirm that the
#    gctpc code is working right and is not very useful. This may change.
#
#  USE_G2CLIB: include NCEP's g2clib (for testing purposes)
#
#  To turn off PNG support
#  USE_PNG=0
#  USE_G2CLIB=0
#
#  To turn off JPEG200 support
#  USE_JASPER
#  USE_G2CLIB=0
#
# on NCEP AIX
# export CC=/usr/vacpp/bin/xlc_r
# export CPP=/usr/bin/cpp
# export FC=xlf_r
#
# for clang
# export CC=clang
# export FC="gfortran -fplugin=dragonegg"
#
# for OS-X: uncomment line for makefile -f scripts/makefile.darwin
#
SHELL=/bin/sh

# 
# netcdf3: write netcdf files with netcdf-3 library
# netcdf4: write netcdf files with netcdf-4 library
# regex: regular expression package used by (match,not), POSIX-2
# tigge: enable -tigge option for tigge names
# mysql: write to mysql files
# ipolates: fortran interpolation library
# udf: user defined functions
# openmp: multicore support using OpenMP
# wmo_validation: used for testing new templates
#
# the flags are stored in wgrib2/config.h
#

# Warning do not set both USE_NETCDF3 and USE_NETCDF4 to one
USE_NETCDF3=1
USE_NETCDF4=0
USE_REGEX=1
USE_TIGGE=1
USE_MYSQL=0
USE_IPOLATES=1
USE_UDF=0
USE_OPENMP=1
USE_PROJ4=0
USE_G2CLIB=1
USE_WMO_VALIDATION=0

USE_PNG=1
USE_JASPER=1

# Add any customization comments, appears in help and config pages
BUILD_COMMENTS=HWRF interpolation

# often enviroment variable FC=fortran compiler, is set to f77, need f90 compiler
# FC=gfortran
# CC=gcc

ifeq ($(USE_PNG),0)
  ifeq ($(USE_G2CLIB),1)
    $(error ERROR, USE_G2CLIB = 1 and USE_PNG = 0 are incompatible)
  endif
endif

ifeq ($(USE_JASPER),0)
  ifeq ($(USE_G2CLIB),1)
    $(error ERROR, USE_G2CLIB = 1 and USE_JASPER = 0 are incompatible)
  endif
endif

ifeq ($(USE_NETCDF3),1)
  ifeq ($(USE_NETCDF4),1)
    $(error ERROR, USE_NETCDF3 = 1 or USE_NETCDF4 = 1, not both)
  endif
endif

ifeq ($(USE_NETCDF4),1)
  ifeq ($(FC),)
    $(error ERROR, USE_NETCDF4 = 1: must set fortran90 compiler by environement variable FC)
  endif
endif

ifeq ($(USE_IPOLATES),1)
  ifeq ($(FC),)
    $(error ERROR, USE_IPOLATES = 1: must set fortran90 compiler by environement variable FC)
  endif
endif


# wCPPFLAGS has the directory of the includes 
# wLDFLAGS has the directory/name of the library

ifeq ($(findstring gcc,$(notdir $(CC))),gcc)
   wCPPFLAGS+=-Wall -Wmissing-prototypes -Wold-style-definition -Werror=format-security -ffast-math  -O3 -g
   hdf5CFLAGS+=-Wall -Wmissing-prototypes -Wold-style-definition -O1 -g
endif
ifeq ($(findstring opencc,$(notdir $(CC))),opencc)
   wCPPFLAGS+=-O3 -Wall -ffast-math -opencc
   hdf5CFLAGS+=-O1 -Wall -opencc
endif
ifeq ($(findstring icc,$(notdir $(CC))),icc)
   wCPPFLAGS+=-O2
   hdf5CFLAGS+=-O2
   $(error ERROR, makefile does not make jasper correctly with intel compiler)
endif
ifeq ($(findstring pgcc,$(notdir $(CC))),pgcc)
   wCPPFLAGS+=-O2
   hdf5CFLAGS+=-O2
   $(error ERROR, makefile does not make jasper correctly with portland compiler)
endif
ifeq ($(findstring xlc_r,$(notdir $(CC))),xlc_r)
   wCPPFLAGS+=-O3
   hdf5CFLAGS+=-O2
endif
ifeq ($(findstring clang,$(notdir $(CC))),clang)
   wCPPFLAGS+=-O3
   hdf5CFLAGS+=-O2
endif

ifndef wCPPFLAGS
   wCPPFLAGS+=-O3
   hdf5CFLAGS+=-O2
endif

cwd:=${CURDIR}
lib:=${cwd}/lib
wLDFLAGS:=-L${lib}
a:=$(shell mkdir -p ${lib})
wCPPFLAGS+=-I${cwd}/include

CONFIG_H=${cwd}/wgrib2/config.h
a:=$(shell echo "/* config.h */" > ${CONFIG_H})

ifeq ($(USE_REGEX),1)
   a:=$(shell echo "\#define USE_REGEX" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_REGEX" >> ${CONFIG_H})
endif

ifeq ($(USE_TIGGE),1)
   a:=$(shell echo "\#define USE_TIGGE" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_TIGGE" >> ${CONFIG_H})
endif

# grib2c library

ifeq ($(USE_PNG),1)
   g:=${cwd}/g2clib-1.4.0
   glib:=${lib}/libgrib2c.a
   wLDFLAGS+=-lgrib2c
   wCPPFLAGS+=-I$g
else
   ifeq ($(USE_JASPER),1)
      g:=${cwd}/g2clib-1.4.0
      glib:=${lib}/libgrib2c.a
      wLDFLAGS+=-lgrib2c
      wCPPFLAGS+=-I$g
   endif
endif

ifeq ($(USE_G2CLIB),1)
   a:=$(shell echo "\#define USE_G2CLIB" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_G2CLIB" >> ${CONFIG_H})
endif

# gctpc library
gctpc:=${cwd}/gctpc
gctpcsrc:=gctpc20a.tgz
gctpclib:=${lib}/libgeo.a
wLDFLAGS+=-lgeo
# wCPPFLAGS+=-I${gctpc}/source

# proj4 library
ifeq ($(USE_PROJ4),1)
   proj4:=${cwd}/proj-4.8.0
   proj4src:=${cwd}/proj-4.8.0.tar.gz
   proj4lib:=${lib}/libproj.a
   wLDFLAGS+=-lproj
#   wCPPFLAGS+=-I${proj4}/src
   a:=$(shell echo "\#define USE_PROJ4" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_PROJ4" >> ${CONFIG_H})
endif

# Jasper

ifeq ($(USE_JASPER),1)
   j=${cwd}/jasper-1.900.1
#   jsrc=jasper_1.900.1-10ubuntu1.debian.tgz
   jsrc=jasper-fedora19.tgz
   jlib=${lib}/libjasper.a
   wLDFLAGS+=-ljasper
# wCPPFLAGS+=-I$j/src/libjasper/include
   a:=$(shell echo "\#define USE_JASPER" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_JASPER" >> ${CONFIG_H})
endif


ifeq ($(USE_NETCDF3),1)
   n:=${cwd}/netcdf-3.6.3
   netcdfsrc=netcdf-3.6.3.tar.gz
   nlib:=${lib}/libnetcdf.a
   wLDFLAGS+=-lnetcdf
#   wCPPFLAGS+=-I$n/libsrc
   a:=$(shell echo "\#define USE_NETCDF3" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_NETCDF3" >> ${CONFIG_H})
endif

ifeq ($(USE_NETCDF4),1)
   n4:=${cwd}/netcdf-4.3.0
   netcdf4src=netcdf-4.3.0.tar.gz
   n4lib:=${lib}/libnetcdf.a
   h5:=${cwd}/hdf5-1.8.11
   hdf5src:=hdf5-1.8.11.tar.gz
   h5lib:=${lib}/libhdf5.a
   wLDFLAGS+=-lnetcdf -lhdf5_hl -lhdf5 -ldl
#   wCPPFLAGS+=-I${n4}/include -I${h5}/src -I${h5}/hl/src
   a:=$(shell echo "\#define USE_NETCDF4" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_NETCDF4" >> ${CONFIG_H})
endif

ifeq ($(USE_MYSQL),1)
   wCPPFLAGS+=`mysql_config --cflags`
   wLDFLAGS+=`mysql_config --libs`
   a:=$(shell echo "\#define USE_MYSQL" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_MYSQL" >> ${CONFIG_H})
endif

ifeq ($(USE_IPOLATES),1)
   ip:=${cwd}/iplib_hwrf
   iplib:=${lib}/libipolate_hwrf.a
   wLDFLAGS+=-lipolate_hwrf

#  for compiling with fortran library
#  wLDFLAGS+= (libraries need by the fortran code)
#  wCPPFLAGS+= -D(FORTRAN Name)   see New_grid.c

# for G95 - personal system
   ifeq ($(findstring g95,$(notdir $(FC))),g95)
#      wLDFLAGS+=-L/export/cpc-lw-webisuzak/wd51we/g95-install/lib/gcc-lib/i686-unknown-linux-gnu/4.0.3 -lf95
      wLDFLAGS+=-L/export/cpc-lw-webisuzak/wd51we/g95-install_64/lib/gcc-lib/x86_64-unknown-linux-gnu/4.0.3 -lf95
      wCPPFLAGS+=-DG95
      wFFLAGS+=-O2
    endif

# for gfortran - ubuntu and cygwin 1.7.7-1
   ifeq ($(findstring gfortran,$(notdir $(FC))),gfortran)
      wLDFLAGS+=-lgfortran
      wCPPFLAGS+=-DGFORTRAN
      wFFLAGS+=-O2
   endif

# for open64 fortran - personal system
   ifeq ($(findstring openf95,$(notdir $(FC))),openf95)
      wLDFLAGS+=/export/cpc-lw-webisuzak/wd51we/opt/x86_open64-4.5.1/lib/gcc-lib/x86_64-open64-linux/4.5.1/libfortran.a
      wLDFLAGS+=/export/cpc-lw-webisuzak/wd51we/opt/x86_open64-4.5.1/lib/gcc-lib/x86_64-open64-linux/4.5.1/libffio.a
      wCPPFLAGS+=-DOPENF95
      wFFLAGS+=-O2
   endif

# for portland f95
   ifeq ($(notdir $(FC)),pgf95)
#      wLDFLAGS+=/export/cpc-lw-webisuzak/wd51we/opt/x86_open64-4.2.5.1/lib/gcc-lib/x86_64-open64-linux/4.2.5.1/libfortran.a
#      wLDFLAGS+=/export/cpc-lw-webisuzak/wd51we/opt/x86_open64-4.2.5.1/lib/gcc-lib/x86_64-open64-linux/4.2.5.1/libffio.a
      wCPPFLAGS+=-DPGF95
      wFFLAGS+=-O2
   endif

# intel fortran
   ifeq ($(notdir $(FC)),ifort)
      wCPPFLAGS+=-DIFORT -cxxlib
      wLDFLAGS+=-lifcore -lc -limf -lintlc
      wFFLAGS+=-O2 -nofor_main  -cxxlib
   endif

# NCEP CCS:
   ifeq ($(findstring xlf_r,$(notdir $(FC))),xlf_r)
      wLDFLAGS+=-L/usr/lib - -lxlf90_r
      wCPPFLAGS+=-DXLF
      wFFLAGS+=-O2
   endif

   ifndef wFFLAGS
      $(error ERROR, did not recognize fortran compiler, cannot use IPOLATES, set FC=f90 compiler)
   endif

   a:=$(shell echo "\#define USE_IPOLATES" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_IPOLATES" >> ${CONFIG_H})
endif


ifeq ($(USE_UDF),1)
   a:=$(shell echo "\#define USE_UDF" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define USE_UDF" >> ${CONFIG_H})
endif

# OPENMP .. only select configurations

ifeq ($(USE_OPENMP),1)
   ifeq ($(findstring gcc,$(notdir $(CC))),gcc)
      ifeq ($(findstring gfortran,$(notdir $(FC))),gfortran)
	 a:=$(shell echo "\#define USE_OPENMP" >> ${CONFIG_H})
	 wCPPFLAGS+=-fopenmp
	 wFFLAGS+=-fopenmp
      endif
   endif
   ifeq ($(findstring opencc,$(notdir $(CC))),opencc)
      ifeq ($(findstring openf95,$(notdir $(FC))),openf95)
	 a:=$(shell echo "\#define USE_OPENMP" >> ${CONFIG_H})
	 wCPPFLAGS+=-fopenmp
	 wFFLAGS+=-fopenmp
      endif
   endif
   ifeq ($(notdir $(CC)),icc)
      ifeq ($(notdir $(FC)),ifort)
	 a:=$(shell echo "\#define USE_OPENMP" >> ${CONFIG_H})
	 wCPPFLAGS+=-openmp
	 wFFLAGS+=-openmp
      endif
   endif
   ifeq ($(findstring xlc_r,$(notdir $(CC))),xlc_r)
      ifeq ($(findstring xlf_r,$(notdir $(FC))),xlf_r)
	 a:=$(shell echo "\#define USE_OPENMP" >> ${CONFIG_H})
	 wCPPFLAGS+=-qsmp=omp
	 wFFLAGS+=-qsmp=omp
      endif
   endif
endif


# save fortran and C compiler names in config.h file

a:=$(shell echo "\#define CC \"${CC}\"" >> ${CONFIG_H})
a:=$(shell echo "\#define FORTRAN \"${FC}\"" >> ${CONFIG_H})
a:=$(shell echo "\#define BUILD_COMMENTS \"${BUILD_COMMENTS}\"" >> ${CONFIG_H})

# png 

ifeq ($(USE_PNG),1)
   p=${cwd}/libpng-1.2.50
   psrc=${cwd}/libpng-1.2.50.tar.gz
   plib=${lib}/libpng.a
   wLDFLAGS+=-lpng
# wCPPFLAGS+=-I$p
   a:=$(shell echo "\#define USE_PNG" >> ${CONFIG_H})

# z

   z=${cwd}/zlib-1.2.8
   zlib=${lib}/libz.a
   wLDFLAGS+=-lz
   # wCPPFLAGS+=-I$z
else
   a:=$(shell echo "//\#define USE_PNG" >> ${CONFIG_H})
endif

# WMO Validation testing mode
ifeq ($(USE_WMO_VALIDATION),1)
   a:=$(shell echo "\#define WMO_VALIDATION" >> ${CONFIG_H})
else
   a:=$(shell echo "//\#define WMO_VALIDATION" >> ${CONFIG_H})
endif



wLDFLAGS+=-lm
wCPPFLAGS+=-I/usr/include ${CPPFLAGS}

# -----------------------------------------------------

# check if make is GNU make else use gmake
make_is_gnu:=$(word 1,$(shell make -v))
ifeq ($(make_is_gnu),GNU)
   MAKE:=make
else
   MAKE:=gmake
endif


w=wgrib2
prog=$w/wgrib2

all:	${netcdf4src} ${hdf5src} ${prog} aux_progs/gmerge aux_progs/smallest_grib2 aux_progs/smallest_4


${prog}:        $w/*.c $w/*.h ${jlib} ${nlib} ${zlib} ${plib} ${h5lib} ${glib} ${n4lib} ${iplib} ${gctpclib} ${proj4lib}
	cd "$w" && export LDFLAGS="${wLDFLAGS}" && export CPPFLAGS="${wCPPFLAGS}" && ${MAKE}

fast:        $w/*.c $w/*.h ${jlib} ${nlib} ${zlib} ${plib} ${h5lib} ${glib} ${n4lib} ${iplib} ${gctpclib} ${proj4lib}
	cd "$w" && export LDFLAGS="${wLDFLAGS}" && export CPPFLAGS="${wCPPFLAGS}" && ${MAKE} fast


${jlib}:
	cp ${jsrc}  tmpj.tar.gz
	gunzip -n -f tmpj.tar.gz
	tar -xvf tmpj.tar
	rm tmpj.tar
	cd "$j" && export CFLAGS="${wCPPFLAGS}" && ./configure --without-x --disable-libjpeg --disable-opengl --prefix=${cwd} && ${MAKE} check install

${plib}:	${zlib}
	cp ${psrc} tmpp.tar.gz
	gunzip -n -f tmpp.tar.gz
	tar -xvf tmpp.tar
	rm tmpp.tar
#       for OSX
#	export LDFLAGS="-L$z" && cd "$p" && export CPPFLAGS="${wCPPFLAGS}" && make -f scripts/makefile.darwin
#	for everybody else
	export LDFLAGS="-L${lib}" && cd "$p" && export CPPFLAGS="${wCPPFLAGS}" && ./configure --disable-shared --prefix=${cwd} && ${MAKE} check install

${zlib}:
	cp $z.tar.gz tmpz.tar.gz
	gunzip -f tmpz.tar.gz
	tar -xvf tmpz.tar
	rm tmpz.tar
	cd "$z" && export CFLAGS="${wCPPFLAGS}" && ./configure --prefix=${cwd} && ${MAKE} check install


${glib}:	${jlib} ${plib} ${zlib}
	touch ${glib}
	rm ${glib}
	cd "$g" && export CPPFLAGS="${wCPPFLAGS}" && ${MAKE} && cp libgrib2c.a ${lib}

${gctpclib}:
	cp ${gctpcsrc} tmpgctpc.tar.gz
	gunzip -n -f tmpgctpc.tar.gz
	tar -xvf tmpgctpc.tar
	rm tmpgctpc.tar
	cp makefile.gctpc proj.h ${gctpc}/source/
	cd "${gctpc}/source" && export CPPFLAGS="${wCPPFLAGS}" && ${MAKE} -f makefile.gctpc
	cp ${gctpc}/source/libgeo.a ${lib}
	cp ${gctpc}/source/proj.h ${cwd}/include

${proj4lib}:
	cp ${proj4src}  tmpproj4.tar.gz
	gunzip -f tmpproj4.tar.gz
	tar -xvf tmpproj4.tar
	rm tmpproj4.tar
	cd ${proj4} && ./configure --disable-shared --prefix=${cwd} && ${MAKE} check install

${nlib}:
	cp ${netcdfsrc} tmpn.tar.gz
	gunzip -f tmpn.tar.gz
	tar -xvf tmpn.tar
	rm tmpn.tar
	cd $n && export CPPFLAGS="${wCPPFLAGS}" && ./configure --enable-c-only --prefix=${cwd} && ${MAKE} check install

${n4lib}:	${zlib} ${netcdf4src} ${h5lib}
	cp ${netcdf4src} tmpn4.tar.gz
	gunzip -n -f tmpn4.tar.gz
	tar -xvf tmpn4.tar
	rm tmpn4.tar
	cd "${n4}" && export CPPFLAGS="${wCPPFLAGS}" && export LDFLAGS="-L${lib}" && export LIBS="-lhdf5 -ldl" && ./configure --disable-fortran --disable-cxx --disable-dap --enable-netcdf-4 --prefix=${cwd} --disable-shared && ${MAKE} install

${netcdf4src}:
	$(error ERROR, get netcdf4 source by "wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.3.0.tar.gz" )

${h5lib}:	${hdf5src}
	cp ${hdf5src} tmph5.tar.gz
	gunzip -n -f tmph5.tar.gz
	tar -xvf tmph5.tar
	rm tmph5.tar
	cd "${h5}" && export CFLAGS="${hdf5CFLAGS}" && export LDFLAGS="${LDFLAGS}" && ./configure --disable-shared --with-zlib=$z --prefix=${cwd} && ${MAKE} all check install


${hdf5src}:
	$(error ERROR, get hdf5 source by "wget http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8.11/src/hdf5-1.8.11.tar.gz" )

${iplib}:
	cd "${ip}" && export FFLAGS="${wFFLAGS}" && ${MAKE} && cp libipolate.a ${iplib}

aux_progs/gmerge:	aux_progs/gmerge.c		
	cd aux_progs && ${MAKE} -f gmerge.make

aux_progs/smallest_grib2:	aux_progs/smallest_grib2.c
	cd aux_progs && ${MAKE} -f smallest_grib2.make

aux_progs/smallest_4:	aux_progs/smallest_4.c
	cd aux_progs && ${MAKE} -f smallest_4.make

clean:
	[ -d "${lib}" ] && rm -r ${lib}
	[ -d "${cwd}/bin" ] && rm -r ${cwd}/bin
	[ -d "${cwd}/include" ] && rm -r ${cwd}/include
	[ -d "${cwd}/man" ] && rm -r ${cwd}/man
	cd $w && ${MAKE} clean
	cd $g && touch junk.a junk.o && rm *.o *.a
	[ -d "$j" ] && rm -rf $j
	[ -d "$p" ] && rm -rf $p
	[ -d "$z" ] && rm -rf $z
	[ -d "${gctpc}" ] && rm -rf ${gctpc}
ifeq ($(USE_IPOLATES),1)
	[ -d "${ip}" ] && touch ${ip}/junk.o ${ip}/junk.a && rm ${ip}/*.o ${ip}/*.a
endif
ifeq ($(USE_NETCDF3),1)
	[ -d "$n" ] && rm -rf $n
endif
ifeq ($(USE_NETCDF4),1)
	[ -d "${n4}" ] && rm -rf ${n4}
	[ -d "${h5}" ] && rm -rf ${h5}
endif
ifeq ($(USE_PROJ4),1)
	[ -d "${proj4}" ] && rm -rf ${proj4}
endif
	cd aux_progs && ${MAKE} clean -f gmerge.make
	cd aux_progs && ${MAKE} clean -f smallest_grib2.make
	cd aux_progs && ${MAKE} clean -f smallest_4.make
