LoMeS/assets/test/test.sh
2025-10-10 21:59:27 +02:00

108 lines
No EOL
3.2 KiB
Bash
Executable file

#!/bin/bash
source assets/shell/colors
source assets/config/deps/dependencies
### DEPENDENCY CHECK & INSTALLER
echo -e "\n ${LCY}Dependency and Privilege Check running...${CRS}\n"
### PRIVILEGES
if (( $(id -u) == 0 )); then ### AM I ROOT?
echo -e " ${GRN}Privilege check passed...${CRS}\n"
else
echo -e " ${RED}Privilege check failed... Please run script with sudo or as root!${CRS}\n"
exit 1
fi
### DEPENDENCIES
check_for_package() {
if dpkg-query -s "$1" 1>/dev/null 2>&1; then
return 0
elif apt-cache show "$1" 1>/dev/null 2>&1; then
return 1
else
return 2
fi
}
for package in $deps; do
if check_for_package "$package"; then
printf "%-20s - %s\n" "$package" "package is installed"
elif [ $? -eq 1 ]; then
printf "%-20s - %s\n" "$package" "package is not installed, it is available in package repository"
else
printf "%-20s - %s\n" "$package" "package is not installed, it is not available in package repository"
fi
done
while true; do
echo -e " ${YEL}Do you wish to install via APT?"
read -p " (Y/n) --> " install
echo ""
if [[ "$install" = "" || "$install" = "y" || "$install" = "Y" ]]; then
sudo apt update && sudo apt install -y $pkgs_apt
echo -e "\n ${GRN}Dependencies installed. Proceeding...${CRS}\n"
break
elif [[ "$install" == "n" || "$install" = "N" ]]; then
echo -e "\n ${RED}Missing dependencies... Exiting!${CRS}\n"
exit 1
else
echo -e "\n ${YEL}Invalid response... Try again...\n\n ${GRN}Y ${YEL}= (Yes, install dependencies and continue)\n ${RED}N ${YEL}= (No, don't install dependencies and exit)${CRS}\n "
fi
done
fi
# List of packages to check
packages=("git" "gcc" "python3" "vim")
# Function to check if a package is installed and install if not
check_and_install() {
local pkg="$1"
local package_manager=""
# Determine the package manager based on the system
if command -v apt-get >/dev/null 2>&1; then
package_manager="apt-get"
elif command -v yum >/dev/null 2>&1; then
package_manager="yum"
elif command -v dnf >/dev/null 2>&1; then
package_manager="dnf"
else
echo "Unsupported package manager. Cannot proceed."
exit 1
fi
# Check if package is installed
if dpkg -s "$pkg" >/dev/null 2>&1; then
echo "$pkg is already installed."
else
echo "$pkg is not installed."
read -p "Would you like to install $pkg? (Y/N): " answer
case $answer in
[Yy]*)
if [[ "$package_manager" == "apt-get" ]]; then
sudo apt-get install -y "$pkg"
elif [[ "$package_manager" == "yum" ]]; then
sudo yum install -y "$pkg"
elif [[ "$package_manager" == "dnf" ]]; then
sudo dnf install -y "$pkg"
fi
;;
*)
echo "Skipping installation of $pkg."
;;
esac
fi
}
# Loop through each package and check/install
for pkg in "${packages[@]}"; do
check_and_install "$pkg"
done