#!/bin/sh
#
# Copyright (C) 2012-2020, 2025  Etersoft
# Copyright (C) 2012-2020, 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/>.
#

# Install packages from Arch User Repository
# Args: package names
__epm_install_from_aur()
{
    # Try AUR helpers in order of popularity
    if is_command yay ; then
        docmd yay -S "$@"
        return
    fi

    if is_command paru ; then
        docmd paru -S "$@"
        return
    fi

    if is_command pikaur ; then
        docmd pikaur -S "$@"
        return
    fi

    if is_command trizen ; then
        docmd trizen -S "$@"
        return
    fi

    # Fallback to manual makepkg
    info "No AUR helper found (yay, paru, pikaur, trizen), using makepkg..."
    __epm_install_from_aur_makepkg "$@"
}

# Non interactive install packages from Arch User Repository
# Args: package names
__epm_ni_install_from_aur()
{
    # Try AUR helpers in order of popularity
    if is_command yay ; then
        docmd yay --noconfirm -S "$@"
        return
    fi

    if is_command paru ; then
        docmd paru --noconfirm -S "$@"
        return
    fi

    if is_command pikaur ; then
        docmd pikaur --noconfirm -S "$@"
        return
    fi

    if is_command trizen ; then
        docmd trizen --noconfirm -S "$@"
        return
    fi

    # Fallback to manual makepkg
    info "No AUR helper found (yay, paru, pikaur, trizen), using makepkg..."
    __epm_install_from_aur_makepkg "$@"
}

__epm_install_from_aur_makepkg()
{
    local pkg

    assure_exists git
    assure_exists makepkg base-devel

    for pkg in "$@" ; do
        local tmpdir
        tmpdir="$(mktemp -d)" || fatal "Could not create temporary directory"

        # Clone the AUR package
        info "Cloning $pkg from AUR..."
        if docmd git clone "https://aur.archlinux.org/$pkg.git" "$tmpdir/$pkg" ; then
            # Build package
            if ( cd "$tmpdir/$pkg" && docmd makepkg -s --noconfirm ) ; then
                # Install built package
                docmd epm install "$tmpdir/$pkg"/*.pkg.tar.*
            else
                warning "Failed to build $pkg from AUR"
            fi
        else
            warning "Failed to clone $pkg from AUR"
        fi

        rm -rf "$tmpdir"
    done
}
