Files
lnx-arch/scripts/99_software.sh
tuxwarrior 68552e4ad9 u
2024-05-01 17:45:56 -05:00

169 lines
4.1 KiB
Bash

#!/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
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
# -----
sudo pacman -Syu
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
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"
elif [[ $installed != *$x* ]]
then
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
echo "^^^^^^^^^^^^^^^^^^^^^^^^^ ${list_aur}"
#sudo -u ${user} bash -c 'yay -S --needed --noconfirm --timeupdate ${list_aur}'
sudo -u ${user} bash -c 'yay'
#runuser -l ${user} -c 'yay -S --needed --noconfirm --timeupdate ${list_aur}'
#su ${user}
#yay -S --needed --noconfirm --timeupdate ${list_aur}
#yay -S --needed --noconfirm --timeupdate ${list_aur}
sudo -u ${user} bash -c 'echo "--------- __ ----------" ${list_aur}'
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