#!/usr/bin/env python
# -*- coding: utf-8

import sys

import anvio
import anvio.dbops as dbops
import anvio.terminal as terminal

from anvio.errors import ConfigError, FilesNPathsError, SamplesError


__author__ = "A. Murat Eren"
__copyright__ = "Copyright 2015, The anvio Project"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "A. Murat Eren"
__email__ = "a.murat.eren@gmail.com"


run = terminal.Run()
progress = terminal.Progress()


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description="Create a new anvi'o samples information database.")

    groupA = parser.add_argument_group('BIG INPUTS', "Full-blown input files as described in http://merenlab.org/2015/11/10/samples-db/.\
                                        You don't have to have both, either of them will also work.")
    groupA.add_argument(*anvio.A('samples-information-file'), **anvio.K('samples-information-file'))
    groupA.add_argument(*anvio.A('samples-order-file'), **anvio.K('samples-order-file'))

    groupB = parser.add_argument_group('SMALL INPUTS', "For lazy people.")
    groupB.add_argument(*anvio.A('single-order-file'), **anvio.K('single-order-file'))
    groupB.add_argument(*anvio.A('order-name'), **anvio.K('order-name'))

    groupC = parser.add_argument_group('OUTPUT', "The output business.")
    groupC.add_argument(*anvio.A('output-db-path'), **anvio.K('output-db-path', {'default': 'SAMPLES.db'}))

    args = parser.parse_args()

    try:
        s = dbops.SamplesInformationDatabase(args.output_db_path, run, progress, quiet=False)
        s.create(args.samples_information_file, args.samples_order_file, args.single_order_file, args.order_name)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-2)
    except SamplesError as e:
        print(e)
        sys.exit(-3)
