Files
lnx-arch/scripts/99_software.sh

245 lines
5.7 KiB
Bash
Raw Normal View History

2024-04-30 07:40:24 -05:00
#!/usr/bin/env bash
# klevstul
# https://wiki.archlinux.org/title/pacman
echo "<< 99_software.sh >>"
2024-05-01 16:53:48 -05:00
user=poq
2024-05-01 18:07:41 -05:00
if [ "$EUID" -ne 0 ] ; then
echo "error: run as 'root'"; exit 1;
2024-04-30 07:40:24 -05:00
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
2024-04-30 21:58:03 -05:00
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2024-04-30 07:40:24 -05:00
param_check() {
_parameter=$1
_error_msg=$2
2024-04-30 07:40:24 -05:00
if [ -z "${p_arameter}" ] ; then
echo "error: ${_error_msg}" ; exit 1
2024-04-30 07:40:24 -05:00
fi
}
2024-04-30 22:02:09 -05:00
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2024-04-30 07:40:24 -05:00
# -----
# always update the system
# -----
2024-06-08 15:07:40 -05:00
echo "- - - - -"
echo "if no uninstallable packages were found, the following error will be displayed:"
echo "\"error: no targets specified (use -h for help)\""
echo "- - - - -"
2024-06-08 15:10:22 -05:00
echo "removing obsolete packages:"
2024-05-06 07:25:05 -05:00
pacman -Rns $(pacman -Qtdq) # remove packages that are not needed
2024-06-08 15:10:22 -05:00
echo "update installed packages:"
pacman -Syu # update installed packages
2024-04-30 07:40:24 -05:00
echo "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
2024-06-08 14:50:43 -05:00
do_update() {
hostname=$1
2024-04-30 07:40:24 -05:00
2024-06-08 14:50:43 -05:00
param_check "${hostname}" "no host name given"
2024-04-30 07:40:24 -05:00
2024-06-08 15:10:22 -05:00
echo
2024-08-17 15:08:47 -05:00
echo "-----"
echo "INSTALLING PACKAGES FOR HOST '${hostname}':"
echo "-----"
2024-06-08 15:10:22 -05:00
2024-06-08 14:50:43 -05:00
package_list_url=https://git.mz.fo/fro/lnx-arch/raw/branch/master/dots/archinstall/${hostname}/packages.txt
2024-04-30 07:40:24 -05:00
trg_file=/tmp/packages.txt
2024-05-08 07:41:45 -05:00
rm -rf ${trg_file}
rm -rf ${trg_file}.tmp
2024-05-31 13:05:30 -05:00
wget --no-check-certificate -q ${package_list_url} -O ${trg_file}
2024-04-30 07:40:24 -05:00
if ! [ -s ${trg_file} ] ; then
echo "error: packages.txt is empty"
echo ""
echo "attempted to to download 'packages.txt' from:
echo '${package_list_url}'"
2024-06-08 14:50:43 -05:00
echo "'${hostname}' might be an invalid hostname, or 'packages.txt' might be missing for given host."
2024-04-30 07:40:24 -05:00
exit 1
fi
2024-05-01 15:56:09 -05:00
# https://unix.stackexchange.com/questions/157328/how-can-i-remove-all-comments-from-a-file
2024-05-01 15:50:01 -05:00
sed '/^[[:blank:]]*#/d;s/#.*//' ${trg_file} > ${trg_file}.tmp
2024-05-01 15:56:09 -05:00
# https://stackoverflow.com/questions/30988586/creating-an-array-from-a-text-file-in-bash
2024-05-01 15:50:01 -05:00
mapfile -t packages < ${trg_file}.tmp
2024-04-30 07:40:24 -05:00
installed=$(pacman -Qq)
list=()
2024-05-01 15:56:09 -05:00
list_aur=()
2024-04-30 07:40:24 -05:00
for x in ${packages[@]}
do
2024-05-08 07:49:16 -05:00
if [[ $installed == $x ]]
2024-04-30 07:40:24 -05:00
then
2024-08-17 15:01:27 -05:00
:
#echo "Skipping (1) $x"
else
2024-05-01 15:50:01 -05:00
if [[ $(echo "$installed" | grep -E -o "^$x$") != $x ]] then
2024-05-01 15:56:09 -05:00
if [[ "$x" =~ ^aur:* ]]; then
2024-05-01 16:00:36 -05:00
x=${x:4}
2024-08-17 15:01:27 -05:00
# echo "Adding $x to AUR list"
2024-05-04 13:11:29 -05:00
list_aur+="$x "
2024-05-01 15:56:09 -05:00
else
2024-05-01 16:00:36 -05:00
echo "Adding $x to Arch list"
2024-05-01 15:56:09 -05:00
list+="$x "
fi
2024-08-17 15:01:27 -05:00
#else
# echo "Skipping (2) $x"
2024-04-30 07:40:24 -05:00
fi
fi
done
2024-08-17 15:08:47 -05:00
echo "ARCH PACKAGES:"
2024-04-30 07:40:24 -05:00
if [[ -n "$list" ]]
then
2024-05-01 16:04:05 -05:00
pacman -S --needed --noconfirm $list
2024-04-30 07:40:24 -05:00
else
2024-08-17 15:08:47 -05:00
echo "none to install"
2024-05-01 16:03:18 -05:00
fi
2024-08-17 15:08:47 -05:00
echo "AUR PACKAGES:"
2024-05-01 16:03:18 -05:00
if [[ -n "$list_aur" ]]
then
2024-05-01 18:46:58 -05:00
# 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
2024-05-01 18:44:16 -05:00
echo ${list_aur} > /tmp/list_aur.txt
2024-05-04 13:10:54 -05:00
sudo -u ${user} bash -c 'yay -S --needed --noconfirm --timeupdate $(</tmp/list_aur.txt)'
2024-05-01 16:03:18 -05:00
else
2024-08-17 15:08:47 -05:00
echo "none to install"
2024-04-30 07:40:24 -05:00
fi
2024-06-08 14:50:43 -05:00
}
2024-06-08 15:15:36 -05:00
do_services() {
hostname=$1
2024-06-08 14:50:43 -05:00
2024-06-08 15:15:36 -05:00
param_check "${hostname}" "no host name given"
2024-05-19 17:00:13 -05:00
2024-06-08 15:15:36 -05:00
echo
echo "installing packages for the host '${hostname}':"
2024-05-19 17:00:13 -05:00
2024-06-08 15:15:36 -05:00
service_list_url=https://git.mz.fo/fro/lnx-arch/raw/branch/master/dots/archinstall/${hostname}/services.txt
2024-05-19 17:00:13 -05:00
trg_file=/tmp/services.txt
rm -rf ${trg_file}
2024-05-31 13:05:30 -05:00
wget --no-check-certificate -q ${service_list_url} -O ${trg_file}
2024-05-19 17:00:13 -05:00
if ! [ -s ${trg_file} ] ; then
echo "error: services.txt is empty"
echo ""
echo "attempted to to download 'services.txt' from:
echo '${service_list_url}'"
2024-06-08 15:15:36 -05:00
echo "'${hostname}' might be an invalid hostname, or 'services.txt' might be missing for given host."
2024-05-19 17:00:13 -05:00
exit 1
fi
mapfile -t services < ${trg_file}
list=()
for x in ${services[@]}
do
echo "systemctl enable ${x}"
2024-05-19 17:03:47 -05:00
systemctl enable ${x}
2024-05-19 17:00:13 -05:00
done
2024-06-08 15:15:36 -05:00
}
# -----
# update all packages for given host
# -----
if [ ${operation} == "update" ] ; then
2024-08-17 15:13:07 -05:00
echo "::::: :::::"
echo ${parameter}
do_update "common"
echo "::: :::"
echo ${parameter}
do_update ${parameter}
echo ": :"
2024-08-17 15:10:25 -05:00
echo ${parameter}
2024-06-08 15:15:36 -05:00
fi
# -----
# enable services
# -----
if [ ${operation} == "services" ] ; then
2024-08-13 21:17:02 -05:00
do_services "common"
do_services ${parameter}
2024-06-08 15:15:36 -05:00
2024-05-19 17:00:13 -05:00
fi
2024-04-30 07:40:24 -05:00
# -----
# 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 "- - - - -"
2024-06-08 15:07:40 -05:00
echo "if no removable orphans were found, the following error will be displayed:"
2024-04-30 07:40:24 -05:00
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