#!/usr/bin/python3 -sP
# -*- coding: utf-8 -*-
#  kodos.py: -*- Python -*-  DESCRIPTIVE TEXT.

import sys
import os
import logging
import optparse

try:
    from PyQt5 import QtWidgets
    from PyQt5 import QtCore
except:
    sys.exit("""Could not locate the PyQt module.  Please make sure that
you have installed PyQt for the version of Python that you are running.""")

from kodos.main import Kodos
from kodos.util import findFile

parser = optparse.OptionParser()
parser.add_option('-f', '--filename', dest='filename',
                  help='Load filename on startup', metavar='FILE')
parser.add_option('-d', '--debug', dest='debug', action='store_true',
                  help='Set log level to debug')
parser.add_option('-k', dest='kodos_dir', type='string',
                  default=os.path.join(sys.prefix, "kodos"),
                  help='Set path containing Kodos images & help subdirs',
                  metavar='DIR')
parser.add_option('-l', '--locale', dest='locale', type='string',
                  default='en',
                  help='2-letter locale', metavar='LEVEL')
options, args = parser.parse_args()

if options.debug:
    logging.basicConfig(level=logging.DEBUG)
else:
    logging.basicConfig(level=logging.WARNING)
log = logging.getLogger('kodos')

os.environ['KODOS_DIR'] = options.kodos_dir

qApp = QtWidgets.QApplication(sys.argv)
qApp.setOrganizationName("kodos")
qApp.setApplicationName("kodos")
qApp.setOrganizationDomain("kodos.sourceforge.net")

if options.locale != 'en':
    localefile = "kodos_%s.qm" % (options.locale or QtCore.QTextCodec.locale())
    localepath = findFile(os.path.join("translations", localefile))
    log.debug('locale changed to: %s, file: %s, path: %s' % (locale,
                                                             localefile,
                                                             localepath))

    translator = QtCore.QTranslator(qApp)
    translator.load(localepath)
    qApp.installTranslator(translator)

kodos = Kodos(qApp, options.filename)
kodos.show()
sys.exit(qApp.exec_())
