"""Main IDJC python package initialiser.

Generated from file __init__.py.in.in -- edit the original.
"""

#   Copyright (C) 2011-2024 Stephen Fairchild (s-fairchild@users.sourceforge.net)
#   This program 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 2 of the License, or
#   (at your option) any later version.
#
#   This program 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 this program in the file entitled COPYING.
#   If not, see <http://www.gnu.org/licenses/>.


import os
import gettext
import ctypes
import sys

import gi
gi.require_version("Gtk", "3.0")
gi.require_version("GLib", "2.0")
gi.require_version("GObject", "2.0")
gi.require_version("Gdk", "3.0")
gi.require_version("GdkPixbuf", "2.0")
from gi.repository import GLib

from .utils import FixedAttributes
from .utils import PolicedAttributes
from .utils import PathStr


def lib_probe(at_runtime, names, extension):
    if at_runtime:
        for name in names.split():
            name += extension
            try:
                ctypes.CDLL(name)
            except Exception:
                continue
            else:
                return True, name
        else:
            return False, ""
    else:
        return True, ""


class FGlobs(metaclass=FixedAttributes):
    """Namespace class of variables which cannot be altered.

    These are set by the build process.
    """

    datarootdir = PathStr("""@datarootdir@""")
    libdir = PathStr("""@libdir@""")
    pkglibdir = PathStr("""${pkglibdir}""")
    pkgdatadir = PathStr("""${pkgdatadir}""")
    darkthemedir = pkgdatadir / "darktheme"
    lightthemedir = pkgdatadir / "lighttheme"
    localedir = PathStr("""@localedir@""")
    bindir = PathStr("""@bindir@""")
    avenabled = (0, 1)[@HAVE_AVFORMAT@ and @HAVE_AVCODEC@ and @HAVE_AVUTIL@]
    flacenabled = @HAVE_FLAC@
    oggflacenabled = @HAVE_OGGFLAC@
    opusenabled = @HAVE_OPUS@
    speexenabled = @HAVE_SPEEX@
    twolameenabled = @HAVE_TWOLAME@
    reallibshoutidjc = @REAL_LIBSHOUTIDJC@
    shouttlsenabled = @HAVE_SHOUT_TLS@

    have_libmp3lame, libmp3lame_filename = lib_probe(@DYN_LAME@,
                                "libmp3lame liblame", """@DYLIB_EXT@""")

    have_libmpg123, libmpg123_filename = lib_probe(@DYN_MPG123@,
                                        "libmpg123", """@DYLIB_EXT@""")

    package_name = """@PACKAGE_NAME@"""
    package_version = """@PACKAGE_VERSION@"""
    backend = PathStr("""${pkglibdir}""") / ("idjc" + """@DYLIB_EXT@""")


_ = gettext.translation(FGlobs.package_name, FGlobs.localedir,
                                                        fallback=True).gettext


class PGlobs(metaclass = PolicedAttributes):
    """Values which cannot be changed once they have been read."""

    config_dir = PathStr(GLib.get_user_config_dir()) / FGlobs.package_name
    #config_dir = PathStr(os.path.expanduser("~")) / ("." + FGlobs.package_name)
    profile_dir = config_dir / "profiles"
    dbus_bus_basename = "net.sf." + FGlobs.package_name
    dbus_objects_basename = "/net/sf/" + FGlobs.package_name
    app_shortform = "IDJC"
    app_longform = "Internet DJ Console"
    jack_prefix = "idjc"
    default_icon = FGlobs.pkgdatadir / "icon.svg"
    autoload_profile_pathname = config_dir / "autoload-profile"
    copyright = _("Copyright 2005-%s Stephen Fairchild and others."
                                                    ) % """@COPYRIGHT_YEAR@"""
    license = _("Released under the GNU General Public License V2.0+.")
    num_micpairs = 2
    num_streamers = 6
    num_encoders = 6
    num_recorders = 2
    num_mini_players = 36
    num_panpresets = 3
    theme = "lighttheme"
    themedir = FGlobs.lightthemedir


def main():
    """Package execution entry point."""

    from .prelims import ProfileManager
    pm = ProfileManager()

    print(FGlobs.package_name, FGlobs.package_version)
    print(sys.implementation.name, sys.version)

    if pm.testing:
        from .testing import main
    else:
        from .maingui import main

    return main()
