profile/packages/os.sh

112 lines
1.8 KiB
Bash
Raw Normal View History

2018-02-17 19:12:09 +00:00
#!/bin/sh -e
tag_file="$(mktemp)"
trap 'rm "${tag_file}"' EXIT
DEBUG="${DEBUG:-0}"
log() {
if [ "${DEBUG}" -gt 0 ]; then
printf "$@"
fi
}
2018-02-17 19:12:09 +00:00
add_tag() {
for tag in "$@"; do
if ! has_tags "$tag"; then
log "Adding detected tag: $tag\n"
2018-02-17 19:12:09 +00:00
echo "${tag}" >> "${tag_file}"
fi
done
}
has_tags() {
for req in "$@"; do
log "Checking for tag: $req... "
detected=0
2018-02-17 19:12:09 +00:00
while IFS= read -r line; do
if [ "$line" = "$req" ]; then
log "yes\n"
2018-02-17 19:12:09 +00:00
detected=1
break
fi
done < "$tag_file"
if [ "$detected" -eq 0 ]; then
log "no\n"
2018-02-17 19:12:09 +00:00
return 1
fi
done
return 0
}
has_binary() {
log "Checking for binary: $1... "
2018-02-17 19:12:09 +00:00
if ! command -v "$1" >/dev/null 2>&1; then
log "no\n"
2018-02-17 19:12:09 +00:00
return 1
fi
log "yes\n"
2018-02-17 19:12:09 +00:00
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
(
2018-02-17 19:31:58 +00:00
. /etc/os-release
2018-02-17 19:12:09 +00:00
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 \
2018-02-17 19:33:40 +00:00
apt-get \
brew \
2018-02-17 19:12:09 +00:00
pacaur \
pacman \
2018-02-19 10:01:01 +00:00
pip3 \
2019-11-22 09:22:54 +00:00
pipx \
2018-02-17 19:12:09 +00:00
npm \
yaourt \
yarn \
yay \
; do
if has_binary "$pm"; then
2018-02-17 19:33:40 +00:00
case "$pm" in
apt-get)
add_tag "pm:apt"
;;
*)
add_tag "pm:$pm"
;;
esac
2018-02-17 19:12:09 +00:00
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
2018-02-17 19:18:10 +00:00
# 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
2018-02-18 22:07:07 +00:00
if [ "$(ps aux | grep Xorg | wc -l)" -gt 1 ]; then
2018-02-17 19:12:09 +00:00
add_tag "xorg_running" "desktop"
fi