# -*- coding: utf-8 -*-
import sys, os
import globalvars as g

#Add libs to path
for lib in os.listdir("libs" + os.path.sep):
  if not lib == "kbhooklin" and not lib == "kbhookwin":
    sys.path.append("libs" + os.path.sep + lib)

#Add external plugins folder to path
sys.path.append(g.plugins_path)

import pluginsystem
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from QConsole import QConsole
import utils as u

#Create app
app = QtGui.QApplication(sys.argv)
app.setGraphicsSystem("raster")

#Check for read/write perms in user dir and current dir.
prefs = u.canRW(g.user_path)
updatable = u.canRW(os.getcwd())
if not prefs or not updatable:
  if not prefs and updatable:
    u.warnNoPrefs()
  elif prefs and not updatable:
    u.warnNoUpdatable()
  else:
    u.warnNoPrefsNoUpdatable()

#Check for updates
if updatable:
  link = u.checkUpdates()
  if not link == "":
    if u.qupdate():
      window = QDownloader()
      window.url = link
      window.downloadPath = os.getcwd() + os.path.sep + "update" + sys.argv[0]
      window.download()
      u.finishUpdate()
      sys.exit(0)

#Load plugins
pluginsystem.load_plugins()
pluginsystem.trigger_event("init")

window = QConsole()
sys.exit(app.exec_())