/** \file
 *
 *  Contains the Config class definition.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#ifndef CONFIG_H_
#define CONFIG_H_

#include "component/Component.h"
#include "logger/Logger.h"
#include "utils/FastMap.h"

/**
 *  The Config class provides static methods for accessing configuration values.
 *  Configuration files are read by lua code in Tethys.lua::readConfig().
 *  Hence, this class falls under \ref module, with Lua, rather than under
 *  \ref utils.
 *
 *  \ingroup module
 */
class Config: public Component
{
public:

    /// Destructor -- never called
    virtual ~Config()
    {}

    /// Scans a folder for files that end in ".cfg" and loads them
    /// Also looks in sub-folders corresponding to the host name and user name
    static void LoadConfigFiles( const Str& path, const Str& altConfig, const Str& dataDir, Logger& logger );

    /// Returns an instance, or NULL if the config file can't be read
    /// duplicate will be set to true if this is a duplicate.
    static Config* Instance( const Str& realPath, const Str& basePath, const char* name, const Str& dataDir, bool& duplicate, Logger& logger );

    /// No reason to ever run a config component.
    void run() {};

    /// Saves a configuration setting persisted via configSet command.
    static void Persist( const Str& name, const char* line, Logger& logger );

    /// Loads configuration settings persisted via configSet command.
    /// If reload is false, settings will not be loaded after an unintentional restart
    /// If list is true, settings will just be displayed, not set
    static void LoadPersistedConfigSets( Logger& logger, bool reload, bool list );

    /// Release static resources.
    static void Uninitialize();

private:

    static FastMap<const Str, Str*> PersistedConfigSetMap_;
    static bool ignorePersistedConfig_;

    /// Copies the indicated file and its path to the destDir
    static void CopyFileToDir( const Str& path, const char* name, const Str& destDir, Logger& logger );

    /// Private constructor -- use Instance
    Config( const Str& name );

    /// Scans a folder for files that end in ".cfg" and loads them
    static void LoadConfigFilesInPath( const Str& basePath, const Str& addPath, const Str& dataDir, Logger& logger );

    /// Parse a line from the input file, associating the value with the Config object
    static bool ParseLine( Component* config, char* buffer, unsigned int length, bool secure );

};

#endif /*CONFIG_H_*/
