#!python
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 09:58:27 2019

@author: ltetrel
"""

import argparse
import os
import codecs
from repo2data.repo2data import Repo2Data

def read(relative_path):
    """Read the curent file.
    Parameters
    ----------
        relative_path : string, required
            relative path to the file to be read, from the directory of this file
    
    Returns
    -------
        string : content of the file at relative path
    """
    here = os.path.abspath(os.path.dirname(__file__))
    with codecs.open(os.path.join(here, relative_path), 'r') as fp:
        return fp.read()

def get_version():
    """Get the version of this software, as describe in the __init__.py file from the top module.
    
    Returns
    -------
        string : version of this software
    """
    os.path.dirname(__file__)
    relative_path = os.path.join(os.path.dirname(__file__), "..", "repo2data", "__init__.py")
    for line in read(relative_path).splitlines():
        if line.startswith('__version__'):
            delim = '"' if '"' in line else "'"
            return line.split(delim)[1]
    else:
        raise RuntimeError("Unable to find version string.")

def get_parser():
    parser = argparse.ArgumentParser(
            formatter_class=argparse.RawDescriptionHelpFormatter
            , description=""
            , epilog="""
            Documentation at https://github.com/SIMEXP/Repo2Data
            """)

    parser.add_argument(
            "-r"
            , "--data_requirement"
            , required=False
            , default=None
            , help="Data requirement file (can be a https://github repo), Default: current directory",
            )
    
    parser.add_argument(
            "--server"
            , action="store_true"
            , required=False
            , default=False
            , help="Disabling the dst option for the user, Default: False",
            )

    parser.add_argument(
            "--version"
            , action="version"
            , version=get_version())

#    parser.add_argument(
#            "-l"
#            , "--log_level"
#            , required=False
#            , default="INFO"
#            , choices=["DEBUG","INFO","WARNING","ERROR","CRITICAL"]
#            , help="Set logging level",
#            )
   
    return parser

def main():
    args = get_parser().parse_args()
    repo2data = Repo2Data(**vars(args))
    repo2data.install()
    
if __name__ == '__main__':
    main()
