#include "grib_int.h"
#include <string.h>
#include <ctype.h>

FILE *grprsm = NULL;
int CGRIBEX_grib_calendar = -1;

void
gribSetCalendar(int calendar)
{
  CGRIBEX_grib_calendar = calendar;
}

void
grsdef(void)
{
  /*
C---->
C**** GRSDEF - Initial (default) setting of common area variables
C              for GRIBEX package.
C
C     Purpose.
C     --------
C
C     Sets initial values for common area variables for all
C     routines of GRIBEX package, if not already done.
C
C**   Interface.
C     ----------
C
C     CALL GRSDEF
C
C     Input Parameters.
C     -----------------
C
C     None.
C
C     Output Parameters.
C     ------------------
C
C     None.
C
C     Method.
C     -------
C
C     Self-explanatory.
C
C     Externals.
C     ----------
C
C     None.
C
C     Reference.
C     ----------
C
C     See subroutine GRIBEX.
C
C     Comments.
C     ---------
C
C     None
C
C     Author.
C     -------
C
C     J. Clochard, Meteo France, for ECMWF - March 1998.
C
C     Modifications.
C     --------------
C
C     J. Clochard, Meteo France, for ECMWF - June 1999.
C     Add variable NSUBCE.
C     Use a static variable to determine if initialisation has already
C     been done. NUSER removed .
C     Reverse defaults for NEXT2O and NLOC2O, for consistency with
C     version 13.023 of software .
C
  */
  /*
C     ----------------------------------------------------------------
C*    Section 0 . Definition of variables.
C     ----------------------------------------------------------------
  */
  char *envString;
  char *env_stream;
  static bool lfirst = true;
  extern int CGRIBEX_Const;

  if (!lfirst) return;

  /*
    ----------------------------------------------------------------
    Section 1 . Set values, conditionally.
    ----------------------------------------------------------------
  */
  /*
    Common area variables have not been set. Set them.
  */
  // Set GRIB calendar.
  if (CGRIBEX_grib_calendar == -1)
    {
      CGRIBEX_grib_calendar = CALENDAR_PROLEPTIC;

      envString = getenv("GRIB_CALENDAR");
      if (envString)
        {
          if (strncmp(envString, "standard", 8) == 0)
            CGRIBEX_grib_calendar = CALENDAR_STANDARD;
          else if (strncmp(envString, "proleptic", 9) == 0)
            CGRIBEX_grib_calendar = CALENDAR_PROLEPTIC;
          else if (strncmp(envString, "360days", 7) == 0)
            CGRIBEX_grib_calendar = CALENDAR_360DAYS;
          else if (strncmp(envString, "365days", 7) == 0)
            CGRIBEX_grib_calendar = CALENDAR_365DAYS;
          else if (strncmp(envString, "366days", 7) == 0)
            CGRIBEX_grib_calendar = CALENDAR_366DAYS;
          else if (strncmp(envString, "none", 4) == 0)
            CGRIBEX_grib_calendar = CALENDAR_NONE;
        }
    }
  // Set GRIBEX compatibility mode.
  envString = getenv("GRIB_GRIBEX_MODE_ON");
  if (envString != NULL)
    {
      if (atoi(envString) == 1) CGRIBEX_Const = 0;
    }

  // See if output stream needs changing
  grprsm = stdout;
  env_stream = getenv("GRPRS_STREAM");
  if (env_stream)
    {
      if (isdigit((int) env_stream[0]))
        {
          int unit;
          unit = atoi(env_stream);
          if (unit < 1 || unit > 99)
            Warning("Invalid number for GRPRS_STREAM: %d", unit);
          else if (unit == 2)
            grprsm = stderr;
          else if (unit == 6)
            grprsm = stdout;
          else
            {
              char filename[] = "unit.00";
              snprintf(filename, sizeof(filename), "%2.2d", unit);
              grprsm = fopen(filename, "w");
              if (!grprsm) SysError("GRPRS_STREAM = %d", unit);
            }
        }
      else
        {
          if (env_stream[0])
            {
              grprsm = fopen(env_stream, "w");
              if (!grprsm) SysError("GRPRS_STREAM = %s", env_stream);
            }
        }
    }
  // Mark common area values set by user.
  lfirst = false;
}
