#!/usr/bin/env python2

##
# PyChat
# https://github.com/leosartaj/PyChat.git
#
# Copyright (c) 2014 Sartaj Singh
# Licensed under the MIT license.
##

# system imports
import sys
import os

# twisted imports
from twisted.internet import gtk2reactor
gtk2reactor.install() # install reactor for gui
from twisted.internet import reactor
from twisted.python import log
from twisted.internet import defer

# Other imports
from PyChat.client.gui.clientGUIClass import clientGUIClass # get the main gui class
from PyChat.client.gui.helper import helperFunc as hf
from PyChat.client.options import parse_args

if __name__ == '__main__':
    options, host = parse_args() # parse the arguments

    try:
        # Start Logging
        # Logs in pwd as pychat.log by default
        log.startLogging(open(options.log , 'a'))
    except Exception, e:
        print 'Cannot Start PyChat'
        print e
        sys.exit(1) # end program if invalid log file

    gui = clientGUIClass(options.client) # start the gui

    if host != None and options.port != None: # allow deafult connecting
        if hf.validate_host(host):
            gui.connect(host, options.port)

    reactor.run()
