#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "grb2.h"
#include "wgrib2.h"
#include "fnlist.h"

#if defined USE_NETCDF3 || defined USE_NETCDF4
#include <netcdf.h>
#endif

/*
 * Config.c  just prints out the configuration
 *
 * 3/2009 public domain Wesley Ebisuzaki
 */

extern const char *default_vectors[];

/*
 * HEADER:100:config:misc:0:shows the configuration
 */
int f_config(ARG0) {

    char *filename;
    FILE *input;
#ifdef USE_IPOLATES
    int i;
#endif

    inv_out[0] = 0;
    strcat(inv_out, "wgrib2 " WGRIB2_VERSION "\n    " BUILD_COMMENTS "\n\n");

    inv_out += strlen(inv_out);
    sprintf(inv_out,"Compiled on %s %s\n\n",__TIME__,__DATE__);

#if defined USE_NETCDF3 || defined USE_NETCDF4
    strcat(inv_out, "Netcdf package: ");
    strcat(inv_out,  nc_inq_libvers());
    strcat(inv_out, " is installed\n");
#else
    strcat(inv_out, "Netcdf package is not installed\n");
#endif


#ifdef USE_MYSQL
    strcat(inv_out, "mysql package is installed\n");
#else
    strcat(inv_out, "mysql package is not installed\n");
#endif


#ifdef USE_REGEX
    strcat(inv_out, "regex package is installed\n");
#else
    strcat(inv_out, "regex package is not installed\n");
#endif


#ifdef USE_TIGGE
    strcat(inv_out, "tigge package is installed\n");
#else
    strcat(inv_out, "tigge package is not installed\n");
#endif

#ifdef USE_IPOLATES
    strcat(inv_out, "IPOLATES " IPOLATES_LIB " is installed, default vectors:\n    ");
    i = 0;
    while (default_vectors[i] != NULL) {
	if ( (i % 15 == 14)) strcat(inv_out, "\n    ");
        strcat(inv_out, default_vectors[i]);
        strcat(inv_out, i & 1 ? " " : "/");
        i++;
    }
    strcat(inv_out, "\n");
#else
    strcat(inv_out, "interpolation package is not installed\n");
#endif

strcat(inv_out, "Geolocation library status (by search order)\n");
#ifdef USE_PROJ4
#if (DEFAULT_PROJ4 == 0)
    strcat(inv_out, "  Proj4 geolocation is installed but not enabled\n");
#else
    strcat(inv_out, "  Proj4 geolocation is installed and is primary geolocation library\n");
#endif
#endif
#if (DEFAULT_GCTPC == 1)
    strcat(inv_out, "  gctpc geolocation is enabled\n"); 
#else
    strcat(inv_out, "  gctpc geolocation is disabled\n");
#endif
strcat(inv_out, "  spherical geolocation is enabled\n"); 


#ifdef USE_UDF
    strcat(inv_out, "UDF package is installed\n");
#else
    strcat(inv_out, "UDF package is not installed\n");
#endif

#ifdef N_ARGLIST
    inv_out += strlen(inv_out);
    sprintf(inv_out, "maximum number of arguments on command line: %d\n",
	N_ARGLIST);
#else
    inv_out += strlen(inv_out);
    sprintf(inv_out, "maximum number of arguments on command line: limited by shell/OS\n");
#endif

#ifdef USE_REGEX
    inv_out += strlen(inv_out);
    sprintf(inv_out, "maximum number of -match,-not,-if, and -not_if options: %d\n", MATCH_MAX);
#endif
    inv_out += strlen(inv_out);
    sprintf(inv_out, "maximum number of -match_fs,-not_fs,-if_fs, and -not_if_fs options: %d\n", MATCH_MAX);
    inv_out += strlen(inv_out);
    sprintf(inv_out, "maximum number of -fgrep, -egrep, -fgrep_v, -egrep_v options: %d\n", GREP_MAX);
    inv_out += strlen(inv_out);
    sprintf(inv_out, "RPN registers: 0..9\n");
    inv_out += strlen(inv_out);
    sprintf(inv_out, "memory files: @mem:0, @mem:1 .. @mem:%d\n",N_mem_buffers-1);
    inv_out += strlen(inv_out);
    sprintf(inv_out, "stdout buffer length: %d\n", INV_BUFFER);

#if (DEFAULT_G2CLIB == 0)
    strcat(inv_out, "default decoding: WMO standard\n");
#endif
#if (DEFAULT_G2CLIB == 1)
    strcat(inv_out, "default decoding: g2clib emulation\n");
#endif
#if (DEFAULT_G2CLIB == 2)
    strcat(inv_out, "default decoding: g2clib\n");
#endif

#ifdef USE_G2CLIB
    strcat(inv_out, "g2clib decoders are installed\n");
#else
    strcat(inv_out, "g2clib decoders are not installed\n");
#endif

    strcat(inv_out,"Supported decoding: simple, complex, rle, ieee");
#ifdef USE_PNG
    strcat(inv_out, ", png");
#endif
#ifdef USE_JASPER
    strcat(inv_out, ", jpeg2000");
#endif
#ifdef USE_AEC
    strcat(inv_out, ", CCSDS AEC");
#endif
    strcat(inv_out, "\n");

    filename = getenv("GRIB2TABLE");
    if (filename == NULL) filename = getenv("grib2table");
    if (filename == NULL) filename = "grib2table";
    input = fopen(filename,"r");
    inv_out += strlen(inv_out);
    sprintf(inv_out, "user gribtable: %s\n", input == NULL ? "(none)" : filename);
    if (input) fclose(input);

#ifdef CC
    strcat(inv_out,"C compiler: " CC "\n");
#endif

#ifdef FORTRAN
    strcat(inv_out,"Fortran compiler: " FORTRAN "\n");
#endif

#ifdef USE_OPENMP
    strcat(inv_out,"OpenMP: control number of threads with environment variable OMP_NUM_THREADS\n");
#else
    strcat(inv_out,"OpenMP: not used\n");
#endif
    inv_out += strlen(inv_out);

    sprintf(inv_out, "INT_MAX:   %d\n", INT_MAX);
    inv_out += strlen(inv_out);
    sprintf(inv_out, "ULONG_MAX: %lu\n", ULONG_MAX);

    return 1;
}
