#!/bin/bash

# SPDX-FileCopyrightText: 2021 Luca Beltrame <lbeltrame@kde.org>
#
# SPDX-License-Identifier: BSD-3-Clause

function rebuild_broken() {
	local packages=$1
	local repo=$2
	echo  "Checking broken packages in ${repo}"
	for pkg in $packages
	do
		echo $pkg
		osc token --trigger $(cat $HOME/.auth_token) -o runservice $repo $pkg
	done
}

function check_broken() {
	local repo=$1
	local target=$2
	osc pr "${repo}" -q -V -a x86_64 -s B -r "${target}" --csv | cut -d\; -f1 | sed '1d'
}

status_frameworks=$(check_broken "KDE:Unstable:Frameworks" openSUSE_Factory)
status_applications=$(check_broken "KDE:Unstable:Applications" KDE_Unstable_Frameworks_openSUSE_Factory)
status_extra=$(check_broken "KDE:Unstable:Extra" KDE_Unstable_Frameworks_openSUSE_Factory)

if [ ! -z "$status_frameworks" ];
then
	rebuild_broken "$status_frameworks" KDE:Unstable:Frameworks
fi

if [ ! -z "$status_applications" ];
then
	rebuild_broken "$status_applications" KDE:Unstable:Applications
fi

if [ ! -z "$status_extra" ];
then
	rebuild_broken "$status_extra" KDE:Unstable:Extra
fi