/*   Cataract - Static web photo gallery generator
 *   Copyright (C) 2008 Tomas Bzatek <tbzatek@users.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 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, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include <glib.h>
#include <glib/gstdio.h>

#include <config.h>

#include "gallery-utils.h"
#include "setup.h"
#include "xml-parser.h"



/*
 *   find_setup_xml: try to find setup.xml in standard paths
 */
TGallerySetup *
find_setup_xml ()
{
  gchar *pth;
  gchar *cwd;
  TGallerySetup *setup;

  setup = NULL;
  cwd = g_get_current_dir ();
  pth = g_build_filename (cwd, SETUP_XML, NULL);
  g_free (cwd);

  if (g_access (pth, R_OK) == 0)
    setup = parse_setup_xml (pth);
  g_free (pth);

  if (! setup) {
    pth = g_build_filename (g_getenv ("HOME"), ".cgg", SETUP_XML, NULL);
    if (g_access (pth, R_OK) == 0)
      setup = parse_setup_xml (pth);
    g_free (pth);
  }

  if (! setup) {
    pth = g_build_filename (DATADIR, "cgg", SETUP_XML, NULL);
    if (g_access (pth, R_OK) == 0)
      setup = parse_setup_xml (pth);
    g_free (pth);
  }

  return setup;
}


/*
 *   parse_setup_xml: XML parser for setup.xml file
 */
TGallerySetup *
parse_setup_xml (const gchar *filename)
{
  TXMLFile *xml;
  gchar *s;
  TGallerySetup *setup;

  xml = xml_parser_load (filename);
  if (xml == NULL)
    return NULL;

  setup = g_malloc0 (sizeof (TGallerySetup));
  setup->setup_xml_path = g_strdup (filename);

  setup->templates_path = xml_file_get_node_value (xml, "/gallery_setup/templates/path/text()");
  setup->template_index = xml_file_get_node_value (xml, "/gallery_setup/templates/index/text()");
  setup->template_album = xml_file_get_node_value (xml, "/gallery_setup/templates/album/text()");
  setup->template_photo = xml_file_get_node_value (xml, "/gallery_setup/templates/photo/text()");
  s = xml_file_get_node_value (xml, "/gallery_setup/templates/template_files/text()");
  if (s) {
    setup->template_files = g_strsplit (s, "\n", -1);
    g_free (s);
  }
  setup->support_files_use_common_root = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/templates/support_files_use_common_root", "value", FALSE);
  setup->index_file_name = xml_file_get_node_value_with_default (xml, "/gallery_setup/templates/index_file/text()", DEFAULT_INDEX_FILENAME);

  setup->thumbnail_dir = xml_file_get_node_value_with_default (xml, "/gallery_setup/images/thumbnail_dir/text()", DEFAULT_THUMBNAIL_DIR);
  setup->img_big_dir = xml_file_get_node_value_with_default (xml, "/gallery_setup/images/preview_dir/text()", DEFAULT_IMG_BIG_DIR);
  setup->img_orig_dir = xml_file_get_node_value_with_default (xml, "/gallery_setup/images/original_dir/text()", DEFAULT_IMG_ORIG_DIR);
  setup->thumbnail_name_format = xml_file_get_node_value_with_default (xml, "/gallery_setup/images/thumbnail_name_format/text()", DEFAULT_THUMBNAIL_NAME_FORMAT);
  if (strstr (setup->thumbnail_name_format, "%s") == NULL) {
    g_free (setup->thumbnail_name_format);
    setup->thumbnail_name_format = g_strdup (DEFAULT_THUMBNAIL_NAME_FORMAT);
  }

  setup->thumbnail_landscape_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "landscape_w", 0);
  setup->thumbnail_landscape_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "landscape_h", 0);
  setup->thumbnail_portrait_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "portrait_w", 0);
  setup->thumbnail_portrait_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "portrait_h", 0);
  setup->thumbnail_square_size = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "square", 0);
  setup->thumbnail_quality = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "quality", -1);
  setup->preview_landscape_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "landscape_w", 0);
  setup->preview_landscape_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "landscape_h", 0);
  setup->preview_portrait_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "portrait_w", 0);
  setup->preview_portrait_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "portrait_h", 0);
  setup->preview_quality = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "quality", -1);
  setup->footer = xml_file_get_node_value (xml, "/gallery_setup/footer/text()");
  setup->border_style = xml_file_get_node_attribute (xml, "/gallery_setup/images/border", "style");
  setup->meta_author = xml_file_get_node_value (xml, "/gallery_setup/meta/author/text()");
  setup->meta_author_email = xml_file_get_node_value (xml, "/gallery_setup/meta/author_email/text()");
  setup->meta_description = xml_file_get_node_value (xml, "/gallery_setup/meta/description/text()");
  setup->meta_keywords = xml_file_get_node_value (xml, "/gallery_setup/meta/keywords/text()");

  setup->preload = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/images/preload", "value", TRUE);
  setup->use_iptc_exif = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/images/use_iptc_exif", "value", FALSE);
  setup->erase_exif_thumbnail = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/images/erase_embed_thumbnail", "value", FALSE);
  setup->use_title_as_meta = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/meta/use_title_as_meta", "value", TRUE);

  setup->site_title = xml_file_get_node_attribute (xml, "/gallery_setup/meta/site", "title");
  setup->add_copyright = xml_file_get_node_value (xml, "/gallery_setup/meta/add_copyright/text()");
  setup->use_inpage_links = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/navigation/use_inpage_links", "value", TRUE);
  setup->show_go_up = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/navigation/show_go_up", "value", TRUE);
  setup->show_exif_table = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/navigation/show_exif_table", "value", TRUE);

  setup->nofullsize = xml_file_get_node_present (xml, "/gallery_setup/images/nofullsize");

  setup->favicon_file = xml_file_get_node_value (xml, "/gallery_setup/meta/favicon/text()");
  setup->favicon_type = xml_file_get_node_attribute (xml, "/gallery_setup/meta/favicon", "type");

  setup->feed_enabled = xml_file_get_node_attribute_boolean (xml, "/gallery_setup/feed", "enable", FALSE);
  setup->feed_filename = xml_file_get_node_value (xml, "/gallery_setup/feed/filename/text()");
  setup->feed_title = xml_file_get_node_value (xml, "/gallery_setup/feed/title/text()");
  setup->feed_base_url = xml_file_get_node_value (xml, "/gallery_setup/feed/base_url/text()");

  s = xml_file_get_node_attribute (xml, "/gallery_setup/images/squared_thumbnails", "type");
  if (s && g_ascii_strcasecmp (s, "simple") == 0)
    setup->squared_thumbnail_type = THUMBNAIL_SQUARE_TYPE_SIMPLE;
  else
    setup->squared_thumbnail_type = THUMBNAIL_SQUARE_TYPE_NONE;
  g_free (s);

  xml_parser_free (xml);
  return setup;
}



