#!/bin/zsh error() { echo "Your package manager is not supported." echo "$COMMAND could not be installed automatically." echo "Please install $COMMAND manually before running this script." exit 1 } install() { COMMAND=$1 PACKAGE=$2 if ! command -v $COMMAND &> /dev/null then if [[ "$OSTYPE" == "linux-gnu"* ]]; then echo "$COMMAND not found. Installing $PACKAGE..." DISTRO=$(cat /etc/os-release | grep -oP '(?<=^ID=).+' | tr -d '"') if [[ $DISTRO == "arch" ]]; then yes | sudo pacman -S $PACKAGE yes | sudo pacman -S $COMMAND exit 0 elif [[ $DISTRO == "archarm" ]]; then yes | sudo pacman -S $PACKAGE yes | sudo pacman -S $COMMAND exit 0 elif [[ $DISTRO == "ubuntu" ]]; then yes | sudo apt-get install $PACKAGE yes | sudo apt-get install $COMMAND exit 0 elif [[ $DISTRO == "fedora" ]]; then yes | sudo dnf install $PACKAGE yes | sudo dnf install $COMMAND exit 0 elif [[ $DISTRO == "centos" ]]; then yes | sudo yum install $PACKAGE yes | sudo yum install $COMMAND exit 0 elif [[ $DISTRO == "debian" ]]; then yes | sudo apt install $PACKAGE yes | sudo apt install $COMMAND exit 0 else error fi elif [[ "$OSTYPE" == "darwin"* ]]; then echo "$COMMAND not found. Installing $PACKAGE..." if command -v "brew" &> /dev/null then yes | brew install $PACKAGE yes | brew install $COMMAND elif command -v "port" &> /dev/null then yes | sudo port install $PACKAGE yes | sudo port install $COMMAND else echo "Homebrew or MacPorts not found. Please install one of them before running this script." exit 1 fi else error fi else echo "Package $PACKAGE is already installed." fi } # If the number of arguments is not 2, print the usage if [[ $# -ne 2 ]]; then echo "Usage: check-dependency " echo " check-dependency <--git> " echo "Checks if a command is installed and if not, installs the package." echo "If --git is specified, it will check if the git repository is executable." echo "Supported package managers: apt, pacman, dnf, yum, brew, port" exit 1 fi COMMAND=$1 PACKAGE=$2 # If the command is git, we will clone the repository and install it if [[ "$COMMAND" == "--git" ]]; then USER=$(echo $PACKAGE | cut -d'/' -f1) REPO=$(echo $PACKAGE | cut -d'/' -f2) # If the repository is not installed, we will clone it and install it if ! command -v $REPO &> /dev/null then THIS_USER=$SUDO_USER if [ ! -d /home/$THIS_USER/build ]; then echo "Creating build directory for user $THIS_USER..." su $THIS_USER -c "mkdir /home/$THIS_USER/build" fi cd /home/$THIS_USER/build git clone https://github.com/$USER/$REPO cd $REPO inst exit 0 else echo "Repository $PACKAGE is already installed." exit 0 fi else install $COMMAND $PACKAGE fi