/** @file  pmConfig.h
 *
 *  @author PAP, IfA
 *
 *  @version $Revision: 1.11.2.1 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2006-07-12 21:09:44 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */
#ifndef PM_CONFIG_H
#define PM_CONFIG_H

#include "pslib.h"

// What recipe sources have been read so far?
typedef enum {
    P_PM_RECIPE_SOURCE_NONE        = 0x00, // None yet
    P_PM_RECIPE_SOURCE_SITE        = 0x01, // Site configuration
    P_PM_RECIPE_SOURCE_CAMERA      = 0x02, // Camera configuration
    P_PM_RECIPE_SOURCE_CL          = 0x04, // Command-line
    P_PM_RECIPE_SOURCE_SYMBOLIC    = 0x14, // Symbolic link, specified on command-line
    P_PM_RECIPE_SOURCE_ALL         = 0xff  // All sources
} p_pmRecipeSource;

// Configuration information
typedef struct
{
    psMetadata *site;                   // Site configuration
    psMetadata *camera;                 // Camera specification
    psMetadata *recipes;                // Recipes for processing
    psMetadata *arguments;              // Processed command-line arguments
    psMetadata *files;                  // pmFPAfiles used for analysis
    psDB *database;                     // Database handle
    int *argc;                          // Number of command-line arguments
    char **argv;                        // Command-line arguments (raw version)
    // Private members
    p_pmRecipeSource recipesRead;       // Which recipe sources have been read
    psMetadata *recipesSource;          // Where each recipe came from
}
pmConfig;

pmConfig *pmConfigAlloc(int *argc,      // Number of command-line arguments
                        char **argv     // Command-line arguments
                       );

void pmConfigSet(const char *path); // set the configPath

// Free static variables
void pmConfigDone(void);

/** pmConfigRead
 *
 * pmConfigRead shall load the site configuration (according to the above rule
 * for determining the source). The camera configuration shall also be loaded if
 * it is specified on the command line (argc, argv); otherwise it shall be set to
 * NULL. The recipe shall also be loaded from the command line (if specified) or,
 * if the camera configuration has been loaded, from the camera configuration and
 * recipe specification therein (see below). In dealing with the command line
 * parameters, the functions shall use the appropriate functions in psLib to
 * retrieve and remove the relevant options from the argument list; this
 * simplifies assignment of the mandatory arguments, since all the optional
 * command line arguments are removed leaving only the mandatory arguments. The
 * following psLib setups shall also be performed if they are specified in the
 * site configuration:
 *
 */
pmConfig *pmConfigRead(
    int *argc,
    char **argv);

/** we need this elsewhere; make it public **/
bool readConfig(
    psMetadata **config,                // Config to output
    const char *name,                   // Name of file
    const char *description             // Description of file
);

/** pmConfigValidateCamera
 *
 * This function, used by pmConfigCameraFromHeader, shall return true if the
 * FITS header matches the rule contained in the camera configuration (see
 * x2.2.2.3); otherwise it shall return false.
 *
 */
bool pmConfigValidateCameraFormat(
    const psMetadata *cameraFormat,
    const psMetadata *header);


/** pmConfigCameraFromHeader
 *
 * pmConfigCameraFromHeader shall load the camera configuration based on the
 * contents of the FITS header, using the list of known cameras contained in the
 * site configuration. If more than one camera matches the FITS header, a warning
 * shall be generated and the first matching camera returned.
 *
 */
psMetadata *pmConfigCameraFormatFromHeader(
    pmConfig *config,                   // The configuration
    const psMetadata *header           // The FITS header
);


// find the camera given the name
psMetadata *pmConfigCameraByName(
    pmConfig *config,                   // The configuration
    const char *cameraName                      // The camera name header
);

/** pmConfigRecipeFromCamera
 *
 * pmConfigRecipeFromCamera shall load the recipes from the list of known recipes contained in the camera
 * configuration.
 *
 */
bool pmConfigReadRecipes(pmConfig *config // Configuration
                        );

/** pmConfigDB
 *
 * pmConfigDB shall use the site configuration data to open a database handle.
 * This is fairly straightforward at the moment, but will change when we beef up
 * security. (TBD)
 *
 */
psDB *pmConfigDB(
    pmConfig *config                    // Configuration
);

/** pmConfigConformHeader
 *
 * Make the supplied header conform to the nominated camera format.
 */
bool pmConfigConformHeader(psMetadata *header, // Header to conform
                           const psMetadata *format // Camera format
                          );


psArray *pmConfigFileSets(pmConfig *config, char *file, char *list);
bool pmConfigFileSetsMD (psMetadata *metadata, pmConfig *config, char *name, char *file, char *list);


#endif
