#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Copyright (C) 2015 Mathieu D. (MatToufoutu)
#
#    This program 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 3 of the License, or
#    (at your option) any later version.
#
#    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
import os
from argparse import ArgumentParser
from logging import DEBUG, INFO

from ToolBoxAssistant import ToolBoxAssistant
from ToolBoxAssistant.log import logger


tba = ToolBoxAssistant()
parser = ArgumentParser(description='Easily manage your toolbox applications.')
parser.add_argument(
    '-f', '--file',
    help='toolbox specfile to use (default: toolbox.json)',
    default='toolbox.json'
)
parser.add_argument(
    '-v', '--verbose',
    help='display debug information',
    action='store_true'
)
subparsers = parser.add_subparsers(
    title='Subcommands',
    help='(use "%(prog)s <cmd> -h" for commands help)',
    dest='action'
)

sync_parser = subparsers.add_parser(
    'sync',
    help='synchronize installed applications with specfile'
)
sync_parser.add_argument(
    '-u', '--unlisted',
    help='list installed applications missing from specfile',
    action='store_true'
)
sync_parser.set_defaults(func=tba.do_sync)

genspec_parser = subparsers.add_parser(
    'genspec',
    help='generate specfile from installed applications'
)
genspec_parser.add_argument(
    'path',
    help='toolbox folder to scan for applications'
)
genspec_parser.add_argument(
    '-m', '--merge',
    help='merge found applications with existing specfile',
    metavar='FILE'
)
genspec_parser.set_defaults(func=tba.do_genspec)
args = parser.parse_args()
if not args.file.startswith(os.path.sep):
    args.file = os.path.join(os.getcwd(), args.file)
logger.setLevel(DEBUG if args.verbose else INFO)
args.func(args)
