diff --git a/fastfetch.sh b/fastfetch.sh index 9217fad..91533c5 100755 --- a/fastfetch.sh +++ b/fastfetch.sh @@ -6,38 +6,115 @@ RED='\033[31m' # Red GREEN='\033[32m' # Green YELLOW='\033[33m' # Yellow -# Define variables for the repository URL and the installation directory -FASTFETCH_REPO_URL="https://github.com/LinusDierheimer/fastfetch" -INSTALL_DIR="$HOME/fastfetch" - -# Check if the installation directory exists, create it if it doesn't -if [ ! -d "$INSTALL_DIR" ]; then - printf "${YELLOW}Cloning fastfetch repository into: $INSTALL_DIR${RC}\n" - if git clone "$FASTFETCH_REPO_URL" "$INSTALL_DIR"; then - printf "${GREEN}Successfully cloned fastfetch repository${RC}\n" +checkEnv() { + ## Check for requirements. + REQUIREMENTS='curl groups sudo' + for req in $REQUIREMENTS; do + if ! command_exists "$req"; then + printf "${RED}To run me, you need: $REQUIREMENTS${RC}\n" + exit 1 + fi + done + + ## Check Package Handler + PACKAGEMANAGER='apt dnf pacman' + for pgm in $PACKAGEMANAGER; do + if command_exists "$pgm"; then + PACKAGER="$pgm" + printf "Using $pgm\n" + break + fi + done + + if [ -z "$PACKAGER" ]; then + printf "${RED}Can't find a supported package manager${RC}\n" + exit 1 + fi + + if command_exists sudo; then + SUDO_CMD="sudo" + elif command_exists doas && [ -f "/etc/doas.conf" ]; then + SUDO_CMD="doas" else - printf "${RED}Failed to clone fastfetch repository${RC}\n" + SUDO_CMD="su -c" + fi + + printf "Using $SUDO_CMD as privilege escalation software\n" + + ## Check if the current directory is writable. + GITPATH=$(dirname "$(realpath "$0")") + if [ ! -w "$GITPATH" ]; then + printf "${RED}Can't write to $GITPATH${RC}\n" exit 1 fi -else - printf "${GREEN}Repository already exists at: $INSTALL_DIR${RC}\n" -fi -# Navigate to the installation directory -cd "$INSTALL_DIR" + ## Check SuperUser Group + SUPERUSERGROUP='wheel sudo root' + for sug in $SUPERUSERGROUP; do + if groups | grep -q "$sug"; then + SUGROUP="$sug" + printf "Super user group $SUGROUP\n" + break + fi + done -# Pull the latest changes from the repository -git pull + ## Check if member of the sudo group. + if ! groups | grep -q "$SUGROUP"; then + printf "${RED}You need to be a member of the sudo group to run me!${RC}\n" + exit 1 + fi +} + +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} + printf "%b\n" "${CYAN}Using ${tool} for privilege escalation${RC}" + ESCALATION_TOOL_CHECKED=true + return 0 + fi + done + + printf "%b\n" "${RED}Can't find a supported escalation tool${RC}" + exit 1 + fi +} -# Create a build directory and navigate into it -mkdir -p build -cd build -# Run cmake and make to compile fastfetch -cmake .. -make -j"$(nproc)" +installFastfetch() { + if ! command_exists fastfetch; then + printf "%b\n" "${YELLOW}Installing Fastfetch...${RC}" + case "$PACKAGER" in + pacman) + "$ESCALATION_TOOL" "$PACKAGER" -S --needed --noconfirm fastfetch + ;; + apt-get|nala) + curl -sSLo /tmp/fastfetch.deb https://github.com/fastfetch-cli/fastfetch/releases/latest/download/fastfetch-linux-amd64.deb + "$ESCALATION_TOOL" "$PACKAGER" install -y /tmp/fastfetch.deb + rm /tmp/fastfetch.deb + ;; + *) + "$ESCALATION_TOOL" "$PACKAGER" install -y fastfetch + ;; + esac + else + printf "%b\n" "${GREEN}Fastfetch is already installed.${RC}" + fi +} -# Install fastfetch -sudo make install +setupFastfetchConfig() { + printf "%b\n" "${YELLOW}Copying Fastfetch config files...${RC}" + if [ -d "${HOME}/.config/fastfetch" ] && [ ! -d "${HOME}/.config/fastfetch-bak" ]; then + cp -r "${HOME}/.config/fastfetch" "${HOME}/.config/fastfetch-bak" + fi + mkdir -p "${HOME}/.config/fastfetch/" + curl -sSLo "${HOME}/.config/fastfetch/config.jsonc" https://raw.githubusercontent.com/ChrisTitusTech/mybash/main/config.jsonc +} -printf "${GREEN}fastfetch has been successfully installed${RC}\n" +checkEnv +checkEscalationTool +installFastfetch +setupFastfetchConfig \ No newline at end of file