#!/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/>.
#

# Check if any argument is a valid task format: 123456, #123456, 123456/pkg, task/123456, task/123456/pkg
is_taskarg() {
    local arg
    for arg in "$@"; do
        [[ "$arg" =~ ^[[:space:]]*#?[0-9]+$ ]] && return 0
        [[ "$arg" =~ ^[0-9]+/[^/]+$ ]] && return 0
        [[ "$arg" =~ ^task/[0-9]+(/[^/]+)?$ ]] && return 0
    done
    return 1
}


is_taskurl()
{
    local pkg_urls="$1"
    [ -n "$pkg_urls" ] || return

    echo "$pkg_urls" | grep -q -E "https://packages.altlinux.org/ru/tasks/[0-9]+/*$" || \
    echo "$pkg_urls" | grep -q -E "https://git.altlinux.org/tasks/[0-9]+/*$" || \
    echo "$pkg_urls" | grep -q -E "https://git.altlinux.org/tasks/archive/done/_[0-9]+/[0-9]+/*$"
}

# Extract task number from various formats: 123456, #123456, 123456/pkg, task/123456, task/123456/pkg
get_tasknumber_from_arg()
{
    local arg="$1"
    local task=""
    # strip leading spaces and #
    arg="$(echo "$arg" | sed -e 's|^ *#*||')"
    case "$arg" in
        task/*/*)
            task=$(printf "%s" "$arg" | cut -d/ -f2)
            ;;
        task/*)
            task=$(printf "%s" "$arg" | cut -d/ -f2)
            ;;
        */*)
            task=$(printf "%s" "$arg" | cut -d/ -f1)
            ;;
        *)
            task="$arg"
            ;;
    esac
    isnumber "$task" && echo "$task"
}

# Extract package name from task arg formats: 123456/pkg, task/123456/pkg
get_pkgname_from_taskarg()
{
    local arg="$1"
    case "$arg" in
        task/*/*)
            printf "%s" "$arg" | cut -d/ -f3
            ;;
        task/[0-9]*)
            # task/123456 - no package name
            ;;
        [0-9]*/*)
            # 123456/pkg - first part is task number
            printf "%s" "$arg" | cut -d/ -f2
            ;;
    esac
}

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

get_task_state()
{
    local tn="$1"
    fetch_url "$ALTTASKURL/$tn/task/state" 2>/dev/null
}

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

    res="$(fetch_url $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

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


get_task_packages()
{
    local arg tn
    for arg in "$@" ; do
        is_taskarg "$arg" || continue
        tn=$(get_tasknumber_from_arg "$arg")
        if ! get_task_status "$tn" ; then
            local state
            state="$(get_task_state "$tn")"
            if [ -n "$state" ] ; then
                warning 'Task $tn state is $state, packages are not yet available.'
            else
                warning 'Task $tn not found.'
            fi
            continue
        fi
        get_task_packages_list "$tn"
        [ "$DISTRARCH" = "x86_64" ] || continue
        get_task_arepo_status "$tn" || continue
        get_task_arepo_packages_list "$tn"
    done
}
