/*
 * 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 <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#include <bp/common.h>

#include "cmdline.h"
#include "utils.h"

struct command_line_data cmdline = {
   NULL,   /* dirname */
   NULL,   /* basename */
   NULL,   /* root_dirs */
   NULL,   /* morph_names */
   NULL,   /* header_names */
   false,  /* show_parser_stats */
   false,  /* no_default_names */
   false   /* incremental */
};

static void display_usage(const char *argv0)
{
   bp_display_usage(argv0,
      "[option...] <base-name> [root-dir...]",
      "Builds a binary corpus in the IPI PAN format from XCES source.",
      "  -h, --help              display this help and exit\n"
      "  -v, --version           display version information and exit\n"
      "  -H, --parser-stats      show parser statistics\n"
      "  -e, --header-name=NAME  add NAME as alternative name for header files\n"
      "  -m, --morph-name=NAME   add NAME as alternative name for morph files\n"
      "  -n, --no-default-names  don't allow default names for morph/header files\n"
      "                          (respectively morph.xml and header.xml)\n"
      "  -i, --incremental       perform incremental build\n",
      NULL);
}

void cmdline_process(int argc, char *argv[])
{
   const struct option options[] = {
      { "help",             0, NULL, 'h' },
      { "version",          0, NULL, 'v' },
      { "incremental",      0, NULL, 'i' },
      { "parser-stats",     0, NULL, 'H' },
      { "header-name",      1, NULL, 'e' },
      { "morph-name",       1, NULL, 'm' },
      { "no-default-names", 0, NULL, 'n' },
      { 0,                  0, 0,    0   }
   };
   int opt;
   bool default_names = true;

   while ((opt = getopt_long(argc, argv, "hviHe:m:n", options, NULL)) != -1)
   {
      switch (opt) {
         case 'h':
            display_usage(*argv);
            break;
         case 'v':
            bp_display_version();
            break;
         case 'i':
            cmdline.incremental = true;
            break;
         case 'H':
            cmdline.show_parser_stats = true;
            break;
         case 'e':
            cmdline.header_names = list_prepend(cmdline.header_names,
               list_create(string_init(optarg)));
            break;
         case 'm':
            cmdline.morph_names = list_prepend(cmdline.morph_names,
               list_create(string_init(optarg)));
            break;
         case 'n':
            default_names = false;
            break;
         default:
            exit(EXIT_FAILURE);
      }
   }
   if (default_names) {
      cmdline.header_names = list_prepend(cmdline.header_names,
         list_create(string_init("header.xml")));
      cmdline.morph_names = list_prepend(cmdline.morph_names,
         list_create(string_init("morph.xml")));
   }
   if (optind < argc) {
      string_t path = string_init(argv[optind++]);
      split_pathname(path, &cmdline.dirname, &cmdline.basename);
      string_free(path);
      while (optind < argc)
         cmdline.root_dirs = list_prepend(cmdline.root_dirs,
            list_create(string_init(argv[optind++])));
      if (cmdline.root_dirs == NULL)
         cmdline.root_dirs = list_create(string_init("."));
   } else {
      fputs("bp: corpus basename not given, aborting\n", stderr);
      exit(EXIT_FAILURE);
   }
}
