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

#include <bp/common.h>

#include <bp/ng/list.h>
#include <bp/ng/command-line.h>

struct command_line_data command_line_data = {
   .base_name = NULL,
   .root_dirs = list_empty_body,
   .incremental = false,
   .parallel = false,
};

static void display_usage(const char *argv0)
{
   bp_display_usage(argv0,
      "[option...] <base-name> [xml-root-dir...]",
      "Builds a binary corpus in the IPI PAN format from NKJP or XCES source.",
      "  -h, --help              display this help and exit\n"
      "  -v, --version           display version information and exit\n"
      "  -c, --continue          continue a previous build\n"
#if HAVE_OPENMP
      "  -j, --parallel          use multiple threads of execution\n"
#endif
      , NULL);
}

void command_line_process(int argc, char *argv[])
{
   const struct option options[] = {
      { "help",             0, NULL, 'h' },
      { "version",          0, NULL, 'v' },
      { "incremental",      0, NULL, 'i' }, /* for compatibility with old bp */
      { "continue",         0, NULL, 'c' },
#if HAVE_OPENMP
      { "parallel",         0, NULL, 'j' },
#endif
      { NULL,               0, NULL,  0  }
   };
   int opt;
   const char *optstring =
      "hvic"
#if HAVE_OPENMP
      "j"
#endif
   ;

   while ((opt = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
      switch (opt) {
         case 'h':
            display_usage(*argv);
            break;
         case 'v':
            bp_display_version();
            break;
         case 'c':
         case 'i':
            command_line_data.incremental = true;
            break;
         case 'j':
            command_line_data.parallel = true;
            break;
         default:
            exit(EXIT_FAILURE);
      }
   }
   if (optind < argc) {
      command_line_data.base_name = argv[optind++];
      while (optind < argc)
         list_append(&command_line_data.root_dirs, argv[optind++]);
      if (command_line_data.root_dirs.length == 0 && !command_line_data.incremental)
         list_append(&command_line_data.root_dirs, ".");
   } else
      bp_fatal("corpus base name not given");
}

void command_line_destroy()
{
   list_destroy(&command_line_data.root_dirs, NULL);
   command_line_data.base_name = NULL;
}
