#!/usr/bin/env python3
#
# Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.com>
#
# 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 <https://www.gnu.org/licenses/>.

import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timedelta
from launchpadlib.launchpad import Launchpad

now = datetime.now()

def print_log(string):
    global now
    old_now = now
    now = datetime.now()
    time_elapsed = now - old_now
    print(f"[{now}] (took {time_elapsed}) {string}")

parser = argparse.ArgumentParser()
parser.add_argument("release")
args = parser.parse_args()

print(f"[{now}] Logging into Launchpad...")
launchpad = Launchpad.login_with("grim-reaper", "production", version="devel")

print_log("Logged in. Initializing repositories...")
ubuntu = launchpad.distributions["ubuntu"]
series = ubuntu.getSeries(name_or_version=args.release)
lubuntu_ci = launchpad.people["lubuntu-ci"]
regular = lubuntu_ci.getPPAByName(distribution=ubuntu, name="unstable-ci")
proposed = lubuntu_ci.getPPAByName(distribution=ubuntu, name="unstable-ci-proposed")

print_log("IS THAT THE GRIM REAPER?!?!?!?!!!")

# Fetch packages once
packages = []
for repository in [proposed, regular]:
    packages.append(repository.getPublishedSources(status="Superseded", distro_series=series))
total_removals = sum(len(repository) for repository in packages)

print_log(f"Total packages to remove: {total_removals}")
current_package = 1
current_percentage = 0
for repository in packages:
    for pkg in repository:
        # Cancel all running builds for the package
        for build in pkg.getBuilds():
            if build.buildstate in ["Currently building", "Needs building"]:
                # Only cancel the build if we can
                if build.can_be_cancelled:
                    build.cancel()
        # Delete the source package
        pkg.requestDeletion(removal_comment="superseded")
        new_percentage = int(current_package / total_removals)
        if new_percentage > current_percentage:
            current_percentage = new_percentage
            print_log(f"{new_percentage}% complete ({current_package}/{total_removals})")
        current_package += 1