#!/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 ../configure.ac ; then
   CDILIBVERSION=\"`sed -n '/^AC_INIT(/s/AC_INIT *( *[^,]*,[[ ]*\([^],]*\).*/\1/ p' ../configure.ac`\"
else
   echo "error: cannot find configure.ac" >&2
   exit 1
fi

cat > ${PROG} << EOR

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

/* CDILIB_VERSION=${CDILIBVERSION} */

#ifdef _ARCH_PWR6
#pragma options nostrict
#endif

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

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#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>
#ifndef _SX
#include <aio.h>
#endif
#include <stdbool.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

#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

files="basetime.c \
   binary.c \
   calendar.c \
   cdf.c \
   cdf_int.c \
   cdf_util.c \
   cdf_lazy_grid.c \
   cdi_cksum.c \
   cdi_error.c \
   cdi_int.c \
   cdi_util.c \
   cgribexlib.c \
   cksum.c \
   dmemory.c \
   error.c \
   extralib.c \
   file.c \
   gaussgrid.c \
   gribapi.c \
   gribapi_utilities.c \
   grid.c \
   ieglib.c \
   input_file.c \
   institution.c \
   iterator.c \
   iterator_fallback.c \
   iterator_grib.c \
   model.c \
   namespace.c \
   referenceCounting.c \
   resource_handle.c \
   serialize.c \
   servicelib.c \
   stream.c \
   stream_write.c \
   stream_read.c \
   stream_cdf_i.c \
   stream_cdf_o.c \
   stream_cgribex.c \
   stream_ext.c \
   stream_grb.c \
   stream_gribapi.c \
   stream_history.c \
   stream_ieg.c \
   stream_record.c \
   stream_srv.c \
   stream_var.c \
   grb_write.c \
   grb_read.c \
   cdf_write.c \
   cdf_read.c \
   subtype.c \
   swap.c \
   table.c \
   taxis.c \
   timebase.c \
   tsteps.c \
   util.c \
   varscan.c \
   vlist.c \
   vlist_att.c \
   vlist_var.c \
   zaxis.c"

car () {
    echo "$1"
}
cdr () {
    shift
    echo "$@"
}
listIncludes () {
    grep '^ *# *include  *"' "$1" | sed 's/^ *# *include  *"\(.*\)".*$/\1/'
}

scanlist="$files"
fileList=
until test "foo$scanlist" = "foo" ; do
    curFile="$(car $scanlist)"
    scanlist="$(cdr $scanlist)"
    case $curFile in
        (\<*\>)
            fileList="$fileList $(echo "$curFile" | sed 's/<\(.*\)>/\1/')"
            ;;
        (*)
            if echo "$fileList" | grep -q '\<'"$curFile"'\>' ; then
                true    #Nothing to do, we have already scanned this header.
            else
                #Prepend the includes of the current header to the scanlist so that we will scan them in the order that the preprocessor would.
                scanlist="$(listIncludes "$curFile") <$curFile> $scanlist"
            fi
            ;;
    esac
done

echo file list:
echo $fileList

for file in $fileList ; do
  if [ "$file" = "config.h" ] ; then
    echo "skipped file: $file"
  else
    cat $srcdir/$file | grep -v '^ *# *include  *"'  >> ${PROG}
  fi
done


cpp -P -DVERSION="${CDILIBVERSION}" -DCDI_H_ \
  $srcdir/version.c >>${PROG}

# 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
