#!/usr/bin/env python3
#
#

""" Timeline Console Bot. """

import warnings
warnings.simplefilter("ignore")

## bootstrap

import os, sys
sys.path.insert(0, os.getcwd())
sys.path.insert(0, os.getcwd() + os.sep + "..")

from tl.version import getversion
from tl.utils.opts import setopts, do_opts
from tl.utils.log import setloglevel
from tl.lib.boot import boot, plugin_packages

import getpass

## command line parsing

opts, cfg = do_opts(type="console")

## boot the bot

boot(opts.datadir)

## tl imports

from tl.utils.exception import handle_exception
from tl.utils.generic import waitforqueue, waitevents
from tl.drivers.console.bot import ConsoleBot
from tl.lib.config import Config
from tl.lib.errors import NoOwnerSet, NoSuchCommand
from tl.lib.commands import cmnds
from tl.lib.exit import globalshutdown
from tl.lib.fleet import getfleet
from tl.lib.threads import start_new_thread
from tl.lib.datadir import getdatadir
from tl.id import get_id

## basic imports

import getpass
import time
import logging

## start the bot

try:
    bot = ConsoleBot(cfg)
except NoOwnerSet:
    print("the owner is not set in %s" % cfg.cfile)
    os._exit(1)

if opts.args:
    cmndstring = ";"
    for cmnd in opts.args:
        if "|" in cmnd: break
        cmndstring += "%s " % cmnd
    id = get_id()
    event = bot.make_event(id, opts.channel or id, cmndstring.strip(), showall=True)
    event.nodispatch = False
    try: event.execute(True)
    except NoSuchCommand as ex: print("no %s command found." % str(ex).strip())
    print("")
    try: sys.stdout.close()
    except: pass
    os._exit(0)

## starting the bot

bot.start(False)
fleet = getfleet()
fleet.addbot(bot)

## if opts is provided, start the fleet

if opts.fleet:
    bots = fleet.loadall()
    logging.error("starting fleet %s" % fleet.list())
    start_new_thread(fleet.startall, ())

bot.startshell()

## on exit check if we need to print exceptions that occured

if opts.exceptions and not opts.bork:
    from tl.utils.exception import exceptionevents, exceptionlist
    for (event, msg) in exceptionevents: print("EXCEPTION: %s - %s" % (event.txt,msg))
    for msg in exceptionlist: print("EXCEPTION: %s" % msg)

## mzzl

globalshutdown()
