Files
lnx-arch/scripts/99_software.sh
tuxwarrior 51f9bbf694 u
2024-05-01 14:28:24 -05:00

147 lines
3.2 KiB
Bash

#!/usr/bin/env bash
# klevstul
# https://wiki.archlinux.org/title/pacman
echo "<< 99_software.sh >>"
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://stackoverflow.com/questions/30988586/creating-an-array-from-a-text-file-in-bash
mapfile -t packages < grep -o '^[^#]*' ${trg_file}
installed=$(pacman -Qq)
list=()
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
#grep -o '^[^#]*' ${x} > x
echo "Adding $x to list"
list+="$x "
else
echo "Skipping (2) $x"
fi
fi
done
if [[ -n "$list" ]]
then
echo "--------------->"
echo $list
#pacman -S --needed --noconfirm $list
else
echo "Nothing 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