toolbox/profile.d/toolbox.sh
Debarshi Ray f392a69a4b profile.d: Restore compatibility with Z shell
Otherwise, every zsh instance on Fedora Kinoite and Silverblue was
running into:
  /etc/profile.d/toolbox.sh:30: bad substitution

... because case modification with "${VARIANT_ID^}" is undefined in
POSIX shell [1], and doesn't work with Z shell.

Fedora Silverblue got its own PRETTY_NAME (and VARIANT and VARIANT_ID)
starting from Fedora 32 [2].  Therefore, it's better to use PRETTY_NAME
and let the downstream distributor of the host operating system decide
how it should be presented to the user, instead of coming up with a
custom string.  eg., PRETTY_NAME isn't the same as "Fedora $VARIANT" on
Fedora Silverblue.

One nice side-effect of this is that while VARIANT and VARIANT_ID are
optional fields, PRETTY_NAME has a well-defined fallback value of
'Linux' [3].  This makes this a little less specific to Fedora Kinoite
and Silverblue.

The rest of the welcome text was reformatted to prevent it from getting
too wide depending on the contents of PRETTY_NAME.

Fallout from 3641a0032f

[1] https://www.shellcheck.net/wiki/SC3059

[2] https://pagure.io/workstation-ostree-config/c/c18ef957d11862d32f362722931dbfdf1f5beb0d

[3] https://www.freedesktop.org/software/systemd/man/os-release.html

https://github.com/containers/toolbox/issues/1017
2022-11-25 18:36:31 +01:00

106 lines
3.5 KiB
Bash

# shellcheck shell=sh
# shellcheck disable=SC2153
[ "${BASH_VERSION:-}" != "" ] || [ "${ZSH_VERSION:-}" != "" ] || return 0
[ "$PS1" != "" ] || return 0
toolbox_config="$HOME/.config/toolbox"
host_welcome_stub="$toolbox_config/host-welcome-shown"
toolbox_welcome_stub="$toolbox_config/toolbox-welcome-shown"
# shellcheck disable=SC1091
# shellcheck disable=SC2046
eval $(
if [ -f /etc/os-release ]; then
. /etc/os-release
else
. /usr/lib/os-release
fi
echo ID="$ID"
echo PRETTY_NAME="\"$PRETTY_NAME\""
echo VARIANT_ID="$VARIANT_ID"
)
if [ -f /run/ostree-booted ] \
&& ! [ -f "$host_welcome_stub" ] \
&& [ "${ID}" = "fedora" ] \
&& { [ "${VARIANT_ID}" = "workstation" ] || [ "${VARIANT_ID}" = "silverblue" ] || [ "${VARIANT_ID}" = "kinoite" ]; }; then
echo ""
echo "Welcome to ${PRETTY_NAME:-Linux}."
echo ""
echo "This terminal is running on the host system. You may want to try"
echo "out the Toolbox for a directly mutable environment that allows "
echo "package installation with DNF."
echo ""
printf "For more information, see the "
# shellcheck disable=SC1003
printf '\033]8;;https://docs.fedoraproject.org/en-US/fedora-silverblue/toolbox/\033\\documentation\033]8;;\033\\'
printf ".\n"
echo ""
mkdir -p "$toolbox_config"
touch "$host_welcome_stub"
fi
if [ -f /run/.containerenv ] \
&& [ -f /run/.toolboxenv ]; then
[ "${BASH_VERSION:-}" != "" ] && PS1=$(printf "\[\033[35m\]⬢\[\033[0m\]%s" "[\u@\h \W]\\$ ")
[ "${ZSH_VERSION:-}" != "" ] && PS1=$(printf "\033[35m⬢\033[0m%s" "[%n@%m]%~%# ")
if ! [ -f "$toolbox_welcome_stub" ]; then
echo ""
echo "Welcome to the Toolbox; a container where you can install and run"
echo "all your tools."
echo ""
if [ "${ID}" = "fedora" ]; then
echo " - Use DNF in the usual manner to install command line tools."
echo " - To create a new tools container, run 'toolbox create'."
echo ""
printf "For more information, see the "
# shellcheck disable=SC1003
printf '\033]8;;https://docs.fedoraproject.org/en-US/fedora-silverblue/toolbox/\033\\documentation\033]8;;\033\\'
printf ".\n"
else
echo " - To create a new tools container, run 'toolbox create'."
fi
echo ""
mkdir -p "$toolbox_config"
touch "$toolbox_welcome_stub"
fi
if ! [ -f /etc/profile.d/vte.sh ] && [ -z "$PROMPT_COMMAND" ] && [ "${VTE_VERSION:-0}" -ge 3405 ]; then
case "$TERM" in
xterm*|vte*)
[ -n "${BASH_VERSION:-}" ] && PROMPT_COMMAND=" "
;;
esac
fi
if [ "$TERM" != "" ]; then
error_message="Error: terminfo entry not found for $TERM"
term_without_first_character="${TERM#?}"
term_just_first_character="${TERM%$term_without_first_character}"
terminfo_sub_directory="$term_just_first_character/$TERM"
if [ "$TERMINFO" = "" ]; then
! [ -e "/usr/share/terminfo/$terminfo_sub_directory" ] \
&& ! [ -e "/lib/terminfo/$terminfo_sub_directory" ] \
&& ! [ -e "$HOME/.terminfo/$terminfo_sub_directory" ] \
&& echo "$error_message" >&2
else
! [ -e "$TERMINFO/$terminfo_sub_directory" ] \
&& echo "$error_message" >&2
fi
fi
fi
unset ID
unset PRETTY_NAME
unset VARIANT_ID
unset toolbox_config
unset host_welcome_stub
unset toolbox_welcome_stub