#! /usr/bin/python3
# -*- encoding: utf-8 -*-
#
# The MIT License (MIT)
#
# Copyright (c) 2016-2018 Tobias Koch <tobias.koch@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import os
import sys
import getopt
import logging
import textwrap

# make relocatable
INSTALL_DIR = os.path.normpath(os.path.dirname(
    os.path.realpath(sys.argv[0])) + os.sep + ".." )
sys.path.insert(1, INSTALL_DIR + os.sep + 'lib')

from aeltra.distro.config.distroinfo import DistroInfo
from aeltra.error import AeltraError, InvocationError

from aeltra.miscellaneous.appconfig import AppConfig
from aeltra.miscellaneous.logformatter import LogFormatter
from aeltra.miscellaneous.switch import switch

from aeltra.package.deb2aeltra.debianpackagecache import DebianPackageCache
from aeltra.package.deb2aeltra.constants import (
    DEBIAN_ARCHIVE_KEYRING_FILE,
    DEBIAN_ARCHIVE_REMOVED_KEYS_FILE
)
from aeltra.package.deb2aeltra.converter import Deb2AeltraPackageConverter

AELTRA_ERR_INVOCATION = 1
AELTRA_ERR_RUNTIME    = 2

LOGGER = logging.getLogger()

def print_usage():
    print(textwrap.dedent(
        """\
        Copyright (C) 2016-2021 Tobias Koch <tobias.koch@gmail.com>

        This tool converts Debian source packages to Aeltra source packages with some
        loss of information.

        USAGE:

          deb2aeltra [OPTIONS] <pkg_name>

        OPTIONS:

          -h, --help              Print this help message.

          --arch=<arch>           Analyze packages for given arch (default: amd64).
          --release=<release>     The Debian release to work against (default: stable).

          --run-rules=<target>    Invoke debian/rules target after unpacking the
                                  sources. This option can be supplied multiple times.

          --create-patch-tarball  Put the patches into a tarball instead of copying
                                  them to the "patches" folder.
          --disable-updates       Ignore the release-updates pocket.
          --disable-security      Ignore security updates.

          --set-maintainer        Write maintainer info to changelog (initial import).

          --no-update-cache       Do not update the package cache.
          --no-load-contents      Do not load and analyze binary package contents.
          --no-gpg-checks         Do not verify GPG signature on Release files.
        """
    ))
#end function

def parse_cmd_line():
    # define default configuration
    config = {
        "release":
            None,
        "arch":
            "amd64",
        "create_patch_tarball":
            False,
        "do_gpg_checks":
            True,
        "do_update_cache":
            True,
        "do_load_contents":
            True,
        "set_maintainer":
            False,
        "updates_enabled":
            True,
        "security_enabled":
            True,
        "run_rules":
            []
    }

    try:
        opts, args = getopt.getopt(sys.argv[1:], "h",
            [
                "help",
                "set-maintainer",
                "create-patch-tarball",
                "disable-updates",
                "disable-security",
                "no-gpg-checks",
                "no-update-cache",
                "no-load-contents",
                "arch=",
                "release=",
                "run-rules="
            ]
        )
    except getopt.GetoptError as e:
        raise InvocationError("error parsing command line: {}".format(str(e)))

    for o, v in opts:
        for case in switch(o):
            if case("--help", "-h"):
                print_usage()
                sys.exit(0)
                break
            if case("--set-maintainer"):
                config["set_maintainer"] = True
                break
            if case("--create-patch-tarball"):
                config["create_patch_tarball"] = True
                break
            if case("--disable-updates"):
                config["updates_enabled"] = False
                break
            if case("--disable-security"):
                config["security_enabled"] = False
                break
            if case("--no-gpg-checks"):
                config["do_gpg_checks"] = False
                break
            if case("--no-update-cache"):
                config["do_update_cache"] = False
                break
            if case("--no-load-contents"):
                config["do_load_contents"] = False
                break
            if case("--arch"):
                config["arch"] = v.strip()
                break
            if case("--release"):
                config["release"] = v.strip()
                break
            if case("--run-rules"):
                config["run_rules"].append(v.strip())
                break
        #end switch
    #end for

    return config, args
#end function

def configure_logging():
    fmt = LogFormatter("deb2aeltra")
    handler = logging.StreamHandler()
    handler.setFormatter(fmt)
    LOGGER.addHandler(handler)
    LOGGER.setLevel(logging.INFO)
#end function

if __name__ == "__main__":
    try:
        options, args = parse_cmd_line()

        if len(args) != 1:
            print_usage()
            sys.exit(AELTRA_ERR_INVOCATION)
        #end if

        configure_logging()

        if not options["set_maintainer"]:
            maintainer_info = None
        else:
            maintainer_info = AppConfig().get("maintainer-info", {})
            if not maintainer_info:
                raise InvocationError(
                    "no maintainer-info found, please fix your config file."
                )
            #end if
        #end if

        distro_info = DistroInfo()

        if not options.get("release"):
            try:
                latest_aeltra_release = \
                    list(distro_info.list(supported=True).values())[-1]
                options["release"] = latest_aeltra_release \
                    .get("upstream", {}) \
                    .get("id")
            except KeyError:
                pass

            if not options["release"]:
                raise AeltraError(
                    "unable to determine latest upstream release and no "
                    "release specified."
                )
            #end if
        #end if

        keyring = None

        if options["do_gpg_checks"]:
            keyrings = [
                DEBIAN_ARCHIVE_KEYRING_FILE,
                DEBIAN_ARCHIVE_REMOVED_KEYS_FILE
            ]

            for k in keyrings:
                if not os.path.exists(k):
                    raise AeltraError(
                        "GPG keyring file '{}' not found.".format(k)
                    )
                #end if
            #end for
        #end if

        pkg_cache = DebianPackageCache(
            options["release"],
            arch=options["arch"],
            updates_enabled=options["updates_enabled"],
            security_enabled=options["security_enabled"],
            keyrings=keyrings
        )

        if options["do_update_cache"]:
            pkg_cache.update()
        else:
            pkg_cache.reload()

        # RUN ACTION
        deb2aeltra = Deb2AeltraPackageConverter(
            pkg_cache,
            release=options["release"],
            arch=options["arch"]
        )

        deb2aeltra.convert(
            args[0],
            maintainer_info=maintainer_info,
            run_rules=options["run_rules"],
            do_load_contents=options["do_load_contents"],
            create_patch_tarball=options["create_patch_tarball"],
        )
    except InvocationError as e:
        LOGGER.error(e)
        sys.exit(AELTRA_ERR_INVOCATION)
    except AeltraError as e:
        LOGGER.error(e)
        sys.exit(AELTRA_ERR_RUNTIME)
    except KeyboardInterrupt:
        LOGGER.warning("caught keyboard interrupt, exiting.")
        sys.exit(AELTRA_ERR_RUNTIME)
    #end try
#end __main__
