#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0+
# License-Filename: LICENSES/GPL-3.0

import gettext
import logging
import locale
import os.path
import optparse
import signal
import sys

import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
gi.require_version("GDesktopEnums", "3.0")

import gtweak
from gtweak.defs import VERSION, APP_ID, GSETTINGS_SCHEMA_DIR, TWEAK_DIR, DATA_DIR, \
                                PKG_DATA_DIR, LOCALE_DIR

def set_internationalization(domain, locale_dir):
    """
    Initialize gettext and locale domains
    """

    try:
        locale.bindtextdomain(domain, locale_dir)
        locale.textdomain(domain)
    except AttributeError as e:
        # Python built without gettext support does not have
        # bindtextdomain() and textdomain().
        logging.error(
            "Could not bind the gettext translation domain. Some translations will not work.",
            exc_info=e,
        )

    gettext.bindtextdomain(domain, locale_dir)
    gettext.textdomain(domain)

    gettext.install(domain, names=('gettext', 'ngettext'))

def setup_defs_for_prefix(prefix: str):
    """
    Setup the directory definitions given a prefix
    """
    
    gtweak.DATA_DIR = os.path.join(prefix, "share")
    gtweak.LOCALE_DIR = os.path.join(prefix, "share", "locale")
    gtweak.GSETTINGS_SCHEMA_DIR = os.path.join(prefix, "share", "glib-2.0", "schemas")

    me = os.path.abspath(os.path.dirname(__file__))
    gtweak.TWEAK_DIR = os.path.join(me, "gtweak", "tweaks")
    gtweak.PKG_DATA_DIR = os.path.join(me, "data")


def setup_defs(prefix: str | None = None):
    """
    Setup the directory definitions
    """
    
    gtweak.APP_ID = APP_ID

    if prefix:
        setup_defs_for_prefix(prefix)
    else:
        gtweak.GSETTINGS_SCHEMA_DIR = GSETTINGS_SCHEMA_DIR
        gtweak.TWEAK_DIR = TWEAK_DIR
        gtweak.DATA_DIR = DATA_DIR
        gtweak.PKG_DATA_DIR = PKG_DATA_DIR
        gtweak.LOCALE_DIR = LOCALE_DIR
    

if __name__ == '__main__':
    parser = optparse.OptionParser(version=VERSION)
    parser.add_option("-p", "--prefix",
                      help="Installation prefix (for gsettings schema, themes, etc)",
                      metavar="[/, /usr]")
    parser.add_option("-v", "--verbose", action="store_true",
                      help="Print the names of settings modified")
    parser.add_option("-d", "--debug", action="store_true",
                      help="Enable debug output")
    parser.add_option("--gapplication-service", action="store_true")
    options, args = parser.parse_args()

    setup_defs(options.prefix or None)

    gtweak.VERBOSE = options.verbose

    if options.debug:
        level = logging.DEBUG
    else:
        level = logging.WARNING

    logging.basicConfig(format="%(levelname)-8s: %(message)s", level=level)

    set_internationalization(gtweak.APP_NAME, LOCALE_DIR)

    from gtweak.app import GnomeTweaks

    app = GnomeTweaks()
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    exit_status = app.run(sys.argv)
    sys.exit(exit_status)
