#!/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`

config_file="${srcdir}/../configure.ac"
if test -f "$config_file" ; then
   CDILIBVERSION=\"`sed -n '/^AC_INIT(/s/AC_INIT *( *[^,]*,[[ ]*\([^],]*\).*/\1/ p' "$config_file"`\"
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} */

#if defined(_WIN32) || defined(_WIN64)
#define restrict
#define ssize_t long
#elif ! defined HAVE_CONFIG_H
#define HAVE_UNISTD_H
#endif

#ifdef _ARCH_PWR6
#pragma options nostrict
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif

#ifdef  HAVE_UNISTD_H
#include <unistd.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 <stdbool.h>
#include <assert.h>

#ifdef HAVE_LIBGRIB_API
#include <grib_api.h>
#endif

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

#ifdef HAVE_LIBPTHREAD
#include <pthread.h>
#endif

#ifdef HAVE_LIBSZ
#include <szlib.h>
#endif

#ifndef HAVE_CONFIG_H
#define  HAVE_LIBGRIB      1
#define  HAVE_LIBCGRIBEX   1
#define  HAVE_LIBSERVICE   1
#define  HAVE_LIBEXTRA     1
#define  HAVE_LIBIEG       1
#define  CDI              -1
#endif

EOR

files="async_worker.c \
   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_datetime.c \
   cdi_int.c \
   cdi_query.c \
   cdi_util.c \
   cgribexlib.c \
   cksum.c \
   dmemory.c \
   error.c \
   extralib.c \
   file.c \
   gaussian_latitudes.c \
   get_num_missvals.c \
   gribapi.c \
   gribapi_utilities.c \
   grid.c \
   ieglib.c \
   input_file.c \
   institution.c \
   iterator.c \
   iterator_fallback.c \
   iterator_grib.c \
   julian_date.c \
   model.c \
   namespace.c \
   referenceCounting.c \
   resource_handle.c \
   serialize.c \
   servicelib.c \
   stream_scan.c \
   stream.c \
   stream_write.c \
   stream_read.c \
   stream_cdf_i.c \
   stream_cdf_o.c \
   stream_cdf_time.c \
   stream_cgribex.c \
   stream_ext.c \
   stream_grb.c \
   stream_gribapi.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 \
   tsteps.c \
   util.c \
   varscan.c \
   vlist.c \
   cdi_key.c \
   cdi_att.c \
   vlist_var.c \
   vlist_var_pack.c \
   vlist_var_key.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
                curFilePath="$srcdir/$curFile"
                if [ "$curFile" = "config.h" ] ; then curFilePath="$curFile" ; fi
                #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 "$curFilePath") <$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-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 `if test -f cfortran.h; then echo cfortran.h; else echo $srcdir/cfortran.h; fi;` >> ${PROG}
echo "#endif" >>  ${PROG}
cat `if test -f cdiFortran.c; then echo cdiFortran.c; else echo $srcdir/cdiFortran.c; fi;` >> ${PROG}

exit
