/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

#include <poliqarp-config.h>

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include <foostring/foostring.h>
#include "bp-parser.h"
#include "utils.h"

void create_bp_parser_env(struct bp_parser_env *env)
{
   env->num_errors = 0;
}

void destroy_bp_parser_env(struct bp_parser_env *env)
{
}

int check_partial_file(const char *partial_file, const char *xml_file)
{
   time_t time_par;
   time_t time_xml;
   struct stat stat_buf;
   int info;

   info = stat(partial_file, &stat_buf);
   if (info != 0)
      return PARTIAL_FILE_GONE;
   time_par = stat_buf.st_mtime;
   info = stat(xml_file, &stat_buf);
   assert(info == 0);
   time_xml = stat_buf.st_mtime;
   return difftime(time_par, time_xml) < 0 ? /* is the partial file older ? */
      PARTIAL_FILE_NEEDS_REMAKE : PARTIAL_FILE_OK;
}

FILE *open_file_w_filter(const char *pathname, const char *mode, bool *is_pipe)
{
   FILE *stream;
   char *path = NULL;

   *is_pipe = true; /* assume pipe */
   if (strstr(pathname, ".gz")) {
      path = string_aformat("gzip -d -c '%s'", pathname);
      stream = popen(path, mode);
   } else if (strstr(pathname, ".bz2")) {
      path = string_aformat("bzip2 -d -c '%s'", pathname);
      stream = popen(path, mode);
   } else {
      stream = fopen(pathname, mode);
      *is_pipe = false; /* hey, it's a regular file */
   }
   free(path);
   return stream;
}

int bp_read_pool(struct bp_parser *base, struct binary_pool *pool,
   char *pool_name, char *base_name)
{
   char *path_i, *path_o;
   int retval = 0;

   vp_printf(_("Pre-processing pool: %s\n"), pool_name);
   vp_nest(vp_out);
   path_i = string_aformat("%s." CORPUS_MARKER ".%s.image", base_name, 
      pool_name);
   path_o = string_aformat("%s." CORPUS_MARKER ".%s.offset", base_name, 
      pool_name);
   if (binary_pool_add_dict(pool, path_i, path_o) != 0) {
      if (base->incremental) {
         vp_eprintf(_("Cannot open pool %s required for incremental build\n"), 
            pool_name);
         retval = -1;
      }
   }
   free(path_i); free(path_o);
   vp_unnest(vp_out);
   return retval;
}

void bp_write_pool(struct bp_parser *base, struct binary_pool *pool, 
   char *pool_name, char *base_name)
{
   char *path = NULL;
   FILE *f;

   vp_printf(_("Processing pool: %s\n"), pool_name);
   vp_nest(vp_out);
   vp_printf(_("Building dense array\n"));
   binary_pool_make_dense_array(pool);
   vp_printf(_("Pre-sorting by frequency\n"));
   binary_pool_sort(pool, binary_pool_compare_freq);
   path = string_aformat("%s." CORPUS_MARKER ".%s.image", base_name, 
      pool_name);
   f = xfopen(path, true);
   vp_printf(_("Writing pool image to %s\n"), path);
   binary_pool_write_data(pool, f);
   fclose(f);
   free(path); path = NULL;

   path = string_aformat("%s." CORPUS_MARKER ".%s.offset", base_name, 
      pool_name);
   f = xfopen(path, true);
   vp_printf(_("Writing offset data to %s\n"), path);
   binary_pool_write_offsets(pool, f);
   fclose(f);
   free(path); path = NULL;
   vp_unnest(vp_out);
}

