#! /usr/bin/python3
# -*- coding: utf-8 -*-

# qarte
# This file is part of qarte-5
#    
# Author: Vincent Vande Vyvre <vincent.vandevyvre@oqapy.eu>
# Copyright: 2011-2025 Vincent Vande Vyvre
# Licence: GPL3
# Home page: https://launchpad.net/qarte
#
#
# Qarte is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Qarte is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Qarte.  If not, see <http://www.gnu.org/licenses/>.
#
# Launcher of Qarte

import os
import sys
import logging
import platform
import locale

VERSION_STR = "5.14.0"

from PyQt5.QtWidgets import QApplication

def show_infos(arg):
    """Show info and exit."""
    if arg in ["-v", "--version"]:
        print("""\n\tQarte vers: {0}

    Copyright (C) 2011-2025 Vincent Vande Vyvre <vincent.vandevyvre@oqapy.eu>
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
\n""".format(VERSION_STR))

    else:
        print("""\n\tQarte is a recorder for the archivs of arte TV Guide
<http://www.arte.tv/guide/> and arte Concert <http://concert.arte.tv/>.

\tUsage: qarte [OPTION]

\tOptions:
\t  -v --version\t Show version and copyright infos
\t  -h --help\t Show this info
\t  -d --debug\t Run Qarte in mode verbose
\t  --no-player\t Disable the video player in the concerts page
\t  --with-player\t Enable the video player in the concerts page

\tReport bugs to <[2;34mhttps://code.launchpad.net/qarte[0;39m>\n""")
    sys.exit()

def print_infos():
    PY_VERSION = sys.version.split()[0]
    PLATFORM = platform.platform()
    logger.info("Qarte-{0}".format(VERSION_STR))
    logger.info("Python {0} on {1}".format(PY_VERSION, PLATFORM))
    logger.info("File system encoding: {0}".format(sys.getfilesystemencoding()))
    logger.info("System encoding: {0}".format(sys.getdefaultencoding()))
    logger.info("Locale encoding: {0}".format(locale.getlocale()))

def get_installation_path():
    paths = ("/usr/share/qarte", "/usr/local/share/qarte")
    for p in paths:
        if os.path.isdir(p):
            return p

    return False


if __name__ == "__main__":
    logger = logging.getLogger("qarte")
    lvl = logging.DEBUG
    LOG_FORMAT = "%(asctime)-6s: %(levelname)s - %(name)s %(message)s"
    logging.basicConfig(format=LOG_FORMAT, level=lvl, datefmt='%H:%M:%S')

    install_path = get_installation_path()
    if not install_path:
        logger.critical("Qarte not found in '/usr/share/' or '/usr/local/share/'")
        sys.exit()
    
    args = sys.argv
    if len(args) > 1:
        if args[1] in ["-v", "--version", "-h", "--help"]:
            show_infos(args[1])

        if args[1] == "-a":
            os.chdir(install_path)
            daemon = os.path.join(install_path, "daemon.py")
            if not os.path.isfile(daemon):
                logger.critical("File '%s' not found." % daemon)
                logger.critical("Please re-install Qarte")
                sys.exit()

            sys.path.insert(0, install_path)
            from daemon import Daemon
            qarte = Daemon(args[2])
            reply = qarte.read_task()
            sys.exit()

        if args[1] in ["-d", "--debug"]:
            print_infos()

    os.chdir(install_path)
    sys.path.insert(0, install_path)
    app = QApplication(args)
    from core import Core
    main = Core(args)
    sys.exit(app.exec_())
