#include <stdio.h>
#include <string.h>
#include <kom.h>
#include <transport.h>
#include <earthworm.h>
#include "loc_wcatwc.h"         

#define ncommand 40          /* Number of commands in the config file */


 /***********************************************************************
  *                              GetConfig()                            *
  *             Processes command file using kom.c functions.           *
  *               Returns -1 if any errors are encountered.             *
  ***********************************************************************/

int GetConfig( char *config_file, GPARM *Gparm )
{
   char     init[ncommand];     /* Flags, one for each command */
   int      nmiss;              /* Number of commands that were missed */
   int      nfiles;
   int      i;

/* Set to zero one init flag for each required command
   ***************************************************/
   for ( i=0; i<ncommand; i++ ) init[i] = 0;
   strcpy( Gparm->ATPLineupFileBB, "\0" );

/* Open the main configuration file
   ********************************/
   nfiles = k_open( config_file );
   if ( nfiles == 0 )
   {
      fprintf( stderr, "loc_wcatwc: Error opening configuration file <%s>\n",
               config_file );
      return -1;
   }

/* Process all nested configuration files
   **************************************/
   while ( nfiles > 0 )          /* While there are config files open */
   {
      while ( k_rd() )           /* Read next line from active file  */
      {
         int  success;
         char *com;
         char *str;

         com = k_str();          /* Get the first token from line */

         if ( !com ) continue;             /* Ignore blank lines */
         if ( com[0] == '#' ) continue;    /* Ignore comments */

/* Open another configuration file
   *******************************/
         if ( com[0] == '@' )
         {
            success = nfiles + 1;
            nfiles  = k_open( &com[1] );
            if ( nfiles != success )
            {
               fprintf( stderr, "loc_wcatwc: Error opening command file <%s>."
			            "\n", &com[1] );
               return -1;
            }
            continue;
         }

/* Read configuration parameters
   *****************************/
         else if ( k_its( "StaFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->StaFile, str );
            init[23] = 1;
         }
		 
         else if ( k_its( "StaDataFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->StaDataFile, str );
            init[0] = 1;
         }

         else if ( k_its( "InRing" ) )
         {
            if ( str = k_str() )
            {
               if( (Gparm->InKey = GetKey(str)) == -1 )
               {
                  fprintf( stderr, "loc_wcatwc: Invalid InRing name <%s>. "
                                   "Exiting.\n", str );
                  return -1;
               }
            }
            init[1] = 1;
         }

         else if ( k_its( "OutRing" ) )
         {
            if ( str = k_str() )
            {
               if ( (Gparm->OutKey = GetKey(str)) == -1 )
               {
                  fprintf( stderr, "loc_wcatwc: Invalid OutRing name <%s>. "
                                   "Exiting.\n", str );
                  return -1;
               }
            }
            init[2] = 1;
         }

         else if ( k_its( "AlarmRing" ) )
         {
            if ( str = k_str() )
            {
               if ( (Gparm->AlarmKey = GetKey(str)) == -1 )
               {
                  fprintf( stderr, "loc_wcatwc: Invalid AlarmRing name <%s>. "
                                   "Exiting.\n", str );
                  return -1;
               }
            }
            init[3] = 1;
         }

         else if ( k_its( "HeartbeatInt" ) )
         {
            Gparm->HeartbeatInt = k_int();
            init[4] = 1;
         }

         else if ( k_its( "NumPBuffs" ) )
         {
            Gparm->NumPBuffs = k_int();
            init[5] = 1;
			if ( Gparm->NumPBuffs > MAX_PBUFFS )
               {
                  fprintf( stderr, "Too many P buffers specified <%ld>. "
                                   "Exiting.\n", Gparm->NumPBuffs );
                  return -1;
               }
         }

         else if ( k_its( "MaxTimeBetweenPicks" ) )
         {
            Gparm->MaxTimeBetweenPicks = k_val();
            init[6] = 1;
         }

         else if ( k_its( "NumNearStn" ) )
         {
            Gparm->iNumNearStn = k_int();
            init[24] = 1;
         }

         else if ( k_its( "MinPs" ) )
         {
            Gparm->MinPs = k_int();
            init[7] = 1;
            if ( Gparm->MinPs < 5 )
               {
                  fprintf( stderr, "MinPs too small <%ld>. Minimum 5. "
                                   "Exiting.\n", Gparm->MinPs );
                  return -1;
               }
         }

         else if ( k_its( "Debug" ) )
         {
            Gparm->Debug = k_int();
            init[8] = 1;
         }

         else if ( k_its( "MyModId" ) )
         {
            if ( str = k_str() )
            {
               if ( GetModId(str, &Gparm->MyModId) == -1 )
               {
                  fprintf( stderr, "loc_wcatwc: Invalid MyModId <%s>.\n", str);
                  return -1;
               }
            }
            init[9] = 1;
         }
		 
         else if ( k_its( "BValFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szBValFile, str );
            init[10] = 1;
         }
		 
         else if ( k_its( "OldQuakes" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szOldQuakes, str );
            init[11] = 1;
         }
		 
         else if ( k_its( "AutoLoc" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szAutoLoc, str );
            init[12] = 1;
         }
		 
         else if ( k_its( "DummyFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szDummyFile, str );
            init[13] = 1;
         }
		 
         else if ( k_its( "MapFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szMapFile, str );
            init[14] = 1;
         }
		 
         else if ( k_its( "RTPFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szRTPFile, str );
            init[15] = 1;
         }
		 
         else if ( k_its( "QLogFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szQLogFile, str );
            init[16] = 1;
         }
		 
         else if ( k_its( "MwFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szMwFile, str );
            init[17] = 1;
         }
		 
         else if ( k_its( "DepthFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szDepthDataFile, str );
            init[18] = 1;
         }

         else if ( k_its( "SouthernLat" ) )
         {
            Gparm->SouthernLat = k_val();
            init[19] = 1;
         }

         else if ( k_its( "NorthernLat" ) )
         {
            Gparm->NorthernLat = k_val();
            init[20] = 1;
         }

         else if ( k_its( "WesternLon" ) )
         {
            Gparm->WesternLon = k_val();
            init[21] = 1;
         }

         else if ( k_its( "EasternLon" ) )
         {
            Gparm->EasternLon = k_val();
            init[22] = 1;
         }
		 
         else if ( k_its( "PFilePath" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szPFilePath, str );
            init[25] = 1;
         }
		 
         else if ( k_its( "CityFileWUC" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->CityFileWUC, str );
            init[26] = 1;
         }
		 
         else if ( k_its( "CityFileWLC" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->CityFileWLC, str );
            init[27] = 1;
         }
		 
         else if ( k_its( "CityFileEUC" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->CityFileEUC, str );
            init[28] = 1;
         }
		 
         else if ( k_its( "CityFileELC" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->CityFileELC, str );
            init[29] = 1;
         }
		 
         else if ( k_its( "NameFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szNameFile, str );
            init[30] = 1;
         }
		 
         else if ( k_its( "NameFileLC" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szNameFileLC, str );
            init[31] = 1;
         }
		 
         else if ( k_its( "IndexFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szIndexFile, str );
            init[32] = 1;
         }
		 
         else if ( k_its( "LatFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szLatFile, str );
            init[33] = 1;
         }

         else if ( k_its( "MinMagToSend" ) )
         {
            Gparm->MinMagToSend = k_val();
            init[34] = 1;
         }
	 
		          else if ( k_its( "ResponseFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->ResponseFile, str );
            init[35] = 1;
         }

         else if ( k_its( "TaulibFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szTaulibFile, str );
            init[36] = 1;
         }

         else if ( k_its( "Iasp91File" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szIasp91File, str );
            init[37] = 1;
         }

         else if ( k_its( "Iasp91TblFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szIasp91TblFile, str );
            init[38] = 1;
         }
		 
         else if ( k_its( "ThetaFile" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->szThetaFile, str );
            init[39] = 1;
         }

/* Optional command when run with ATPlayer
  ****************************************/	 
         else if ( k_its( "ATPLineupFileBB" ) )
         {
            if ( str = k_str() )
               strcpy( Gparm->ATPLineupFileBB, str );
         }

/* An unknown parameter was encountered
   ************************************/
         else
         {
            fprintf( stderr, "loc_wcatwc: <%s> unknown parameter in <%s>\n",
                    com, config_file );
            continue;
         }

/* See if there were any errors processing the command
   ***************************************************/
         if ( k_err() )
         {
            fprintf( stderr, "loc_wcatwc: Bad <%s> command in <%s>.\n", com,
                     config_file );
            return -1;
         }
      }
      nfiles = k_close();
   }

/* After all files are closed, check flags for missed commands
   ***********************************************************/
   nmiss = 0;
   for ( i = 0; i < ncommand; i++ )
      if ( !init[i] )
         nmiss++;

   if ( nmiss > 0 )
   {
      fprintf( stderr, "loc_wcatwc: ERROR, no " );
      if ( !init[0] ) fprintf( stderr, "<StaDataFile> " );
      if ( !init[1] ) fprintf( stderr, "<InRing> " );
      if ( !init[2] ) fprintf( stderr, "<OutRing> " );
      if ( !init[3] ) fprintf( stderr, "<AlarmRing> " );
      if ( !init[4] ) fprintf( stderr, "<HeartbeatInt> " );
      if ( !init[5] ) fprintf( stderr, "<NumPBuffs> " );
      if ( !init[6] ) fprintf( stderr, "<MaxTimeBetweenPicks> " );
      if ( !init[7] ) fprintf( stderr, "<MinPs> " );
      if ( !init[25] ) fprintf( stderr, "<NumNearStn> " );
      if ( !init[8] ) fprintf( stderr, "<Debug> " );
      if ( !init[9] ) fprintf( stderr, "<MyModId> " );
      if ( !init[10] ) fprintf( stderr, "<BValFile> " );
      if ( !init[11] ) fprintf( stderr, "<OldQuakes> " );
      if ( !init[12] ) fprintf( stderr, "<AutoLoc> " );
      if ( !init[13] ) fprintf( stderr, "<DummyFile> " );
      if ( !init[14] ) fprintf( stderr, "<MapFile> " );
      if ( !init[15] ) fprintf( stderr, "<RTPFile> " );
      if ( !init[16] ) fprintf( stderr, "<QLogFile> " );
      if ( !init[17] ) fprintf( stderr, "<MwFile> " );
      if ( !init[18] ) fprintf( stderr, "<DepthFile> " );
      if ( !init[19] ) fprintf( stderr, "<NorthernLat> " );
      if ( !init[20] ) fprintf( stderr, "<SouthernLat> " );
      if ( !init[21] ) fprintf( stderr, "<WesternLon> " );
      if ( !init[22] ) fprintf( stderr, "<EasternLon> " );
      if ( !init[23] ) fprintf( stderr, "<StaFile> " );
      if ( !init[25] ) fprintf( stderr, "<PFilePath> " );
      if ( !init[26] ) fprintf( stderr, "<CityFileWUC> " );
      if ( !init[27] ) fprintf( stderr, "<CityFileWLC> " );
      if ( !init[28] ) fprintf( stderr, "<CityFileEUC> " );
      if ( !init[29] ) fprintf( stderr, "<CityFileELC> " );
      if ( !init[30] ) fprintf( stderr, "<NameFile> " );
      if ( !init[31] ) fprintf( stderr, "<NameFileLC> " );
      if ( !init[32] ) fprintf( stderr, "<IndexFile> " );
      if ( !init[33] ) fprintf( stderr, "<LatFile> " );
      if ( !init[34] ) fprintf( stderr, "<MinMagToSend> " );
      if ( !init[35] ) fprintf( stderr, "<ResponseFile> " );
      if ( !init[36] ) fprintf( stderr, "<TaulibFile> " );
      if ( !init[37] ) fprintf( stderr, "<Iasp91File> " );
      if ( !init[38] ) fprintf( stderr, "<Iasp91TblFile> " );
      if ( !init[39] ) fprintf( stderr, "<ThetaFile> " );
      fprintf( stderr, "command(s) in <%s>. Exiting.\n", config_file );
      return -1;
   }
   return 0;
}

 /***********************************************************************
  *                              LogConfig()                            *
  *                                                                     *
  *                   Log the configuration parameters                  *
  ***********************************************************************/

void LogConfig( GPARM *Gparm )
{
   logit( "", "\n" );
   logit( "", "StaFile:            %s\n",    Gparm->StaFile );
   logit( "", "StaDataFile:        %s\n",    Gparm->StaDataFile );
   logit( "", "ResponseFile:       %s\n",    Gparm->ResponseFile );
   logit( "", "InKey:              %d\n",    Gparm->InKey );
   logit( "", "OutKey:             %d\n",    Gparm->OutKey );
   logit( "", "AlarmKey:           %d\n",    Gparm->AlarmKey );
   logit( "", "HeartbeatInt:       %d\n",    Gparm->HeartbeatInt );
   logit( "", "NumPBuffs:          %d\n",    Gparm->NumPBuffs );
   logit( "", "MaxTimeBetweenPicks:%lf\n",   Gparm->MaxTimeBetweenPicks );
   logit( "", "MinPs:              %d\n",    Gparm->MinPs );
   logit( "", "NumNearStn:         %d\n",    Gparm->iNumNearStn );
   logit( "", "Debug:              %d\n",    Gparm->Debug );
   logit( "", "MyModId:            %u\n",    Gparm->MyModId );
   logit( "", "BValFile:           %s\n",    Gparm->szBValFile );
   logit( "", "OldQuakes:          %s\n",    Gparm->szOldQuakes );
   logit( "", "AutoLoc:            %s\n",    Gparm->szAutoLoc );
   logit( "", "DummyFile:          %s\n",    Gparm->szDummyFile );
   logit( "", "MapFile:            %s\n",    Gparm->szMapFile );
   logit( "", "RTPFile:            %s\n",    Gparm->szRTPFile );
   logit( "", "MwFile:             %s\n",    Gparm->szMwFile );
   logit( "", "DepthFile  :        %s\n",    Gparm->szDepthDataFile );
   logit( "", "NorthernLat:        %lf\n",   Gparm->NorthernLat );
   logit( "", "SouthernLat:        %lf\n",   Gparm->SouthernLat );
   logit( "", "WesternLon:         %lf\n",   Gparm->WesternLon );
   logit( "", "EasternLon:         %lf\n",   Gparm->EasternLon );
   logit( "", "MinMagToSend:       %lf\n",   Gparm->MinMagToSend );
   logit( "", "CityFileWUC:        %s\n",    Gparm->CityFileWUC );
   logit( "", "CityFileWLC:        %s\n",    Gparm->CityFileWLC );
   logit( "", "CityFileEUC:        %s\n",    Gparm->CityFileEUC );
   logit( "", "CityFileELC:        %s\n",    Gparm->CityFileELC );
   logit( "", "NameFile:           %s\n",    Gparm->szNameFile );
   logit( "", "NameFileLC:         %s\n",    Gparm->szNameFileLC );
   logit( "", "IndexFile:          %s\n",    Gparm->szIndexFile );
   logit( "", "LatFile:            %s\n",    Gparm->szLatFile );
   logit( "", "TaulibFile:         %s\n",    Gparm->szTaulibFile );
   logit( "", "Iasp91File:         %s\n",    Gparm->szIasp91File );
   logit( "", "Iasp91TblFile:      %s\n",    Gparm->szIasp91TblFile );
   logit( "", "ThetaFile:          %s\n",    Gparm->szThetaFile );
   logit( "", "QLogFile:           %s\n\n",  Gparm->szQLogFile );
   if ( strlen( Gparm->ATPLineupFileBB ) > 2 )
      logit( "", "Loc_ to be used with Player - File: %s\n",
       Gparm->ATPLineupFileBB);
   logit( "", "PFilePath:          %s\n\n",  Gparm->szPFilePath );
   return;
}