void bp_write_pool2(struct bp_parser *base, struct binary_pool *pool, 
   char *pool_name, char *base_name)
{
   char *path = NULL;
   FILE *f;

   vp_printf(_("Processing pool: %s\n"), pool_name);
   vp_nest(vp_out);
   vp_printf(_("Building dense array\n"));
   binary_pool_make_dense_array(pool);

   vp_printf(_("Pre-sorting by frequency\n"));
   binary_pool_sort(pool, binary_pool_compare_freq);

   path = string_aformat("%s." CORPUS_MARKER ".%s.image", base_name, 
      pool_name);
   f = xfopen(path, true);
   vp_printf(_("Writing pool image to %s\n"), path);
   binary_pool_write_data(pool, f);
   fclose(f);
   free(path); path = NULL;

   path = string_aformat("%s." CORPUS_MARKER ".%s.offset", base_name, 
      pool_name);
   f = xfopen(path, true);
   vp_printf(_("Writing offset data to %s\n"), path);
   binary_pool_write_offsets(pool, f);
   fclose(f);
   free(path); path = NULL;

   vp_printf(_("Sorting (a fronte, case insensitive)\n"));
   binary_pool_sort_index(pool, binary_pool_compare_ialpha);
   path = string_aformat("%s." CORPUS_MARKER ".%s.index.alpha", base_name, 
      pool_name);
   f = xfopen(path, true);
   vp_printf(_("Writing index to %s\n"), path);
   binary_pool_write_index(pool, f);
   fclose(f);
   free(path); path = NULL;

   vp_printf(_("Sorting (a tergo, case insensitive)\n"));
   binary_pool_sort_index(pool, binary_pool_compare_iatergo);
   path = string_aformat("%s." CORPUS_MARKER ".%s.index.atergo", base_name, pool_name);
   f = xfopen(path, true);
   vp_printf(_("Writing index to %s\n"), path);
   binary_pool_write_index(pool, f);
   fclose(f);
   free(path); path = NULL;

   vp_unnest(vp_out);
}

int generic_parse_loop(const char *dir, const char *filename, 
   xmlParserCtxtPtr xml_parser)
{
   size_t len;
   char buffer[BUFSIZ];
   time_t start_time, end_time;
   bool error = false;
   bool pipe;
   double duration = 0;
   FILE *stream;

   stream = open_file_w_filter(filename, "r", &pipe);
   if (stream == NULL) {
      vp_eprintf("%s/%s: %s\n", dir, filename, strerror(errno));
      return -1;
   }
   vp_nest(vp_out);
   time(&start_time);
   xmlCtxtResetPush(xml_parser, NULL, 0, filename, NULL);
   do {
      /* read from input stream */
      len = fread(buffer, 1, sizeof buffer, stream);
      if (ferror(stream)) {
         vp_eprintf("Read error: %s/%s: %s", dir, filename, 
            strerror(errno));
         error = true;
         goto bye_bye;
      }

      /* invoke parser */
      if (xmlParseChunk(xml_parser, buffer, len, len == 0) != 0) {
         vp_eprintf("%s/%s:%d: %s", dir, filename,
            xml_parser->input->line,
            xml_parser->lastError.message);
         error = true;
         goto bye_bye;
      }
   } while (len > 0);

   /* cleanup */
bye_bye:
   time(&end_time);
   duration = difftime(end_time, start_time);
   vp_unnest(vp_out);
   
   if (duration > 3) {
      vp_printf(ngettext("(%g second fades into the past)", 
         "(%g seconds fade into the past)", duration), duration);
      vp_printf("\n");
   }
   if (pipe) 
      pclose(stream);
   else
      fclose(stream);
   return error ? -1 : 0;
}

xmlSAXHandler bp_dummy_sax_handler = {
   .internalSubset = NULL,
   .isStandalone = NULL,
   .hasInternalSubset = NULL,
   .hasExternalSubset = NULL,
   .resolveEntity = NULL,
   .getEntity = NULL,
   .entityDecl = NULL,
   .notationDecl = NULL,
   .attributeDecl = NULL,
   .elementDecl = NULL,
   .unparsedEntityDecl = NULL,
   .setDocumentLocator = NULL,
   .startDocument = NULL,
   .endDocument = NULL,
   .startElement = NULL,
   .endElement = NULL,
   .reference = NULL,
   .characters = NULL,
   .ignorableWhitespace = NULL,
   .processingInstruction = NULL,
   .comment = NULL,
   .warning = NULL,
   .error = NULL,
   .getParameterEntity = NULL,
   .cdataBlock = NULL,
   .externalSubset = NULL,
   .startElementNs = NULL,
   .endElementNs = NULL,
   .serror = NULL,
   .initialized = XML_SAX2_MAGIC
};
