#!/usr/bin/python3

# newelle.in
#
# Copyright 2023 Yehor Qwersyk
#
# 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 3 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.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import sys
import signal
import locale
import gettext
import argparse

VERSION = '1.4.5'
pkgdatadir = '/usr/share/newelle'
localedir = '/usr/share/locale'

sys.path.insert(1, pkgdatadir)
signal.signal(signal.SIGINT, signal.SIG_DFL)
locale.bindtextdomain('newelle', localedir)
locale.textdomain('newelle')
gettext.install('newelle', localedir)

def parse_args():
    parser = argparse.ArgumentParser(prog='newelle', add_help=False)
    parser.add_argument('--interface', '-I', metavar='INTERFACE',
                        help='Start an interface without the GUI (e.g. api, gui-api)')
    parser.add_argument('--list-interfaces', action='store_true',
                        help='List available interfaces and exit')
    args, remaining = parser.parse_known_args()
    return args, remaining

if __name__ == '__main__':
    args, remaining = parse_args()

    if args.list_interfaces:
        from gi.repository import Gio
        from newelle.constants import AVAILABLE_INTERFACES
        print("Available interfaces:")
        for key, info in AVAILABLE_INTERFACES.items():
            print(f"  {key:15s} - {info['title']}")
        sys.exit(0)

    if args.interface:
        import gi
        gi.require_version('Gtk', '4.0')
        gi.require_version('Adw', '1')
        from gi.repository import Gio
        resource = Gio.Resource.load(os.path.join(pkgdatadir, 'newelle.gresource'))
        resource._register()

        from newelle import main
        sys.exit(main.run_headless(args.interface, VERSION))

    import gi

    from gi.repository import Gio
    resource = Gio.Resource.load(os.path.join(pkgdatadir, 'newelle.gresource'))
    resource._register()

    from newelle import main
    sys.exit(main.main(VERSION))