static int
test_tmpl_access (const gchar *dir, const gchar *path)
{
  gchar *s;
  int b;

  s = g_build_filename (dir, path, NULL);
  b = g_access (s, R_OK);
  g_free (s);
  return b;
}

static int
test_tmpl_files (const gchar *dir, TGallerySetup *setup)
{
  return test_tmpl_access (dir, setup->template_album) |
         test_tmpl_access (dir, setup->template_photo) |
         test_tmpl_access (dir, setup->template_index);
}

/*
 *   find_templates_directory: absolute/relative path checks, trying to find templates directory
 *                             - returned string should be freed
 */
gchar *
find_templates_directory (TGallerySetup *setup)
{
  gchar *base_dir;
  gchar *pth;

  if (IS_DIR_SEP (*setup->templates_path)) {
    if (! test_tmpl_files (setup->templates_path, setup))
      return g_strdup (setup->templates_path);
  } else {
    base_dir = g_path_get_dirname (setup->setup_xml_path);
    pth = g_build_path (G_DIR_SEPARATOR_S, base_dir, setup->templates_path, NULL);
    g_free (base_dir);

    if (! test_tmpl_files (pth, setup))
      return pth;
  }

  log_error ("Couldn't find proper templates directory (tried '%s')\n", setup->templates_path);
  return NULL;
}


/*
 *   free_setup_data: free allocated setup data
 */
void
free_setup_data (TGallerySetup *setup)
{
  if (setup) {
    g_free (setup->real_templates_dir);
    g_free (setup->setup_xml_path);
    g_free (setup->templates_path);
    g_free (setup->template_index);
    g_free (setup->template_album);
    g_free (setup->template_photo);
    g_strfreev (setup->template_files);
    g_free (setup->index_file_name);
    g_free (setup->footer);
    g_free (setup->meta_author);
    g_free (setup->meta_author_email);
    g_free (setup->meta_description);
    g_free (setup->meta_keywords);
    g_free (setup->border_style);
    g_free (setup->site_title);
    g_free (setup->add_copyright);
    g_free (setup->favicon_file);
    g_free (setup->favicon_type);
    g_free (setup->thumbnail_dir);
    g_free (setup->img_big_dir);
    g_free (setup->img_orig_dir);
    g_free (setup->thumbnail_name_format);
    g_free (setup->feed_filename);
    g_free (setup->feed_title);
    g_free (setup->feed_base_url);
    g_free (setup);
  }
}
