/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <gmerlin/cfg_registry.h>
#include <registry_priv.h>
#include <gmerlin/utils.h>
#include <gmerlin/application.h>

bg_cfg_registry_t * bg_cfg_registry = NULL;

void bg_cfg_registry_init(const char * dir)
  {
  char * tmp_path;

  bg_cfg_registry = gavl_dictionary_create();

  bg_app_set_config_dir(dir);
  
  if((tmp_path = bg_search_file_read(dir, "cfg.xml")))
    {
    bg_cfg_registry_load(bg_cfg_registry, tmp_path);
    free(tmp_path);
    }
  }

void bg_cfg_registry_cleanup()
  {
  if(bg_cfg_registry)
    {
    gavl_dictionary_destroy(bg_cfg_registry);
    bg_cfg_registry = NULL;
    }
  }


/*
 *  Path looks like "section:subsection:subsubsection"
 */

// static void 

bg_cfg_section_t * bg_cfg_registry_find_section(bg_cfg_registry_t * r,
                                                const char * path)
  {
  int i;
  char ** tmp_sections;
  
  tmp_sections = bg_strbreak(path, ':');
  
  i = 0;
  while(tmp_sections[i])
    {
    r = bg_cfg_section_find_subsection(r, tmp_sections[i]);
    i++;
    }
  
  bg_strbreak_free(tmp_sections);
  
  return r;
  }


#if defined(__GNUC__)

static void cleanup_cfg_registry() __attribute__ ((destructor));

static void cleanup_cfg_registry()
  {
  bg_cfg_registry_cleanup();
  }

#endif
