#!/usr/bin/python3

from sys import path
import os, argparse

MAINFOLDER = "/usr/share/connector"
path.append(MAINFOLDER)
pwd = os.getcwd() + '/'
os.chdir (MAINFOLDER)
from GLOBAL import (VERSION, RELEASE)

def parseArgs():
    """Description of the command line argument parser"""
    about = "Connector - %s (%s)" % (VERSION, RELEASE)
    args = argparse.ArgumentParser(prog = 'connector', formatter_class=argparse.RawTextHelpFormatter,
                                   description = 'Connector - remote desktop chooser.\n\nDo not specify parameters for starting the GUI.')
    args.add_argument ( '--disable-kiosk', action = 'store_true', default = False, help = "disable the mode KIOSK" )
    args.add_argument ('-v', '--version', action = 'version', help = "show the application version", version = about)
    args.add_argument ('-d', '--debug', action = 'store_true', default = False, help = "show log files online")
    args.add_argument ('-q', '--quit', action = 'store_true', default = False, help = "quit the application")
    args.add_argument('name', type = str, nargs = '?', help = 'name of the file (.ctor, .remmina, .rdp) or saved connection')
    return args.parse_args()

if __name__ == '__main__':
    os.system("xdg-mime default connector.desktop application/x-connector")
    args = parseArgs()
    if args.quit:
        from gui import quitApp as quit
        quit()
    if args.disable_kiosk:
        if os.getuid() == 0:
            try:
                from kiosk.kiosk import disable_kiosk
                disable_kiosk()
                exit (0)
            except ImportError:
                print ( "The mode KIOSK unavailable, package is not installed." )
                exit (127)
        else:
            print ( "Permission denied!" )
            exit (126)
    if args.debug:
        from gui import startDebug as debug
        debug()
    from gui import f_main as run
    run (pwd, args.name)
