#!/bin/sh
#
# Copyright (C) 2014, 2017, 2021, 2025  Etersoft
# Copyright (C) 2014, 2017, 2021, 2025  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

tasknumber()
{
    local num="$(echo "$1" | sed -e "s| *#*||g")"
    isnumber "$num" && echo "$*"
}

ALTTASKURL="http://git.altlinux.org/tasks"

get_task_status()
{
    local tn="$1"
    docmd eget --check-url $ALTTASKURL/$tn/plan/add-bin
}

get_task_arepo_status()
{
    local tn="$1"
    docmd eget --check-url $ALTTASKURL/$tn/plan/arepo-add-x86_64-i586
}

get_task_packages_list()
{
    local tn="$1"
    local res

    showcmd "eget -q -O- $ALTTASKURL/$tn/plan/add-bin"
    res="$(eget -q -O- $ALTTASKURL/$tn/plan/add-bin)" || return
    echo "$res" | cut -f1-3 | grep -E "(noarch|$DISTRARCH)$" | cut -f1
}

get_task_arepo_packages_list()
{
    local tn="$1"
    local res

    showcmd "eget -q -O- $ALTTASKURL/$tn/plan/arepo-add-x86_64-i586"
    res="$(eget -q -O- $ALTTASKURL/$tn/plan/arepo-add-x86_64-i586)" || return
    echo "$res" | cut -f1
}


get_task_packages()
{
    local tn
    for tn in $(tasknumber "$@") ; do
        get_task_status "$tn" || continue
        get_task_packages_list "$tn"
        [ "$DISTRARCH" = "x86_64" ] || continue
        get_task_arepo_status "$tn" || continue
        get_task_arepo_packages_list "$tn"
    done
}