You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
2.1 KiB

#!/bin/sh
# ensure-locale.sh
# Ensures UTF-8 locale is generated and persisted system-wide.
# Safe, idempotent. Requires root (or sudo) for system-wide changes.
# Usage:
# sudo ./ensure-locale.sh
# ./ensure-locale.sh cmd args # entrypoint wrapper
_locale_log() { printf '[ensure-locale] %s\n' "$*" >&2; }
_utf8_active() {
_eff="${LC_ALL:-${LC_CTYPE:-$LANG}}"
case "$_eff" in
*[Uu][Tt][Ff]8* | *[Uu][Tt][Ff]-8*) return 0 ;;
esac
return 1
}
_locale_exists() {
locale -a 2>/dev/null | grep -qi "$1"
}
ensure_locale_generated() {
if _locale_exists '^en_US\.utf'; then
_locale_log "en_US.UTF-8 already generated."
return 0
fi
if ! command -v locale-gen >/dev/null 2>&1; then
_locale_log "locale-gen not found; skipping generation."
return 1
fi
if [ "$(id -u)" != "0" ]; then
_locale_log "Not root; cannot generate locale. Run with sudo."
return 1
fi
_locale_log "Generating en_US.UTF-8..."
[ -w /etc/locale.gen ] &&
sed -i 's/^# *\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 2>/dev/null || true
locale-gen en_US.UTF-8 >/dev/null 2>&1
update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 2>/dev/null || true
_locale_log "Generated en_US.UTF-8."
}
ensure_zshenv() {
_zshenv=/etc/zsh/zshenv
_marker="en_US.UTF-8"
if [ "$(id -u)" != "0" ]; then
_locale_log "Not root; cannot write $_zshenv. Run with sudo."
return 1
fi
if [ -f "$_zshenv" ] && grep -q "$_marker" "$_zshenv" 2>/dev/null; then
_locale_log "$_zshenv already contains UTF-8 locale settings."
return 0
fi
_locale_log "Writing locale exports to $_zshenv..."
mkdir -p "$(dirname $_zshenv)"
cat >>"$_zshenv" <<'EOF'
# UTF-8 locale — added by ensure-locale.sh
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
EOF
_locale_log "Done. Open a new shell or run: exec zsh"
}
ensure_locale_generated
ensure_zshenv
# Export for the current session too
if _locale_exists '^en_US\.utf' && ! _utf8_active; then
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
fi
# Entrypoint wrapper: ./ensure-locale.sh python app.py
if [ $# -gt 0 ]; then
exec "$@"
fi