From f616dae941fd2afb2904627911e01d35dc03a6e3 Mon Sep 17 00:00:00 2001 From: Dhruv Mistry <68422844+dhruvmistry2000@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:19:47 +0530 Subject: [PATCH] Update setup.sh --- setup.sh | 510 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 258 insertions(+), 252 deletions(-) diff --git a/setup.sh b/setup.sh index 46b458e..b1711c6 100755 --- a/setup.sh +++ b/setup.sh @@ -1,294 +1,300 @@ #!/bin/sh -e -# Define color codes RC='\033[0m' RED='\033[31m' YELLOW='\033[33m' GREEN='\033[32m' -setup_environment() { - # Define the repository path and URL - REPO_DIR="$HOME/mybash" - REPO_URL="https://github.com/dhruvmistry2000/mybash" +# Define the repository path and configuration file +REPO_DIR="$HOME/mybash" +REPO_URL="https://github.com/dhruvmistry2000/mybash" - # Define variables for commands and paths - PACKAGER="" - SUDO_CMD="" - SUGROUP="" - GITPATH="$REPO_DIR" - - command_exists() { - command -v "$1" >/dev/null 2>&1 - } - - is_arch_linux() { - [ -f /etc/arch-release ] - } - - check_env() { - REQUIREMENTS='curl groups sudo' - for req in $REQUIREMENTS; do - if ! command_exists "$req"; then - echo "${RED}To run this script, you need: $REQUIREMENTS${RC}" - exit 1 - fi - done - - PACKAGEMANAGER='nala apt dnf yum pacman zypper emerge xbps-install nix-env' - for pgm in $PACKAGEMANAGER; do - if command_exists "$pgm"; then - PACKAGER="$pgm" - echo "Using $pgm as package manager" - break - fi - done +# Check if the repository directory exists, create it if it doesn't +if [ ! -d "$REPO_DIR" ]; then + echo "${YELLOW}Cloning mybash repository into: $REPO_DIR${RC}" + git clone "$REPO_URL" "$REPO_DIR" + if [ $? -eq 0 ]; then + echo "${GREEN}Successfully cloned mybash repository${RC}" + else + echo "${RED}Failed to clone mybash repository${RC}" + exit 1 + fi +else + echo "${GREEN}Repository already exists at: $REPO_DIR${RC}" +fi + +# Define variables for commands and paths +PACKAGER="" +SUDO_CMD="" +SUGROUP="" +GITPATH="$REPO_DIR" + +command_exists() { + command -v "$1" >/dev/null 2>&1 +} - if [ -z "$PACKAGER" ]; then - echo "${RED}Can't find a supported package manager${RC}" +checkEnv() { + ## Check for requirements. + REQUIREMENTS='curl groups sudo' + for req in $REQUIREMENTS; do + if ! command_exists "$req"; then + echo "${RED}To run me, you need: $REQUIREMENTS${RC}" exit 1 fi - - if command_exists sudo; then - SUDO_CMD="sudo" - elif command_exists doas && [ -f "/etc/doas.conf" ]; then - SUDO_CMD="doas" - else - SUDO_CMD="su -c" + done + + ## Check Package Handler + PACKAGEMANAGER='nala apt dnf yum pacman zypper emerge xbps-install nix-env' + for pgm in $PACKAGEMANAGER; do + if command_exists "$pgm"; then + PACKAGER="$pgm" + echo "Using $pgm" + break fi + done - echo "Using $SUDO_CMD for privilege escalation" + if [ -z "$PACKAGER" ]; then + echo "${RED}Can't find a supported package manager${RC}" + exit 1 + fi - GITPATH=$(dirname "$(realpath "$0")") - if [ ! -w "$GITPATH" ]; then - echo "${RED}Can't write to $GITPATH${RC}" - exit 1 - fi + if command_exists sudo; then + SUDO_CMD="sudo" + elif command_exists doas && [ -f "/etc/doas.conf" ]; then + SUDO_CMD="doas" + else + SUDO_CMD="su -c" + fi - SUPERUSERGROUP='wheel sudo root' - for sug in $SUPERUSERGROUP; do - if groups | grep -q "$sug"; then - SUGROUP="$sug" - echo "Super user group $SUGROUP" - break - fi - done + echo "Using $SUDO_CMD as privilege escalation software" - if ! groups | grep -q "$SUGROUP"; then - echo "${RED}You need to be a member of the sudo group to run this script!${RC}" - exit 1 - fi - } + ## 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 - install_depend() { - DEPENDENCIES='bash bash-completion tar bat tree multitail fastfetch wget unzip fontconfig' - if ! command_exists nvim; then - DEPENDENCIES="${DEPENDENCIES} neovim" + ## Check SuperUser Group + SUPERUSERGROUP='wheel sudo root' + for sug in $SUPERUSERGROUP; do + if groups | grep -q "$sug"; then + SUGROUP="$sug" + echo "Super user group $SUGROUP" + break fi + done - echo "${YELLOW}Installing dependencies...${RC}" - case "$PACKAGER" in - pacman) - if ! command_exists yay && ! command_exists paru; then - echo "Installing yay as AUR helper..." - ${SUDO_CMD} ${PACKAGER} --noconfirm -S base-devel - cd /opt && ${SUDO_CMD} git clone https://aur.archlinux.org/yay-git.git && ${SUDO_CMD} chown -R "${USER}:${USER}" ./yay-git - cd yay-git && makepkg --noconfirm -si - else - echo "AUR helper already installed" - fi - if command_exists yay; then - AUR_HELPER="yay" - elif command_exists paru; then - AUR_HELPER="paru" - else - echo "No AUR helper found. Please install yay or paru." - exit 1 - fi - ${AUR_HELPER} --noconfirm -S ${DEPENDENCIES} - ;; - nala) - ${SUDO_CMD} ${PACKAGER} install -y ${DEPENDENCIES} - ;; - emerge) - ${SUDO_CMD} ${PACKAGER} -v app-shells/bash app-shells/bash-completion app-arch/tar app-editors/neovim sys-apps/bat app-text/tree app-text/multitail app-misc/fastfetch - ;; - xbps-install) - ${SUDO_CMD} ${PACKAGER} -v ${DEPENDENCIES} - ;; - nix-env) - ${SUDO_CMD} ${PACKAGER} -iA nixos.bash nixos.bash-completion nixos.gnutar nixos.neovim nixos.bat nixos.tree nixos.multitail nixos.fastfetch nixos.pkgs.starship - ;; - dnf) - ${SUDO_CMD} ${PACKAGER} install -y ${DEPENDENCIES} - ;; - zypper) - ${SUDO_CMD} zypper refresh - ${SUDO_CMD} zypper -n install ${DEPENDENCIES} - ;; - *) - ${SUDO_CMD} ${PACKAGER} install -yq ${DEPENDENCIES} - ;; - esac - - FONT_NAME="Hack" - if fc-list :family | grep -iq "$FONT_NAME"; then - echo "Font '$FONT_NAME' is installed." - else - echo "Installing font '$FONT_NAME'" - FONT_URL="https://github.com/ryanoasis/nerd-fonts/releases/download/v2.3.3/Hack.zip" - FONT_DIR="$HOME/.local/share/fonts" - wget $FONT_URL -O ${FONT_NAME}.zip - unzip ${FONT_NAME}.zip -d $FONT_NAME - mkdir -p $FONT_DIR - mv ${FONT_NAME}/*.ttf $FONT_DIR/ - fc-cache -fv - rm -rf ${FONT_NAME} ${FONT_NAME}.zip - echo "'$FONT_NAME' installed successfully." - fi - } + ## 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}" + exit 1 + fi +} - install_starship_and_fzf() { - if command_exists starship; then - echo "Starship already installed" - else - if ! curl -sS https://starship.rs/install.sh | sh; then - echo "${RED}Something went wrong during Starship install!${RC}" - exit 1 - fi - fi +installDepend() { + ## Check for dependencies. + DEPENDENCIES='bash bash-completion tar bat tree multitail fastfetch wget unzip fontconfig' + if ! command_exists nvim; then + DEPENDENCIES="${DEPENDENCIES} neovim" + fi - if ! command_exists fzf; then - git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf - ~/.fzf/install + echo "${YELLOW}Installing dependencies...${RC}" + if [ "$PACKAGER" = "pacman" ]; then + if ! command_exists yay && ! command_exists paru; then + echo "Installing yay as AUR helper..." + ${SUDO_CMD} ${PACKAGER} --noconfirm -S base-devel + cd /opt && ${SUDO_CMD} git clone https://aur.archlinux.org/yay-git.git && ${SUDO_CMD} chown -R "${USER}:${USER}" ./yay-git + cd yay-git && makepkg --noconfirm -si else - echo "Fzf already installed" + echo "AUR helper already installed" fi - } - - install_zoxide() { - if command_exists zoxide; then - echo "Zoxide already installed" + if command_exists yay; then + AUR_HELPER="yay" + elif command_exists paru; then + AUR_HELPER="paru" else - if ! curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh; then - echo "${RED}Something went wrong during Zoxide install!${RC}" - exit 1 - fi + echo "No AUR helper found. Please install yay or paru." + exit 1 fi - } + ${AUR_HELPER} --noconfirm -S ${DEPENDENCIES} + elif [ "$PACKAGER" = "nala" ]; then + ${SUDO_CMD} ${PACKAGER} install -y ${DEPENDENCIES} + elif [ "$PACKAGER" = "emerge" ]; then + ${SUDO_CMD} ${PACKAGER} -v app-shells/bash app-shells/bash-completion app-arch/tar app-editors/neovim sys-apps/bat app-text/tree app-text/multitail app-misc/fastfetch + elif [ "$PACKAGER" = "xbps-install" ]; then + ${SUDO_CMD} ${PACKAGER} -v ${DEPENDENCIES} + elif [ "$PACKAGER" = "nix-env" ]; then + ${SUDO_CMD} ${PACKAGER} -iA nixos.bash nixos.bash-completion nixos.gnutar nixos.neovim nixos.bat nixos.tree nixos.multitail nixos.fastfetch nixos.pkgs.starship + elif [[ "$PACKAGER" == "dnf" ]]; then + ${SUDO_CMD} ${PACKAGER} install -y ${DEPENDENCIES} + else + ${SUDO_CMD} ${PACKAGER} install -yq ${DEPENDENCIES} + fi - install_additional_dependencies() { - case "$PACKAGER" in - apt) - if [ ! -d "/opt/neovim" ]; then - curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim.appimage - chmod u+x nvim.appimage - ./nvim.appimage --appimage-extract - ${SUDO_CMD} mv squashfs-root /opt/neovim - ${SUDO_CMD} ln -s /opt/neovim/AppRun /usr/bin/nvim - fi - ;; - zypper) - ${SUDO_CMD} zypper refresh - ${SUDO_CMD} zypper -n install neovim - ;; - dnf) - ${SUDO_CMD} dnf check-update - ${SUDO_CMD} dnf install -y neovim - ;; - pacman) - ${SUDO_CMD} pacman -Syu - ${SUDO_CMD} pacman -S --noconfirm neovim - ;; - *) - echo "No supported package manager found. Please install Neovim manually." - exit 1 - ;; - esac - } + # Check to see if the FiraCode Nerd Font is installed (Change this to whatever font you would like) + FONT_NAME="Hack" + if fc-list :family | grep -iq "$FONT_NAME"; then + echo "Font '$FONT_NAME' is installed." + else + echo "Installing font '$FONT_NAME'" + # Change this URL to correspond with the correct font + FONT_URL="https://github.com/ryanoasis/nerd-fonts/releases/download/v2.3.3/Hack.zip" + FONT_DIR="$HOME/.local/share/fonts" + wget $FONT_URL -O ${FONT_NAME}.zip + unzip ${FONT_NAME}.zip -d $FONT_NAME + mkdir -p $FONT_DIR + mv ${FONT_NAME}/*.ttf $FONT_DIR/ + # Update the font cache + fc-cache -fv + # delete the files created from this + rm -rf ${FONT_NAME} ${FONT_NAME}.zip + echo "'$FONT_NAME' installed successfully." + fi +} - create_fastfetch_config() { - USER_HOME=$(getent passwd "${SUDO_USER:-$USER}" | cut -d: -f6) - - if [ ! -d "$USER_HOME/.config/fastfetch" ]; then - mkdir -p "$USER_HOME/.config/fastfetch" - fi +installStarshipAndFzf() { + if command_exists starship; then + echo "Starship already installed" + return + fi - if [ -e "$USER_HOME/.config/fastfetch/config.jsonc" ]; then - rm -f "$USER_HOME/.config/fastfetch/config.jsonc" - fi + if ! curl -sS https://starship.rs/install.sh | sh; then + echo "${RED}Something went wrong during starship install!${RC}" + exit 1 + fi + if command_exists fzf; then + echo "Fzf already installed" + else + git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf + ~/.fzf/install + fi +} - ln -svf "$GITPATH/config.jsonc" "$USER_HOME/.config/fastfetch/config.jsonc" || { - echo "${RED}Failed to create symbolic link for Fastfetch config${RC}" +installZoxide() { + if command_exists zoxide; then + echo "Zoxide already installed" + return + fi + + if ! curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh; then + echo "${RED}Something went wrong during zoxide install!${RC}" + exit 1 + fi +} + +install_additional_dependencies() { + # Additional dependencies installation + return + case "$PACKAGER" in + *apt) + if [ ! -d "/opt/neovim" ]; then + curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim.appimage + chmod u+x nvim.appimage + ./nvim.appimage --appimage-extract + ${SUDO_CMD} mv squashfs-root /opt/neovim + ${SUDO_CMD} ln -s /opt/neovim/AppRun /usr/bin/nvim + fi + ;; + *zypper) + ${SUDO_CMD} zypper refresh + ${SUDO_CMD} zypper -n install neovim # -y doesn't work on opensuse -n is short for -non-interactive which is equivalent to -y + ;; + *dnf) + ${SUDO_CMD} dnf check-update + ${SUDO_CMD} dnf install -y neovim + ;; + *pacman) + ${SUDO_CMD} pacman -Syu + ${SUDO_CMD} pacman -S --noconfirm neovim + ;; + *) + echo "No supported package manager found. Please install neovim manually." exit 1 - } + ;; + esac +} + +create_fastfetch_config() { + ## Get the correct user home directory. + USER_HOME=$(getent passwd "${SUDO_USER:-$USER}" | cut -d: -f6) + + if [ ! -d "$USER_HOME/.config/fastfetch" ]; then + mkdir -p "$USER_HOME/.config/fastfetch" + fi + # Check if the fastfetch config file exists + if [ -e "$USER_HOME/.config/fastfetch/config.jsonc" ]; then + rm -f "$USER_HOME/.config/fastfetch/config.jsonc" + fi + ln -svf "$GITPATH/config.jsonc" "$USER_HOME/.config/fastfetch/config.jsonc" || { + echo "${RED}Failed to create symbolic link for fastfetch config${RC}" + exit 1 } +} - link_config() { - USER_HOME=$(getent passwd "${SUDO_USER:-$USER}" | cut -d: -f6) - OLD_BASHRC="$USER_HOME/.bashrc" - - if [ -e "$OLD_BASHRC" ]; then - echo "${YELLOW}Moving old bash config file to $USER_HOME/.bashrc.bak${RC}" - if ! mv "$OLD_BASHRC" "$USER_HOME/.bashrc.bak"; then - echo "${RED}Can't move the old bash config file!${RC}" - exit 1 - fi +linkConfig() { + ## Get the correct user home directory. + USER_HOME=$(getent passwd "${SUDO_USER:-$USER}" | cut -d: -f6) + ## Check if a bashrc file is already there. + OLD_BASHRC="$USER_HOME/.bashrc" + if [ -e "$OLD_BASHRC" ]; then + echo "${YELLOW}Moving old bash config file to $USER_HOME/.bashrc.bak${RC}" + if ! mv "$OLD_BASHRC" "$USER_HOME/.bashrc.bak"; then + echo "${RED}Can't move the old bash config file!${RC}" + exit 1 fi + fi - echo "${YELLOW}Linking new bash config file...${RC}" - ln -svf "$GITPATH/.bashrc" "$USER_HOME/.bashrc" || { - echo "${RED}Failed to create symbolic link for .bashrc${RC}" - exit 1 - } - ln -svf "$GITPATH/starship.toml" "$USER_HOME/.config/starship.toml" || { - echo "${RED}Failed to create symbolic link for starship.toml${RC}" - exit 1 - } + echo "${YELLOW}Linking new bash config file...${RC}" + ln -svf "$GITPATH/.bashrc" "$USER_HOME/.bashrc" || { + echo "${RED}Failed to create symbolic link for .bashrc${RC}" + exit 1 + } + ln -svf "$GITPATH/starship.toml" "$USER_HOME/.config/starship.toml" || { + echo "${RED}Failed to create symbolic link for starship.toml${RC}" + exit 1 } +} - copy_scripts() { - SCRIPTS_SRC_DIR="$GITPATH/scripts" - SCRIPTS_DEST_DIR="$HOME/.scripts" +copyScripts() { + # Define the source and target directories + SCRIPTS_SRC_DIR="$GITPATH/scripts" + SCRIPTS_DEST_DIR="$HOME/.scripts" + + # Create the target directory if it doesn't exist + mkdir -p "$SCRIPTS_DEST_DIR" + + # Copy all files from the source directory to the target directory + for script in "$SCRIPTS_SRC_DIR"/*; do + # Check if it's a file + if [ -f "$script" ]; then + # Copy the file to the destination directory + cp "$script" "$SCRIPTS_DEST_DIR/" + # Make the file executable + chmod +x "$SCRIPTS_DEST_DIR/$(basename "$script")" + echo "Copied and set execution permission for $(basename "$script")" + fi + done +} - mkdir -p "$SCRIPTS_DEST_DIR" - for script in "$SCRIPTS_SRC_DIR"/*; do - if [ -f "$script" ]; then - cp "$script" "$SCRIPTS_DEST_DIR/" - chmod +x "$SCRIPTS_DEST_DIR/$(basename "$script")" - echo "Copied and set execution permission for $(basename "$script")" - fi - done - } +checkEnv +installDepend +installStarshipAndFzf +installZoxide +install_additional_dependencies +create_fastfetch_config +copyScripts - # Start script execution - check_env - install_depend - install_starship_and_fzf - install_zoxide - install_additional_dependencies - create_fastfetch_config - copy_scripts - - if is_arch_linux; then - if ! command_exists yay; then - echo "${YELLOW}Running yay-setup.sh${RC}" - curl -sSL https://raw.githubusercontent.com/dhruvmistry2000/mybash/main/yay_setup.sh | bash - else - echo "yay is already installed." - fi +echo "${YELLOW}yay-setup.sh${RC}" +curl -sSL https://raw.githubusercontent.com/dhruvmistry2000/mybash/main/yay_setup.sh | bash - echo "${YELLOW}Running numlock.sh${RC}" - curl -sSL https://raw.githubusercontent.com/dhruvmistry2000/mybash/main/numlock.sh | bash - else - echo "${RED}This script is intended for Arch Linux only.${RC}" - fi +echo "${YELLOW}numlock.sh${RC}" +curl -sSL https://raw.githubusercontent.com/dhruvmistry2000/mybash/main/numlock.sh | bash - if link_config; then - echo "${GREEN}Done! Restart your shell to see the changes.${RC}" - else - echo "${RED}Something went wrong!${RC}" - fi -} +if linkConfig; then + echo "${GREEN}Done! Restart your shell to see the changes.${RC}" +else + echo "${RED}Something went wrong!${RC}" +fi -setup_environment