#!/usr/bin/env python3

# ---------------------------------------------------------------------------
#
# oms2outline, a tool that writes an outline from an OpenManuscript database 
#
# ---------------------------------------------------------------------------

import argparse
import configparser
import os
import json
import openms
import openms.outline


# ---------------------------------------------------------------------------
#
# get initial settings from settings file, if there is one
#
# ---------------------------------------------------------------------------
conf_parser = argparse.ArgumentParser(
    # Turn off help, so we print all options in response to -h
    add_help=False
    )

args, remaining_argv = conf_parser.parse_known_args()

# ---------------------------------------------------------------------------
#
# command line options
#
# ---------------------------------------------------------------------------
# Don't surpress add_help here so it will handle -h
parser = argparse.ArgumentParser(
    # Don't mess with format of description
    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    # Inherit options from config_parser
    parents=[conf_parser]
)

# parser.set_defaults(**defaults)
parser.add_argument( "--authorfile", default=None,
    help="read author data from this file. Must be located in MANUSCRIPTDIR")
parser.add_argument( "--columns", nargs="+", default=None,
    help="set which columns of the outline to print when creating outline")
parser.add_argument( "--excludetags", nargs='+', type=str, default=None,
    help="add chapter tags that are considered 'off'")
parser.add_argument( "--includetags", nargs='+', type=str, default=None,
    help="add chapter tags that are considered 'on'")
parser.add_argument( "--manuscriptdir", default=None,
    help="directory that contains the manuscript data")
parser.add_argument( "--manuscriptfile", default=None,
    help="read manuscript definition from this file. Must be located in MANUSCRIPTDIR")
parser.add_argument( "--outputfile", default=None,
    help="write output to this file")
parser.add_argument( "--settingsfile", default=None,
    help="use settings from this file as command line args")
parser.add_argument( "--specversion", action="store_true", default=None,
    help="report the version of OpenManuscript specification this tool supports")
parser.add_argument( "--version", action="version", 
                        version=str(openms.core.get_version()) ) 

args = parser.parse_args(remaining_argv)


# -----------------------------------------------------------------------------
#
# execute
#
# -----------------------------------------------------------------------------

# set settings from a file, if provided 
if args.settingsfile:
    with open( args.settingsfile ) as sfile:
        settings = json.load( sfile )
        for key in settings:
            openms.core.set( key, settings[key] )

# override settings from command line, if provided
if args.authorfile != None:
    openms.core.set("authorfile", args.authorfile)
if args.columns != None:
    openms.core.set("columns", args.columns)
if args.excludetags != None:
    openms.core.set("excludetags", args.excludetags)
if args.includetags != None:
    openms.core.set("includetags", args.includetags)
if args.manuscriptdir != None:
    openms.core.set("manuscriptdir", args.manuscriptdir)
if args.manuscriptfile != None:
    openms.core.set("manuscriptfile", args.manuscriptfile)
if args.outputfile != None:
    openms.core.set("outputfile", args.outputfile)

# check existence of the files we will need
if not os.path.isfile(openms.core.get_authorfile()):
    print("ERROR: cannot open file: " + openms.core.get_authorfile())
    exit(0)

if not os.path.isfile( openms.core.get_manuscriptfile() ):
    print("ERROR: cannot open file: " + openms.core.get_manuscriptfile())
    exit(0)

# read
openms.core.read_data()

# write
openms.outline.write_outline()
