From f7a67d3e822204cce9beb24c3d47762b273d81a9 Mon Sep 17 00:00:00 2001 From: Vatsal Gupta Date: Sat, 13 Jul 2024 10:57:49 +0530 Subject: [PATCH] Fixes --- install.sh | 76 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/install.sh b/install.sh index be93687..de1879c 100755 --- a/install.sh +++ b/install.sh @@ -19,7 +19,7 @@ # Global Variables & Constants ########################################################################################## readonly FILE_NAME=".aliases.sh" -readonly FILE_PATH="$HOME/$FILE_NAME" +readonly FILE_PATH="${HOME}/${FILE_NAME}" readonly FILE_LINK="https://raw.githubusercontent.com/gvatsal60/Linux-Aliases/HEAD/${FILE_NAME}" UPDATE_RC="${UPDATE_RC:-"true"}" @@ -32,27 +32,30 @@ UPDATE_RC="${UPDATE_RC:-"true"}" updaterc() { _rc="" if [ "${UPDATE_RC}" = "true" ]; then - case $ADJUSTED_ID in + case ${ADJUSTED_ID} in debian | rhel) _rc=~/.bashrc ;; alpine | arch) _rc=~/.profile ;; + darwin) + _rc=~/.zshrc + ;; *) - echo "Error: Unsupported or unrecognized Linux distribution ${ADJUSTED_ID}" + echo "Error: Unsupported or unrecognized os distribution ${ADJUSTED_ID}" exit 1 ;; esac # Check if "~.aliases.sh" is already sourced, if not then append it - ALIAS_SOURCE_BLOCK=$(printf "\n# Sourcing custom aliases\nif [ -f \"%s\" ]; then\n . \"%s\"\nfi\n" "$FILE_PATH" "$FILE_PATH") + ALIAS_SOURCE_BLOCK=$(printf "if [ -f \"%s\" ]; then\n . \"%s\"\nfi\n" "${FILE_PATH}" "${FILE_PATH}") if [ -f "${_rc}" ]; then - if ! grep -qxF "$ALIAS_SOURCE_BLOCK" "${_rc}"; then + if ! grep -qxF "${ALIAS_SOURCE_BLOCK}" "${_rc}"; then echo "Updating ${_rc} for ${ADJUSTED_ID}..." # Append the sourcing block to the RC file - printf "%s" "$ALIAS_SOURCE_BLOCK" >> "${_rc}" + printf "\n# Sourcing Custom Aliases\n%s" "${ALIAS_SOURCE_BLOCK}" >> "${_rc}" fi else # Notify if the rc file does not exist @@ -61,7 +64,7 @@ updaterc() { # Create the rc file touch ${_rc} # Append the sourcing block to the newly created rc file - printf "%s" "$ALIAS_SOURCE_BLOCK" >> "${_rc}" + printf "\n# Sourcing Custom Aliases\n%s" "${ALIAS_SOURCE_BLOCK}" >> "${_rc}" fi fi } @@ -69,12 +72,12 @@ updaterc() { # Function: dw_file # Description: Download file using wget or curl if available dw_file() { - # Check if wget is available - if command -v wget >/dev/null 2>&1; then - wget -O "${HOME}/${FILE_NAME}" ${FILE_LINK} # Check if curl is available - elif command -v curl >/dev/null 2>&1; then + if command -v curl >/dev/null 2>&1; then curl -fsSL -o "${HOME}/${FILE_NAME}" ${FILE_LINK} + # Check if wget is available + elif command -v wget >/dev/null 2>&1; then + wget -O "${HOME}/${FILE_NAME}" ${FILE_LINK} else echo "Error: Either install wget or curl" exit 1 @@ -85,36 +88,49 @@ dw_file() { # Main Script ########################################################################################## -# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME -# shellcheck source=/dev/null -. /etc/os-release +OS=$(uname) -# Get an adjusted ID independent of distro variants -if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then - ADJUSTED_ID="debian" -elif [ "${ID}" = "alpine" ]; then - ADJUSTED_ID="alpine" -elif [ "${ID}" = "arch" ] || [ "${ID_LIKE}" = "arch" ] || (echo "${ID_LIKE}" | grep -q "arch"); then - ADJUSTED_ID="arch" -elif [ "${ID}" = "rhel" ] || [ "${ID}" = "fedora" ] || [ "${ID}" = "mariner" ] || (echo "${ID_LIKE}" | grep -q "rhel") || (echo "${ID_LIKE}" | grep -q "fedora") || (echo "${ID_LIKE}" | grep -q "mariner"); then - ADJUSTED_ID="rhel" -else - echo "Error: Linux distro ${ID} not supported." +case ${OS} in +Darwin) + ADJUSTED_ID="darwin" + ;; +Linux) + # Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME + # shellcheck source=/dev/null + . /etc/os-release + + # Get an adjusted ID independent of distro variants + if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then + ADJUSTED_ID="debian" + elif [ "${ID}" = "alpine" ]; then + ADJUSTED_ID="alpine" + elif [ "${ID}" = "arch" ] || [ "${ID_LIKE}" = "arch" ] || (echo "${ID_LIKE}" | grep -q "arch"); then + ADJUSTED_ID="arch" + elif [ "${ID}" = "rhel" ] || [ "${ID}" = "fedora" ] || [ "${ID}" = "mariner" ] || (echo "${ID_LIKE}" | grep -q "rhel") || (echo "${ID_LIKE}" | grep -q "fedora") || (echo "${ID_LIKE}" | grep -q "mariner"); then + ADJUSTED_ID="rhel" + else + echo "Error: Linux distro ${ID} not supported." + exit 1 + fi + ;; +*) + echo "Error: Unsupported or unrecognized os distribution ${ADJUSTED_ID}" exit 1 -fi + ;; +esac if [ -f "${HOME}/${FILE_NAME}" ]; then - echo "File already exists: $HOME/${FILE_NAME}" + echo "File already exists: ${HOME}/${FILE_NAME}" echo "Do you want to replace it (default: y)? [y/n]: " read -r rp_conf rp_conf="${rp_conf:-y}" - if [ "$rp_conf" = "y" ]; then + if [ "${rp_conf}" = "y" ]; then # Replace the existing file - echo "Replacing $HOME/${FILE_NAME}..." + echo "Replacing ${HOME}/${FILE_NAME}..." dw_file updaterc else - echo "Keeping existing file: $HOME/${FILE_NAME}" + echo "Keeping existing file: ${HOME}/${FILE_NAME}" fi else dw_file