profile/packages/os.sh

102 lines
1.7 KiB
Bash

#!/bin/sh -e
tag_file="$(mktemp)"
trap 'rm "${tag_file}"' EXIT
add_tag() {
for tag in "$@"; do
if ! has_tags "$tag"; then
echo "Adding detected tag: $tag"
echo "${tag}" >> "${tag_file}"
fi
done
}
has_tags() {
for req in "$@"; do
printf "Checking for tag: $req... "
local detected=0
while IFS= read -r line; do
if [ "$line" = "$req" ]; then
echo "yes"
detected=1
break
fi
done < "$tag_file"
if [ "$detected" -eq 0 ]; then
echo "no"
return 1
fi
done
return 0
}
has_binary() {
printf "Checking for binary: $1... "
if ! command -v "$1" >/dev/null 2>&1; then
echo "no"
return 1
fi
echo "yes"
return 0
}
# arch linux
if [ -f /etc/arch-release ]; then
add_tag os:arch
add_tag os_like:archlinux
fi
# os-release
if [ -f /etc/os-release ]; then
(
. /etc/os-release
add_tag "os:${ID}"
if [ ! -z "${ID_LIKE}" ]; then
add_tag "os_like:${ID_LIKE}"
fi
if [ ! -z "${VERSION_ID}" ]; then
add_tag "version:${VERSION_ID}"
fi
if [ ! -z "${VERSION_CODENAME}" ]; then
add_tag "version:${VERSION_CODENAME}"
fi
if [ ! -z "${UBUNTU_CODENAME}" ]; then
add_tag "version:${UBUNTU_CODENAME}"
fi
)
fi
# package manager detection
for pm in \
apt-get \
pacaur \
pacman \
pip \
npm \
yaourt \
yarn \
yay \
; do
if has_binary "$pm"; then
case "$pm" in
apt-get)
add_tag "pm:apt"
;;
*)
add_tag "pm:$pm"
;;
esac
fi
done
# graphics UI detection
# basically just checks if an X server is running on the machine and if yes, we just assume it's a desktop, not a server
# grep will be always returned as a process with Xorg in the command line, so assume there must be a second process for this to succeed
if [ $(ps aux | grep Xorg | wc -l) -gt 1 ]; then
add_tag "xorg_running" "desktop"
fi