linutil/src/commands/common-script.sh

130 lines
3.2 KiB
Bash
Raw Normal View History

2024-07-15 20:00:04 +01:00
#!/bin/sh -e
# shellcheck disable=SC2034
2024-07-14 03:16:02 +01:00
RC='\033[0m'
RED='\033[31m'
YELLOW='\033[33m'
GREEN='\033[32m'
2024-07-13 22:44:09 +01:00
command_exists() {
which "$1" >/dev/null 2>&1
2024-07-13 22:44:09 +01:00
}
checkAURHelper() {
## Check & Install AUR helper
if [ "$PACKAGER" = "pacman" ]; then
AUR_HELPERS="yay paru"
for helper in ${AUR_HELPERS}; do
if command_exists "${helper}"; then
AUR_HELPER=${helper}
echo "Using ${helper} as AUR helper"
return 0
fi
done
echo "Installing yay as AUR helper..."
$ESCALATION_TOOL "$PACKAGER" -S --needed --noconfirm base-devel
cd /opt && $ESCALATION_TOOL git clone https://aur.archlinux.org/yay-git.git && $ESCALATION_TOOL chown -R "$USER":"$USER" ./yay-git
cd yay-git && makepkg --noconfirm -si
if command_exists yay; then
AUR_HELPER="yay"
else
echo -e "${RED}Failed to install AUR helper.${RC}"
exit 1
fi
fi
}
2024-08-23 14:12:47 +01:00
checkEscalationTool() {
## Check for escalation tools.
if [ -z "$ESCALATION_TOOL_CHECKED" ]; then
ESCALATION_TOOLS='sudo doas'
for tool in ${ESCALATION_TOOLS}; do
if command_exists "${tool}"; then
ESCALATION_TOOL=${tool}
echo "Using ${tool} for privilege escalation"
ESCALATION_TOOL_CHECKED=true
return 0
fi
done
echo -e "${RED}Can't find a supported escalation tool${RC}"
exit 1
fi
}
checkCommandRequirements() {
2024-07-13 22:44:09 +01:00
## Check for requirements.
REQUIREMENTS=$1
2024-07-14 03:16:02 +01:00
for req in ${REQUIREMENTS}; do
if ! command_exists "${req}"; then
echo "${RED}To run me, you need: ${REQUIREMENTS}${RC}"
2024-07-14 03:16:02 +01:00
exit 1
fi
done
}
2024-07-13 22:44:09 +01:00
checkPackageManager() {
## Check Package Manager
PACKAGEMANAGER=$1
2024-07-13 22:44:09 +01:00
for pgm in ${PACKAGEMANAGER}; do
if command_exists "${pgm}"; then
2024-07-13 22:44:09 +01:00
PACKAGER=${pgm}
2024-07-14 03:16:02 +01:00
echo "Using ${pgm}"
break
2024-07-13 22:44:09 +01:00
fi
done
if [ -z "${PACKAGER}" ]; then
echo -e "${RED}Can't find a supported package manager${RC}"
2024-07-13 22:44:09 +01:00
exit 1
fi
}
2024-07-13 22:44:09 +01:00
checkSuperUser() {
2024-07-13 22:44:09 +01:00
## Check SuperUser Group
SUPERUSERGROUP='wheel sudo root'
for sug in ${SUPERUSERGROUP}; do
if groups | grep -q "${sug}"; then
2024-07-13 22:44:09 +01:00
SUGROUP=${sug}
2024-07-14 03:16:02 +01:00
echo "Super user group ${SUGROUP}"
break
2024-07-13 22:44:09 +01:00
fi
done
## Check if member of the sudo group.
if ! groups | grep -q "${SUGROUP}"; then
echo "${RED}You need to be a member of the sudo group to run me!${RC}"
2024-07-13 22:44:09 +01:00
exit 1
fi
}
2024-07-13 22:44:09 +01:00
checkCurrentDirectoryWritable() {
## Check if the current directory is writable.
GITPATH="$(dirname "$(realpath "$0")")"
if [ ! -w "$GITPATH" ]; then
echo "${RED}Can't write to $GITPATH${RC}"
exit 1
fi
}
checkDistro() {
2024-07-13 22:44:09 +01:00
DTYPE="unknown" # Default to unknown
2024-07-14 03:16:02 +01:00
# Use /etc/os-release for modern distro identification
if [ -f /etc/os-release ]; then
. /etc/os-release
DTYPE=$ID
fi
}
checkEnv() {
checkCommandRequirements 'curl groups sudo'
2024-07-25 23:01:11 +01:00
checkPackageManager 'apt-get nala dnf pacman zypper yum xbps-install nix-env'
checkCurrentDirectoryWritable
checkSuperUser
checkDistro
2024-08-23 14:12:47 +01:00
checkEscalationTool
}