# Fetch package support from debian-security-support (for lts-cve-triage.py)
# Copyright (C) 2016, 2018  Chris Lamb <lamby@debian.org>
# Copyright (C) 2016  Guido Günther <agx@sigxcpu.org>
# Copyright (C) 2022, 2023  Emilio Pozuelo Monfort <pochu@debian.org>
# Copyright (C) 2024  Sylvain Beucler <beuc@beuc.net>
# Copyright (C) 2025  Jochen Sprickerhof <git@jochen.sprickerhof.de>
#
# This file 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 file 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 file.  If not, see <https://www.gnu.org/licenses/>.

import setup_paths  # noqa # pylint: disable=unused-import

import config
import os
import re
import requests

re_line_limited = re.compile(r'(?!#)(?P<pkg>[^\s]+)\s*limited')
re_line_unsupported = re.compile(r'(?!#)(?P<pkg>[^\s]+)\s*non-supported')
# TODO: support new "supported" line that overrides the above;
#       adapt code in lts-cve-triage as well;
#       https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1108170#20


class DebSecSupport(set):
    def __init__(self, update_cache, re_line):
        self.re_line = re_line
        if update_cache:
            self.update_cache()

        self.load()

    def update_cache(self):
        print("Updating {} from {} ...".format(self.cache, self.url))

        response = requests.get(self.url, allow_redirects=True)
        response.raise_for_status()

        with open(self.cache, 'w') as f:
            f.write(response.text)

    def load(self):
        with open(self.cache, 'r') as f:
            for x in f.readlines():
                m = self.re_line.match(x)

                if m is not None:
                    self.add(m.group('pkg'))


class UnsupportedPackages(DebSecSupport):
    URL = "https://salsa.debian.org/debian/debian-security-support/raw/master/security-support.deb{}"
    CACHED_DATA_PATH = "~/.cache/security-support-ended.deb{}"

    def __init__(self, codename, update_cache=True):
        # codename to version number
        dists = list(config.get_config().keys())
        self.debian_version = dists.index(codename) + 1

        self.url = self.URL.format(self.debian_version)

        self.cache = os.path.expanduser(self.CACHED_DATA_PATH).format(
            self.debian_version,
        )

        super(UnsupportedPackages, self).__init__(update_cache, re_line_unsupported)


class LimitedSupportPackages(DebSecSupport):
    URL = "https://salsa.debian.org/debian/debian-security-support/raw/master/security-support.deb{}"
    CACHED_DATA_PATH = "~/.cache/security-support-limited.deb{}"

    def __init__(self, codename, update_cache=True):
        # codename to version number
        dists = list(config.get_config().keys())
        self.debian_version = dists.index(codename) + 1

        self.url = self.URL.format(self.debian_version)

        self.cache = os.path.expanduser(self.CACHED_DATA_PATH).format(
            self.debian_version,
        )

        super(LimitedSupportPackages, self).__init__(update_cache, re_line_limited)
