#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-
##
## This file is part of CDS Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 CERN.
##
## CDS Invenio 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.
##
## CDS Invenio 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 CDS Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""

   "refextract" is used to extract and process the "references"
   or "citations" made to other documents from within a document.
   A document's "references" section is usually found at the end of
   the document, and generally consists of a list of the works
   cited during the course of the document.
   "bibrefextract" can attempt to identify a document's references
   section and extract it from the document.  It can also attempt
   to standardise the references (correct the names of journals
   etc so that they are written in a standard format), and mark them
   up so that they can be linked to the full articles on the Web by
   means of hyper-links.

   "bibrefextract" has 4 phases of processing (passes):
    1. Convert PDF file to plaintext (UTF-8).
    2. Extract References from plaintext.
    3. Recognise and standardise citations in the extracted
       reference lines. (Periodical titles and institutional
       report numbers are standardised with the aid of
       dedicated knowledge-bases.)
    4. Markup standardised citations in MARC XML and output
       them.

    If not running in standalone mode (I.e., without the -i or --standalone flags)
	---> calls the bibsched compliant command line interface refextract module

Options:
   -h, --help     print this help
   -V, --version  print version information
   -v, --verbose  verbosity level (0=mute, 1=default info msg,
                  2=display reference section extraction analysis,
                  3=display reference line citation processing analysis,
                  9=max information)
   -r, --output-raw-refs
                  output raw references, as extracted from the document.
                  No MARC XML mark-up - just each extracted line, prefixed
                  by the recid of the document that it came from.
   -x, --xmlfile
                  write xml output to a file rather than standard output.
   -d, --dictfile
                  write statistics about all matched title abbreviations
                  (i.e. LHS terms in the titles knowledge base) to a file.
   -z, --raw-references
                  treat the input file as pure references. i.e. skip the stage
                  of trying to locate the reference section within a document
                  and instead move to the stage of recognition and
                  standardisation of citations within lines.
   -i, --standalone
                  Run refextract in standalone mode. 
                  I.e., without scheduling the job (Runs
                  independently from the rest of the 
                  Invenio system.)

"""

import sys

def check_args():

    print "Running refextract in standalone mode."
    try:
        from invenio.refextract import main
    except ImportError, err:
        sys.stderr.write("Error: %s" % err)
        sys.stderr.flush()
        sys.exit(1)

    # Run the initialising refextract function
    main()
    print "Extraction complete."


if __name__ == '__main__':
    check_args()
