#!/bin/sh
#
# combine source code into one module
# easier to compile -- no make file needed
#
# pretty generic script -- just echos, cats and greps.
#
#
srcdir=./
if [ ! -z $1 ] ; then
srcdir=$1
fi
PROG=cdilib.c
echo "combining source code into one module"
echo "output is ${PROG}"
#set -x

rm -f ${PROG}

DATE=`date +%F`

if test -f config.h ; then
   CDILIBVERSION=`grep PACKAGE_VERSION config.h | cut -f3 -d" "`
fi

cat > ${PROG} << EOR

/* Automatically generated by $USER at $DATE, do not edit */

/* CDILIB_VERSION=${CDILIBVERSION} */

#if  defined  (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>

#if  defined  (HAVE_LIBGRIB_API)
#  include <grib_api.h>
#endif

#if defined (HAVE_MMAP)
#  include <sys/mman.h> /* mmap() is defined in this header */
#endif

#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>
#endif

#if  defined  (HAVE_LIBSZ)
#  include <szlib.h>
#endif


//#undef _GET_X86_COUNTER
//#undef _GET_IBM_COUNTER
//#undef _ARCH_PWR6

#if   defined(__GNUC__) && (__GNUC__ >= 4)
#elif defined(__ICC)    && (__ICC >= 1100)
#else
#define DISABLE_SIMD
#endif

#ifdef DISABLE_SIMD
#ifndef ENABLE_AVX
#undef __AVX__
#endif
#ifndef ENABLE_SSE2
#undef __SSE2__
#endif
#endif

#ifdef _GET_IBM_COUNTER
#include <libhpc.h>
#endif

#ifdef __AVX__
#include <float.h>
#include <stdint.h>
#include <inttypes.h>
#include <immintrin.h>
#ifdef _GET_X86_COUNTER
#include <x86intrin.h>
#endif
#else
#ifdef __SSE2__
#include <float.h>
#include <stdint.h>
#include <inttypes.h>
#include <emmintrin.h>
#ifdef _GET_X86_COUNTER
#include <x86intrin.h>
#endif
#endif
#endif


#if ! defined (HAVE_CONFIG_H)
#  define  HAVE_LIBGRIB      1
#  define  HAVE_LIBCGRIBEX   1
#  define  HAVE_LIBSERVICE   1
#  define  HAVE_LIBEXTRA     1
#  define  HAVE_LIBIEG       1
#endif

EOR

c="dmemory.c         \
   dmemory.h         \
   error.c           \
   error.h           \
   stream_int.h      \
   taxis.c           \
   timebase.c        \
   calendar.c        \
   model.c           \
   institution.c     \
   table.c           \
   util.c            \
   gaussgrid.c       \
   varscan.c         \
   vlist.c           \
   vlist_att.c       \
   vlist_var.c       \
   basetime.c        \
   servicelib.c      \
   extralib.c        \
   ieglib.c          \
   grid.c            \
   zaxis.c           \
   cdf_int.c         \
   cdi_error.c       \
   cdi_util.c        \
   stream_int.c      \
   stream.c          \
   stream_history.c  \
   stream_cgribex.c  \
   stream_gribapi.c  \
   stream_grb.c      \
   stream_srv.c      \
   stream_ext.c      \
   stream_ieg.c      \
   stream_cdf.c      \
   stream_var.c      \
   stream_record.c   \
   tsteps.c          \
   file.c            \
   cgribexlib.c      \
   gribapi.c         \
   swap.c            \
   binary.c          \
   cdf.c             \
"

h="cdi_limits.h taxis.h dtypes.h file.h cgribex.h gribapi.h service.h extra.h \
   ieg.h cdi.h timebase.h calendar.h basetime.h datetime.h \
   stream_cgribex.h  stream_gribapi.h  stream_grb.h  stream_cdf.h \
   tablepar.h table.h gaussgrid.h grid.h varscan.h binary.h swap.h \
   service.h stream_srv.h stream_ext.h stream_ieg.h cdf_int.h  \
   cdf.h vlist.h"

#cat $h >> ${PROG}
#cat $c | grep -v '#include' | grep -v '#  include' >> ${PROG}

for hfile in $h ; do
  cat $srcdir/$hfile  >> ${PROG}
done

for cfile in $c ; do
  cat $srcdir/$cfile | grep -v '#include' | grep -v '#  include' >> ${PROG}
done


if test -f  config.h ; then
   cpp -P -DHAVE_CONFIG_H  -I./ $srcdir/version.c >>  ${PROG}
else
   echo "error: missing config.h"
fi

# Fortran interface (with -DHAVE_CF_INTERFACE)

echo "#if defined (HAVE_CF_INTERFACE)" >>  ${PROG}
echo "#undef realloc" >>  ${PROG}
echo "#undef malloc"  >>  ${PROG}
echo "#undef calloc"  >>  ${PROG}
echo "#undef free"    >>  ${PROG}
echo "#undef DOUBLE_PRECISION" >>  ${PROG}
cat $srcdir/cfortran.h   >>  ${PROG}
echo "#endif" >>  ${PROG}
cat $srcdir/cdiFortran.c >>  ${PROG}

exit
