#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# This source file is part of Yoda.
#
# Yoda 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.
#
# Yoda 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
# Yoda. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
import argcomplete
import logging
import os

from yoda import Config
from yoda import Logger
from yoda import Subcommands

from yoda.subcommand import Jump
from yoda.subcommand import Show
from yoda.subcommand import Status
from yoda.subcommand import Update

from yoda.subcommand import Workspace

logging.setLoggerClass(Logger)
logger = logging.getLogger("yoda")

yoda_config = Config("%s/.yodarc" % os.environ.get("HOME"))

logfile = "%s/.yoda.log" % os.environ.get("HOME")
if "logfile" in yoda_config:
    logfile = yoda_config["logfile"]

logger.set_file_handler(logfile)

subcmd = Subcommands(yoda_config)
subcmd.add_command(Show())
subcmd.add_command(Status())
subcmd.add_command(Update())
subcmd.add_command(Jump())
ws_cmd = Workspace()
subcmd.add_command(ws_cmd)
ws_cmd.load_workspaces_subcommands(subcmd)
subcmd.parse()

argcomplete.autocomplete(subcmd.parser)

args = subcmd.parser.parse_args()
logger.set_console_handler(True if args.debug else False)

try:
    subcmd.execute(args)
except Exception as e:
    logger = logging.getLogger(__name__)
    logger.error(e)
