#!python

"""
Copyright (c) 2019-2020, Ian Santopietro
All rights reserved.

This file is part of RepoLib.

RepoLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RepoLib 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with RepoLib.  If not, see <https://www.gnu.org/licenses/>.
"""
#pylint: disable=invalid-name
# Pylint will complain about our module name not being snake_case, however this
# is a command rather than a python module, and thus this is correct anyway.

import argparse
import logging
import os
import sys

import repolib
from repolib import command

SOURCES_DIR = '/etc/apt/sources.list.d'

def main(options=None):
    """ Main function for apt-manage."""
    # Set up Argument Parsing.
    parser = repolib.command.parser

    # Parse options
    args = parser.parse_args()
    if options:
        args = parser.parse_args(options)

    if not args.debug:
        args.debug = 0

    if args.debug > 2:
        args.debug = 2

    verbosity = {
        0 : logging.WARN,
        1 : logging.INFO,
        2 : logging.DEBUG
    }

    log = logging.getLogger('apt-manage')
    handler = logging.StreamHandler()
    formatter = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
    handler.setFormatter(formatter)
    log.addHandler(handler)
    log.setLevel(verbosity[args.debug])
    log.debug('Logging set up!')

    if not args.action:
        args = parser.parse_args(sys.argv[1:] + ['list'])
    
    log.debug('Arguments passed: %s', str(args))
    log.debug('Got command: %s', args.action)

    subcommand = args.action.capitalize()

    command = getattr(repolib.command, subcommand)(log, args, parser)
    result = command.run()
    if not result:
        sys.exit(1)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('')
        sys.exit(130)
