#!/bin/sh
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  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/>.
#

load_helper epm-sh-altlinux

__epm_repo_pkgadd_alt()
{
    local archlist="i586 x86_64 aarch64 noarch"

    local REPO_DIR="$1"
    shift
    [ -d "$REPO_DIR" ] || fatal 'Can'\''t find repo dir $REPO_DIR.'

    # default name
    REPO_NAME="addon"
    # detect if already exists
    for arch in $archlist ; do
        local rd="$(echo $REPO_DIR/$arch/RPMS.*)"
        [ -d "$rd" ] && REPO_NAME="$(echo "$rd" | sed -e 's|.*\.||')" && break
    done

    [ -n "$1" ] || fatal "Missed package name"

    while [ -s "$1" ] ; do
        arch="$(epm print arch from filename "$1")" || fatal
        # arch hack (it is better to repack firstly)
        [ "$arch" = "i686" ] && arch="i586"
        [ "$arch" = "i386" ] && arch="i586"
        [ -d $REPO_DIR/$arch/RPMS.$REPO_NAME ] || fatal
        epm checkpkg "$1" || fatal
        cp -v "$1" $REPO_DIR/$arch/RPMS.$REPO_NAME || fatal
        shift
    done

}


__epm_repo_pkgdel_alt()
{
    local archlist="i586 x86_64 aarch64 noarch"

    local REPO_DIR="$1"
    shift
    [ -d "$REPO_DIR" ] || fatal 'Can'\''t find repo dir $REPO_DIR.'

    [ -n "$1" ] || fatal "Missed package name"

    # default name
    REPO_NAME="addon"
    # detect if already exists
    for arch in $archlist ; do
        local rd="$(echo $REPO_DIR/$arch/RPMS.*)"
        [ -d "$rd" ] && REPO_NAME="$(echo "$rd" | sed -e 's|.*\.||')" && break
    done

    while [ -s "$1" ] ; do
        for arch in $archlist ; do
            local rd="$REPO_DIR/$arch/RPMS.$REPO_NAME"
            [ -d $REPO_DIR/$arch/RPMS.$REPO_NAME ] || continue
            for i in $rd/$1* ; do
                [ "$1" = "$(epm print name for package $i)" ] || continue
                rm -v $rd/$1*
            done
        done
        shift
    done

}


__epm_repo_pkgupdate_alt()
{
    local dir="$1"
    shift
    for i in "$@" ; do
        pkg="$(epm print name for package $i)" || fatal
        __epm_repo_pkgdel_alt "$dir" $pkg
    done
    __epm_repo_pkgadd_alt "$dir" "$@"
}



epm_repo_pkgadd()
{

case $PMTYPE in
    apt-rpm)
        __epm_repo_pkgadd_alt "$@"
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE'
        ;;
esac

}


epm_repo_pkgupdate()
{

case $PMTYPE in
    apt-rpm)
        __epm_repo_pkgupdate_alt "$@"
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE'
        ;;
esac

}


epm_repo_pkgdel()
{

case $PMTYPE in
    apt-rpm)
        __epm_repo_pkgdel_alt "$@"
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE'
        ;;
esac

}

# call with packages to put to $put_to_repo
epm_put_to_repo()
{
    epm_repo_pkgupdate "$put_to_repo" "$@"
}