/* *********************************************************
 * *                                                       * 
 * *                Configuration Library                  * 
 * *       IRIS_LOAD - Load IRIS Information From file     * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/iris_load.c
 *
 *   COPYRIGHT (c) 1988, 1989, 1994, 1997, 1999, 2000, 2002, 2003 BY 
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.  
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED. 
 */     
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sigtypes.h"
#include "sigversion.h"
#include "setup.h"
#include "config_lib.h"

/* =================================================================== 
 * This subroutine reads the saved values from several different files
 * which together define the IRIS environment.
 */
MESSAGE irisload
( const char *sRemoteHostname_a,
  volatile struct iris_setup *pIrisSetup_a )
{
  MESSAGE istatus, iExitStatus = SS_NORMAL ;

  /* Initialize to all defaults in case of an early error
   */     
  memset( (void *)pIrisSetup_a, 0, sizeof( struct iris_setup) ) ;
  istatus = setup_limits( "iris_setup", pIrisSetup_a, SETUP_DEFVAL, NULL ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;
      
  /* Load all the component parts of the structure
   */
  istatus = lim_loadFile( sRemoteHostname_a, "setup_link.conf",
			  "link_setup", &pIrisSetup_a->link, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  istatus = lim_loadFile( sRemoteHostname_a, "setup_misc.conf",
			  "misc_setup", &pIrisSetup_a->misc, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  istatus = lim_loadFile( sRemoteHostname_a, "setup_protect.conf",
			  "protect_setup", &pIrisSetup_a->protect, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  istatus = lim_loadFile( sRemoteHostname_a, "setup_device.conf", 
			  "device_setup", &pIrisSetup_a->device, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  istatus = lim_loadFile( sRemoteHostname_a, "setup_site.conf",
			  "site_setup", &pIrisSetup_a->Site, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  istatus = lim_loadFile( sRemoteHostname_a, "setup_input.conf",
			  "input_setup", &pIrisSetup_a->Input, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  istatus = lim_loadFile( sRemoteHostname_a, "setup_web.conf",
			  "web_setup", &pIrisSetup_a->Web, setup_limits ) ;
  if( istatus != SS_NORMAL ) iExitStatus = istatus ;

  return( iExitStatus ) ;
}

/* =================================================================== 
 * This subroutine writes the internal values into several different
 * files which together define the IRIS environment.
 */
MESSAGE irissave
( const char *sRemoteHostname_a,
  volatile const struct iris_setup *pIrisSetup_a )
{
  MESSAGE istatus, iExitStatus = SS_NORMAL ;

  /* Load all the component parts of the structure, filling in the
   * IRIS version for each one.
   */
  {
    struct link_setup setup = pIrisSetup_a->link ;
    strncpy( setup.sVersion, IRIS_VERSION, 8 ) ; setup.sVersion[7] = '\0' ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_link.conf", 
			    "link_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  {
    struct misc_setup setup = pIrisSetup_a->misc ;
    strncpy( setup.sVersion, IRIS_VERSION, 8 ) ; setup.sVersion[7] = '\0' ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_misc.conf",
			    "misc_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  {
    struct protect_setup setup = pIrisSetup_a->protect ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_protect.conf", 
			    "protect_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  {
    struct device_setup setup = pIrisSetup_a->device ;
    strncpy( setup.sVersion, IRIS_VERSION, 8 ) ; setup.sVersion[7] = '\0' ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_device.conf", 
			    "device_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  {
    struct site_setup setup = pIrisSetup_a->Site ;
    strncpy( setup.sVersion, IRIS_VERSION, 8 ) ; setup.sVersion[7] = '\0' ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_site.conf",
			    "site_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  {
    struct input_setup setup = pIrisSetup_a->Input ;
    strncpy( setup.sVersion, IRIS_VERSION, 8 ) ; setup.sVersion[7] = '\0' ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_input.conf",
			    "input_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  {
    struct web_setup setup = pIrisSetup_a->Web ;
    strncpy( setup.sVersion, IRIS_VERSION, 8 ) ; setup.sVersion[7] = '\0' ;
    istatus = lim_saveFile( sRemoteHostname_a, "setup_web.conf",
			    "web_setup", &setup, setup_limits ) ;
    if( istatus != SS_NORMAL ) iExitStatus = istatus ;
  }
  return( iExitStatus ) ;
}
