diff --git a/bash_profile b/bash_profile index ca41948..6d280c3 100644 --- a/bash_profile +++ b/bash_profile @@ -1,12 +1,47 @@ #!/bin/bash -if [ -f "$HOME"/.nix-profile/etc/profile.d/hm-session-vars.sh ]; then - source "$HOME"/.nix-profile/etc/profile.d/hm-session-vars.sh +if [ -f "${HOME}"/.nix-profile/etc/profile.d/hm-session-vars.sh ]; then + source "${HOME}"/.nix-profile/etc/profile.d/hm-session-vars.sh fi -if [ -f "$HOME"/.bash/profile ]; then source "$HOME"/.bash/profile; fi -if [ -d "$HOME"/.bash/profile ]; then for file in "$HOME"/.bash/profile/*; do source "$file"; done; fi -if [ -d "$HOME"/.bash/profile.d ]; then for file in "$HOME"/.bash/profile.d/*; do source "$file"; done; fi +# Define the path to the directory of this file if not already set +if [ -z "${BASH_CONFIG_DIR}" ]; then + # Define a function to get the real path of a script + # (works for most cases : either sourced script or executed script, posix shells) + get_script_dir() { + local target + # Si appelé depuis un script (Bash), on regarde qui a appelé la fonction (${BASH_SOURCE[1]}) + # Si la fonction est exécutée directement dans le terminal, on se rabat sur ${BASH_SOURCE[0]} ou ${0} + if [ -n "${BASH_SOURCE}" ]; then + if [ "${#BASH_SOURCE[@]}" -gt 1 ]; then + target="${BASH_SOURCE[1]}" + else + target="${BASH_SOURCE[0]}" + fi + elif [ -n "${ZSH_VERSION}" ]; then + target="${(%):-%x}" + else + target="${0}" + fi + + # Résolution des liens symboliques POSIX + while [ -L "${target}" ]; do + local link + link=$(readlink "${target}") + case "${link}" in + /*) target="${link}" ;; + *) target="$(dirname "${target}")/${link}" ;; + esac + done + + (cd "$(dirname "${target}")" && pwd -P) + } + BASH_CONFIG_DIR=$(get_script_dir) +fi + +if [ -f "${BASH_CONFIG_DIR}"/profile ]; then source "${BASH_CONFIG_DIR}"/profile; fi +if [ -d "${BASH_CONFIG_DIR}"/profile ]; then for file in "${BASH_CONFIG_DIR}"/profile/*; do source "$file"; done; fi +if [ -d "${BASH_CONFIG_DIR}"/profile.d ]; then for file in "${BASH_CONFIG_DIR}"/profile.d/*; do source "$file"; done; fi # This file is sourced by bash for login shells. The following line # runs your .bashrc and is recommended by the bash info pages. diff --git a/bashrc b/bashrc index e90da1e..6fb2991 100644 --- a/bashrc +++ b/bashrc @@ -13,50 +13,79 @@ fi # DEBUG_BASHRC=true -# Determine path to directory of this file (FIX: remove dependency to readlink for posix compliance) -BASEDIR=$( - source_file=$(readlink -f "${BASH_SOURCE[0]}") - source_dir=$(dirname "${source_file}") - cd "${source_dir}" && pwd -) + +# Define the path to the directory of this file if not already set +if [ -z "${BASH_CONFIG_DIR}" ]; then + # Define a function to get the real path of a script + # (works for most cases : either sourced script or executed script, posix shells) + get_script_dir() { + local target + # Si appelé depuis un script (Bash), on regarde qui a appelé la fonction (${BASH_SOURCE[1]}) + # Si la fonction est exécutée directement dans le terminal, on se rabat sur ${BASH_SOURCE[0]} ou ${0} + if [ -n "${BASH_SOURCE}" ]; then + if [ "${#BASH_SOURCE[@]}" -gt 1 ]; then + target="${BASH_SOURCE[1]}" + else + target="${BASH_SOURCE[0]}" + fi + elif [ -n "${ZSH_VERSION}" ]; then + target="${(%):-%x}" + else + target="${0}" + fi + + # Résolution des liens symboliques POSIX + while [ -L "${target}" ]; do + local link + link=$(readlink "${target}") + case "${link}" in + /*) target="${link}" ;; + *) target="$(dirname "${target}")/${link}" ;; + esac + done + + (cd "$(dirname "${target}")" && pwd -P) + } + BASH_CONFIG_DIR=$(get_script_dir) +fi # Source some helpers functions -source "${BASEDIR}/_helpers.bash" +source "${BASH_CONFIG_DIR}/_helpers.bash" # Source custom libs -_source_dir_files "${BASEDIR}"/libs +_source_dir_files "${BASH_CONFIG_DIR}"/libs # Source 3rd party libs if they exists -_source_file_if_exists "${BASEDIR}/3rd-party/complete-alias/complete_alias" -_source_file_if_exists "${BASEDIR}/3rd-party/junegunn/fzf-git.sh/fzf-git.sh" +_source_file_if_exists "${BASH_CONFIG_DIR}/3rd-party/complete-alias/complete_alias" +_source_file_if_exists "${BASH_CONFIG_DIR}/3rd-party/junegunn/fzf-git.sh/fzf-git.sh" # Early customization -_source_dir_files "${BASEDIR}"/rc.before.d +_source_dir_files "${BASH_CONFIG_DIR}"/rc.before.d # Source rc.d/* -_source_dir_files "${BASEDIR}"/rc -_source_dir_files "${BASEDIR}"/rc.d +_source_dir_files "${BASH_CONFIG_DIR}"/rc +_source_dir_files "${BASH_CONFIG_DIR}"/rc.d # Source functions definitions _source_file_if_exists ~/.bash_functions -_source_file_if_exists "${BASEDIR}"/functions -_source_dir_files "${BASEDIR}"/functions -_source_dir_files "${BASEDIR}"/functions.d +_source_file_if_exists "${BASH_CONFIG_DIR}"/functions +_source_dir_files "${BASH_CONFIG_DIR}"/functions +_source_dir_files "${BASH_CONFIG_DIR}"/functions.d # Source alias definitions _source_file_if_exists ~/.bash_aliases -_source_file_if_exists "${BASEDIR}"/aliases -_source_dir_files "${BASEDIR}"/aliases -_source_dir_files "${BASEDIR}"/aliases.d +_source_file_if_exists "${BASH_CONFIG_DIR}"/aliases +_source_dir_files "${BASH_CONFIG_DIR}"/aliases +_source_dir_files "${BASH_CONFIG_DIR}"/aliases.d # Source bash completion definitions # TODO: Améliorer cette partie pour éviter les erreurs quand aucun fichier n'existe for file in /etc/bash*completion /etc/profile.d/bash*completion*; do source "$file"; done _source_file_if_exists ~/.bash_completion -_source_file_if_exists "${BASEDIR}"/completion -_source_dir_files "${BASEDIR}"/completion -_source_dir_files "${BASEDIR}"/completion.d +_source_file_if_exists "${BASH_CONFIG_DIR}"/completion +_source_dir_files "${BASH_CONFIG_DIR}"/completion +_source_dir_files "${BASH_CONFIG_DIR}"/completion.d _source_dir_files ~/.nix-profile/share/bash-completion/completions if (command -v _complete_alias &>/dev/null); then @@ -64,4 +93,4 @@ if (command -v _complete_alias &>/dev/null); then fi # Late customization -_source_dir_files "${BASEDIR}"/rc.after.d +_source_dir_files "${BASH_CONFIG_DIR}"/rc.after.d diff --git a/create-links.bash b/create-links.bash index 705f923..092be28 100755 --- a/create-links.bash +++ b/create-links.bash @@ -1,11 +1,43 @@ #!/bin/bash -cd $HOME || exit 1 +# Define the path to the directory of this file if not already set +if [ -z "${BASH_CONFIG_DIR}" ]; then + # Define a function to get the real path of a script + # (works for most cases : either sourced script or executed script, posix shells) + get_script_dir() { + local target + # Si appelé depuis un script (Bash), on regarde qui a appelé la fonction (${BASH_SOURCE[1]}) + # Si la fonction est exécutée directement dans le terminal, on se rabat sur ${BASH_SOURCE[0]} ou ${0} + if [ -n "${BASH_SOURCE}" ]; then + if [ "${#BASH_SOURCE[@]}" -gt 1 ]; then + target="${BASH_SOURCE[1]}" + else + target="${BASH_SOURCE[0]}" + fi + elif [ -n "${ZSH_VERSION}" ]; then + target="${(%):-%x}" + else + target="${0}" + fi -CONFDIR=.bash + # Résolution des liens symboliques POSIX + while [ -L "${target}" ]; do + local link + link=$(readlink "${target}") + case "${link}" in + /*) target="${link}" ;; + *) target="$(dirname "${target}")/${link}" ;; + esac + done -ln -sf $CONFDIR/bash_profile .bash_profile -ln -sf $CONFDIR/bashrc .bashrc -ln -sf $CONFDIR/bash_logout .bash_logout + (cd "$(dirname "${target}")" && pwd -P) + } + BASH_CONFIG_DIR=$(get_script_dir) +fi -ln -sf $CONFDIR/inputrc .inputrc +cd "${HOME}" || exit 1 + +ln -sf "${BASH_CONFIG_DIR}"/bash_profile .bash_profile +ln -sf "${BASH_CONFIG_DIR}"/bashrc .bashrc +ln -sf "${BASH_CONFIG_DIR}"/bash_logout .bash_logout +ln -sf "${BASH_CONFIG_DIR}"/inputrc .inputrc