Files
lnx-arch/scripts/99_software.sh
2025-07-02 19:29:16 -05:00

269 lines
6.4 KiB
Bash
Executable File

#!/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
# -----
echo "- - - - -"
echo "if no uninstallable packages were found, the following error will be displayed:"
echo "\"error: no targets specified (use -h for help)\""
echo "- - - - -"
echo "removing obsolete packages:"
pacman -Rns $(pacman -Qtdq) # remove packages that are not needed
echo "update installed packages:"
pacman -Syu # update installed packages
echo "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
do_update() {
hostname=$1
param_check "${hostname}" "no host name given"
echo
echo "-----"
echo "INSTALLING PACKAGES FOR HOST '${hostname}':"
echo "-----"
package_list_url=https://gt.op.fo/fro/lnx-arch/raw/branch/master/dots/archinstall/${hostname}/packages.txt
trg_file=/tmp/packages.txt
rm -rf ${trg_file}
rm -rf ${trg_file}.tmp
wget --no-check-certificate -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 "'${hostname}' might be an invalid hostname, or 'packages.txt' might be missing 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
echo "ARCH PACKAGES:"
if [[ -n "$list" ]]
then
pacman -S --needed --noconfirm $list
else
echo " -> none to install"
fi
echo "AUR PACKAGES:"
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 " -> none to install"
fi
}
do_services() {
hostname=$1
param_check "${hostname}" "no host name given"
echo
echo "installing packages for the host '${hostname}':"
service_list_url=https://gt.op.fo/fro/lnx-arch/raw/branch/master/dots/archinstall/${hostname}/services.txt
trg_file=/tmp/services.txt
rm -rf ${trg_file}
wget --no-check-certificate -q ${service_list_url} -O ${trg_file}
if ! [ -s ${trg_file} ] ; then
echo "error: services.txt is empty"
echo ""
echo "attempted to to download 'services.txt' from:
echo '${service_list_url}'"
echo "'${hostname}' might be an invalid hostname, or 'services.txt' might be missing for given host."
exit 1
fi
# remove comments
sed '/^[[:blank:]]*#/d;s/#.*//' ${trg_file} > ${trg_file}.tmp
mapfile -t services < ${trg_file}.tmp
for x in ${services[@]}; do
echo "systemctl enable ${x}"
systemctl enable ${x}
done
}
# -----
# update all packages for given host
# -----
if [ ${operation} == "update" ] ; then
do_update "common"
do_update ${parameter}
fi
# -----
# enable services
# -----
if [ ${operation} == "services" ] ; then
do_services "common"
do_services ${parameter}
fi
# -----
# install package
# -----
if [ ${operation} == "install" ] ; then
param_check "${parameter}" "no package name given"
if [[ "$parameter" =~ ^aur:* ]]; then
parameter=${parameter:4}
echo "Install AUR package '${parameter}'"
echo ${parameter} > /tmp/list_aur.txt
sudo -u ${user} bash -c 'yay -S --needed --noconfirm --timeupdate $(</tmp/list_aur.txt)'
else
echo "Install Arch package '${parameter}'"
pacman -S ${parameter}
fi
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 removable orphans 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"
if [[ "$parameter" =~ ^aur:* ]]; then
parameter=${parameter:4}
fi
# uninstall program and remove orphans
pacman -Rns ${parameter}
fi
# -----
# update mirrors
# -----
if [ ${operation} == "mirrors" ] ; then
echo "Updating mirrors..."
rate-mirrors --allow-root --protocol https arch | grep -v '^#' | sudo tee /etc/pacman.d/mirrorlist
fi
# -----
# clear cache (cc)
# -----
if [ ${operation} == "cc" ] ; then
echo "Clearing cache..."
runuser -l ${user} yay -Scc
fi