allow the profile to self install

This commit is contained in:
fatalerrors
2026-05-07 11:54:06 +02:00
parent 9a089112c3
commit f5244ac062

View File

@@ -35,10 +35,123 @@
# * OF SUCH DAMAGE. # * OF SUCH DAMAGE.
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
_profile_is_sourced()
{
[[ "${BASH_SOURCE[0]}" != "$0" ]]
}
_profile_finish()
{
local rc="${1:-0}"
if _profile_is_sourced; then
return "$rc"
fi
exit "$rc"
}
_profile_install_in_file()
{
local rc_file="$1"
local source_line="$2"
[[ -f "$rc_file" ]] || touch "$rc_file" || {
printf "[ Error ] Cannot create %s\n" "$rc_file" >&2
return 1
}
if grep -Fqx "$source_line" "$rc_file"; then
printf "[ Info ] Already configured in %s\n" "$rc_file"
return 0
fi
printf "\n%s\n" "$source_line" >> "$rc_file" || {
printf "[ Error ] Cannot write to %s\n" "$rc_file" >&2
return 1
}
printf "[ Info ] Added profile source line to %s\n" "$rc_file"
return 0
}
_profile_install()
{
local install_bashrc=0
local install_profile=0
local target_selected=0
local script_dir source_line rc=0
while [[ $# -gt 0 ]]; do
case "$1" in
--bashrc)
install_bashrc=1
target_selected=1
;;
--profile)
install_profile=1
target_selected=1
;;
-h|--help)
printf "Usage: %s --install [--bashrc] [--profile]\n" "${BASH_SOURCE[0]}"
printf "If no target is specified, both ~/.bashrc and ~/.profile are configured.\n"
return 0
;;
*)
printf "[ Error ] Unknown install option: %s\n" "$1" >&2
return 2
;;
esac
shift
done
if (( target_selected == 0 )); then
install_bashrc=1
install_profile=1
fi
script_dir=$(dirname "$(realpath -s "${BASH_SOURCE[0]}")")
source_line="source \"$script_dir/profile.sh\""
if (( install_bashrc == 1 )); then
_profile_install_in_file "$HOME/.bashrc" "$source_line" || rc=$?
fi
if (( install_profile == 1 )); then
_profile_install_in_file "$HOME/.profile" "$source_line" || rc=$?
fi
return "$rc"
}
if [[ $# -gt 0 ]]; then
case "$1" in
--install)
shift
_profile_install "$@"
_profile_finish $?
;;
-h|--help)
printf "Usage: source %s\n" "${BASH_SOURCE[0]}"
printf " %s --install [--bashrc] [--profile]\n" "${BASH_SOURCE[0]}"
_profile_finish 0
;;
*)
printf "[ Error ] Unknown option: %s\n" "$1" >&2
_profile_finish 2
;;
esac
fi
if ! _profile_is_sourced; then
printf "[ Warning ] profile.sh is designed to be sourced, not executed directly.\n" >&2
printf "Use: source \"%s\"\n" "$(realpath -s "${BASH_SOURCE[0]}")" >&2
printf "Or run: %s --install [--bashrc] [--profile]\n" "${BASH_SOURCE[0]}" >&2
exit 1
fi
if [[ ! $SHELL =~ bash ]]; then if [[ ! $SHELL =~ bash ]]; then
echo "That environment script is designed to be used with bash being the shell." echo "That environment script is designed to be used with bash being the shell."
echo "Please consider using bash to enjoy our features!" echo "Please consider using bash to enjoy our features!"
return 1 _profile_finish 1
fi fi
# Required for associative arrays (4.0+) and namerefs (4.3+) # Required for associative arrays (4.0+) and namerefs (4.3+)
@@ -48,6 +161,8 @@ if ((BASH_VERSINFO[0] < 4)) || [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1
(return 0 2>/dev/null) && return 1 || exit 1 (return 0 2>/dev/null) && return 1 || exit 1
fi fi
unset -f _profile_is_sourced _profile_finish _profile_install_in_file _profile_install
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# path* : private functions for PATH variable management # path* : private functions for PATH variable management
pathremove() pathremove()
@@ -236,6 +351,10 @@ fi
# Parse and load general configuration # Parse and load general configuration
export PROFILE_CONF="$MYPATH/profile.conf" export PROFILE_CONF="$MYPATH/profile.conf"
parse_conf "$PROFILE_CONF" parse_conf "$PROFILE_CONF"
# Overload with user configuration if it exists
if [[ -f "$HOME/.profile.conf" ]]; then
parse_conf "$HOME/.profile.conf"
fi
load_conf system # Load Bash system behavior configuration (history, pager, etc.) load_conf system # Load Bash system behavior configuration (history, pager, etc.)
load_conf general # General purpose configuration (compilation flags, etc.) load_conf general # General purpose configuration (compilation flags, etc.)