template_setup/common/functions.sh
deathbybandaid 7d3e47515d test
2024-05-01 22:05:23 -04:00

207 lines
4.4 KiB
Bash

#!/bin/bash
get_dist() {
echo $(lsb_release -si)
}
is_command() {
local check_command="$1"
command -v "${check_command}" >/dev/null 2>&1
}
git_update() {
local git_name="$1"
local git_path="$2"
local git_repo="$3"
if [ ! -d "$git_path" ]; then
printf "$COL_YELLOW" "* Installing $git_name"
git clone --quiet $git_repo $git_path
else
printf "$COL_YELLOW" "* Updating $git_name"
git -C $git_path pull
fi
}
should_apt_last_update() {
LAST_UPDATED=$( stat --format="%X" /var/cache/apt/pkgcache.bin )
UNIX_TIME=$( date +%s )
TIME_DIFF=$(( UNIX_TIME - LAST_UPDATED ))
if [[ "${TIME_DIFF}" -gt 43200 ]]
then
# 0 = true
return 0
else
# 1 = false
return 1
fi
}
get_distro() {
# echo $(awk -F= '/^NAME/{print $2}' /etc/os-release)
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
echo $OS
}
get_pkg_mgr() {
if is_command apt ; then
echo "apt"
elif is_command apt-get ; then
echo "apt-get"
elif is_command rpm ; then
# Then check if dnf or yum is the package manager
if is_command dnf ; then
echo "dnf"
else
echo "yum"
fi
fi
}
pkgmgr_update(){
# Get Command language
PKG_MANAGER=$( get_pkg_mgr )
# Run apt update for distros that use apt
if [ "$PKG_MANAGER" == *"apt"* ] ; then
if should_apt_last_update ; then
"${PKG_MANAGER} update"
fi
fi
if [ "$PKG_MANAGER" == *"apt"* ] ; then
"$PKG_MANAGER" full-upgrade -y
elif [ "$PKG_MANAGER" == "dnf" ] ; then
"$PKG_MANAGER" update
elif [ "$PKG_MANAGER" == "yum" ] ; then
"$PKG_MANAGER" update
fi
}
get_system_used_space() {
echo $(df --output=used -B1 / | tail -n 1| cut -d'%' -f1)
}
human_readable_bytes() {
local spacebytes="$1"
spacekb=`expr "$spacebytes" / 1024`
spacemb=`expr "$spacebytes" / 1024 / 1024`
spacegb=`expr "$spacebytes" / 1024 / 1024 / 1024`
if [[ "$spacegb" -gt 0 ]]
then
spacewords="$spacegb GB"
elif [[ "$spacemb" -gt 0 ]]
then
spacewords="$spacemb MB"
elif [[ "$spacekb" -gt 0 ]]
then
spacewords="$spacekb KB"
elif [[ "$spacebytes" -gt 0 ]]
then
spacewords="$spacebytes Bytes"
else
spacewords="zero space"
fi
echo "$spacewords"
}
pkgmgr_clean(){
# Get Command language
PKG_MANAGER=$( get_pkg_mgr )
printf "$COL_YELLOW" "Cleaning cache for $PKG_MANAGER"
if [ "$PKG_MANAGER" == *"apt"* ] ; then
"$PKG_MANAGER" clean
"$PKG_MANAGER" autoclean
"$PKG_MANAGER" autoremove -y
elif [ "$PKG_MANAGER" == "dnf" ] ; then
"$PKG_MANAGER" clean dbcache
"$PKG_MANAGER" clean all
"$PKG_MANAGER" autoremove -y
elif [ "$PKG_MANAGER" == "yum" ] ; then
"$PKG_MANAGER" clean packages
"$PKG_MANAGER" clean headers
"$PKG_MANAGER" clean metadata
"$PKG_MANAGER" clean all
"$PKG_MANAGER" autoremove -y
fi
}
pkg_mgr_install_pkg() {
# package to install
local pkg_to_install="$1"
# Get Command language
PKG_MANAGER=$( get_pkg_mgr )
# Run apt update for distros that use apt
if [ "$PKG_MANAGER" == *"apt"* ] ; then
if should_apt_last_update ; then
"${PKG_MANAGER} update"
fi
fi
# Install the package
local PKG_INSTALL=("${PKG_MANAGER}" install -y)
"${PKG_INSTALL[@]}" "${pkg_to_install}"
}
service_exists() {
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
is_shared_lib_present() {
local libtocheck="$1"
printf "$COL_YELLOW" "Checking if Shared library is installed: $libtocheck"
if ! ldconfig -p | grep $libtocheck
then
printf "$COL_YELLOW" "$libtocheck is not installed"
return 1
else
printf "$COL_YELLOW" "$libtocheck is installed"
return 0
fi
}