Update homedir detection & config creation code

This commit is contained in:
JeremyStar™ 2024-06-03 02:10:47 +02:00
parent 2f72de023c
commit 82a527b9c7
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -2,9 +2,19 @@
# shellcheck disable=SC2154
set -eo pipefail
cd "${HOME:?}/.bashrc.d" || (
# Define variables
export "BASHRCD_FALLBACK=exec env --ignore-environment bash --norc --noprofile"
if [ -z "${BASHRCD_SYSTEM}" ]; then
export "BASHRCD_HOMEDIR=${HOME:?}/.bashrc.d"
else
export "BASHRCD_HOMEDIR=/etc/bashrc.d"
fi
# cd into to home directory
cd "${BASHRCD_HOMEDIR}" || (
echo ":: Can't cd into ${HOME:?}/.bashrc.d"
exit 1
${BASHRCD_FALLBACK}
)
# Initialize basic stuff
@ -14,7 +24,11 @@ cd "${HOME:?}/.bashrc.d" || (
[[ -f "init.d/functions.sh" ]] && source "init.d/functions.sh"
# Create new configuration file
[[ ! -f "config.sh" ]] && $(which cat) << EOF >> config.sh
if [ ! -f "config.sh" ]; then
if [ -n "${BASHRCD_SYSTEM}" ] && [ "${UID}" != "0" ]; then
echo ":: Warning: Can't create system-wide configuration file as normal user."
else
$(which cat) << EOF >> config.sh
#!/usr/bin/env bash
CONFIG_FLOW_CONTROL=0
CONFIG_AUTOSAVE_HISTORY=1
@ -23,6 +37,8 @@ CONFIG_EDITOR=nano
CONFIG_REPLACE_PKGMGR_ORIGINAL=pacman
CONFIG_REPLACE_PKGMGR_REPLACEMENT=paru
EOF
fi
fi
# Load configuration file
source "config.sh"