#!/usr/bin/env bash

# klevstul

# https://wiki.archlinux.org/title/pacman

echo "<< 99_software.sh >>"

user=poq

if [ "$EUID" -ne 0 ] ; then
    echo "error: run as 'root'"; exit 1;
fi

this_file_name=`basename "$0"`
if [ $# -lt 1 ]; then
    echo "error: $# arguments given"
    echo "usage: '$this_file_name [operation] [argument?]'"
    exit 1
fi

operation=$1
parameter=$2

# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

param_check() {
    parameter=$1
    error_msg=$2

    if [ -z "${parameter}" ] ; then
        echo "error: ${error_msg}" ; exit 1
    fi
}

# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

# -----
# always update the system
# -----
pacman -Rns $(pacman -Qtdq)     # remove packages that are not needed
pacman -Syu                     # update install packages

echo "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"

# -----
# update all packages for given host
# -----
if [ ${operation} == "update" ] ; then

    param_check "${parameter}" "no host name given"

    package_list_url=https://git.mz.fo/fro/lnx-arch/raw/branch/master/dots/archinstall/${parameter}/packages.txt

    trg_file=/tmp/packages.txt

    rm -rf ${trg_file}
    rm -rf ${trg_file}.tmp

    wget -q ${package_list_url} -O ${trg_file}

    if ! [ -s ${trg_file} ] ; then
        echo "error: packages.txt is empty"
        echo ""
        echo "attempted to to download 'packages.txt' from:
        echo '${package_list_url}'"
        echo "'${parameter}' might be an invalid hostname, or 'packages.txt' might be created for given host."
        exit 1
    fi

    # https://unix.stackexchange.com/questions/157328/how-can-i-remove-all-comments-from-a-file
    sed '/^[[:blank:]]*#/d;s/#.*//' ${trg_file} > ${trg_file}.tmp

    # https://stackoverflow.com/questions/30988586/creating-an-array-from-a-text-file-in-bash
    mapfile -t packages < ${trg_file}.tmp

    installed=$(pacman -Qq)
    list=()
    list_aur=()
    for x in ${packages[@]}
        do
            if [[ $installed == $x ]]
                then
                    echo "Skipping (1) $x"
            else
                    if [[ $(echo "$installed" | grep -E -o "^$x$") != $x ]] then
                        if [[ "$x" =~ ^aur:* ]]; then
                            x=${x:4}
                            echo "Adding $x to AUR list"
                            list_aur+="$x "
                        else
                            echo "Adding $x to Arch list"
                            list+="$x "
                        fi
                    else
                        echo "Skipping (2) $x"
                    fi
            fi
    done

    if [[ -n "$list" ]]
        then
            pacman -S --needed --noconfirm $list
        else
            echo "No Arch packages to install"
    fi

    if [[ -n "$list_aur" ]]
        then
            # using sudo with the -c flag, it does not work to use variables, like `${list_aur}``
            # hence the list of aur packages is written to a file, which is used with yay
            echo ${list_aur} > /tmp/list_aur.txt
            sudo -u ${user} bash -c 'yay -S --needed --noconfirm --timeupdate $(</tmp/list_aur.txt)'
        else
            echo "No AUR packages to install"
    fi

fi

# -----
# install package
# -----
if [ ${operation} == "install" ] ; then

    param_check "${parameter}" "no package name given"

    pacman -S ${parameter}

fi

# -----
# search for package
# -----
if [ ${operation} == "search" ] ; then

    param_check "${parameter}" "no package name given"

    # search in name and description
    pacman -Ss "^${parameter}-"

fi

# -----
# remove orphaned packages (packages which are no longer needed)
# -----
if [ ${operation} == "orphans" ] ; then
    echo "- - - - -"
    echo "if no orphans to be removed were found, the following error will be displayed:"
    echo "\"error: argument '-' specified with empty stdin\""
    echo "- - - - -"
    echo "removing orphans..."
    pacman -Qtdq | pacman -Rns -
fi

# -----
# remove orphaned packages (packages which are no longer needed)
# -----
if [ ${operation} == "uninstall" ] ; then

    param_check "${parameter}" "no package name given"

    # uninstall program and remove orphans
    pacman -Rns ${parameter}

fi
