#!/bin/bash
#
# configure-blocaled - Generate config files for blocaled.
#
# Written 2022 by Bob Funk, Winnipeg, Canada
#
# This script generates a /etc/locale.conf file that can be read by blocaled, 
# for use in a Slackware system.
#
# Blocaled expects to find variable assignments for things like language, 
# keyboard, X keyboard, console fonts, etc. The following lists what variables
# we set for blocaled, and where that translates to in Slackware.
#
# LANG="" and other LC_* variables - From /etc/profile.d/lang.sh
# 
# KEYMAP="" - Using 'loadkeys' manually, or from /etc/rc.d/rc.keymap
# FONT="" - Using 'setfont' from 'setconsolefont' script, or /etc/rc.d/rc.font
#
# xkeyboard settings in Xorg style syntax - /etc/X11/xorg.conf.d/9[0-9]-.*
# This one can be assigned in the blocaled.conf and used directly.

# The conf file we will write (using the blocaled default): 
LOCALE_CONF="/etc/locale.conf"

# First parse Slackware's config files and generate an /etc/locale.conf file:
echo "Generating $LOCALE_CONF"
(
printf "# ${LOCALE_CONF}\n
# This file was automatically generated by $0.\n\n"
(
# Set a font, if one is specified in rc.font, otherwise leave blank:
FONT=""
if [ -x "/etc/rc.d/rc.font" ]; then
  FONT="$(grep -v -E '^(#|$)' /etc/rc.d/rc.font | rev | cut -d' ' -f1 | rev | cut -d. -f 1)"
fi
echo FONT=$FONT

# Set a keymap, if one is specified in rc.keymap, otherwise leave blank:
KEYMAP=""
if [ -x "/etc/rc.d/rc.keymap" ]; then
  KEYMAP="$(grep '^[^#].*loadkeys .*\.map' /etc/rc.d/rc.keymap | rev | cut -d. -f 2 | cut -d' ' -f 1 | rev)"
fi
echo KEYMAP=$KEYMAP

# Echo all the L.* variables that are set in /etc/profile.d/lang.sh, otherwise use the following default:
LANG=en_US.UTF-8
if [ -x "/etc/profile.d/lang.sh" ]; then
  if ( grep -q "^[^#].*LANG=.*" /etc/profile.d/lang.sh); then
    grep '^[^#].*L.*=' /etc/profile.d/lang.sh | sed 's/.*export[[:blank:]]*//g'
  else
    echo LANG=$LANG
    grep '^[^#].*L.*=' /etc/profile.d/lang.sh | sed 's/.*export[[:blank:]]*//g'
  fi
else
  echo LANG=$LANG
fi
)
) > $LOCALE_CONF

