#!/bin/sh

### BEGIN INIT INFO
# Provides:          antiX-L10n
# Required-Start:    udev
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     S 
# Default-Stop:
# Short-Description: antiX-L10n
# Description: Sets timezone, hostname, and console fonts.  Runs update-menus and update-locale
### END INIT INFO

case "$1" in
    start) 
         ;;
    stop)
        exit 0
         ;;
    *)
         echo "Usage: $0 {start|stop}"
         exit 1
         ;;
esac

# For debugging
CMDLINE=${CMDLINE:-$(cat /proc/cmdline)}

for param in $CMDLINE; do
    case "$param" in
            lang=*)    CMD_LANG=${param#*=}; MUST_RUN=true ;;
        hostname=*)    CMD_HOST=${param#*=}; MUST_RUN=true ;;
              tz=*)      CMD_TZ=${param#*=}; MUST_RUN=true ;;
         confont=*)    CMD_FONT=${param#*=}; MUST_RUN=true ;;
          conkbd=*)    CMD_KEYS=${param#*=}; MUST_RUN=true ;;
    esac
done

[ "$MUST_RUN" ] || exit 0

# FIXME: check to see if any of these are needed
umask 022
# Ignore these signals: INT, TERM, SEGV
trap "" 2 3 11

. /usr/share/antiX/lib/antiX-init-utils.sh

start_init_logging
load_translation


# Start block of code that is sent to the log file
{
    echo_script "$_Localizing_console_and_timezone_" $0

    #-jbb: for debugging
    unset COUNTRY WM_LANG KEYBOARDS KEYTABLE LANG MIRROR TZ XKBLAYOUT CONSOLE_FONT

    # Read in: COUNTRY WM_LANG KEYBOARDS KEYTABLE LANG MIRROR TZ XKBLAYOUT CONSOLE_FONT
    [ "$CMD_LANG" ] && get_init_lang $CMD_LANG "error"

    # CMD_TZ overrides value from LANG file above
    # Only set timezone via explicit boot parameter after the first time.
    TZ=${CMD_TZ:-$TZ}
    if [ "$TZ" ]; then
 
        # Only set timezone via explicit boot parameter after the first time.
        if first_write /etc/localtime || [ "$CMD_TZ" ]; then

            # Check for valid tz string
            if [ -e /usr/share/zoneinfo/$TZ ]; then
                echo_live "$_Setting_timezone_and_localtime_to_X_" "$(pquote $TZ)"
                cp -f /usr/share/zoneinfo/$TZ /etc/localtime
                echo $TZ > /etc/timezone
            else
                error "$_Unknown_time_zone_X_" "$(pquote $TZ)"
            fi
        fi
    fi

    if [ "$CMD_HOST" ]; then
        echo_live "$_Setting_hostname_to_X_" "$(pquote $CMD_HOST)"
        echo "$CMD_HOST" > /etc/hostname
        hostname -F /etc/hostname
    fi
    
    if [ "$LANG" ]; then
        echo_live "$_Setting_locale_to_X_" "$(pquote $LANG)"

        export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
        update-locale "LANG=$LANG"
        #export locale
        #sed -i '/^export LANG=/d' /etc/skel/.profile
        #echo "export LANG=$LANG" >>/etc/skel/.profile
        # update fluxbox/icewm/jwm menus for locale
        update-menus
    fi


    KEYTABLE=${CMD_KEYS:-$KEYTABLE}
    if [ -n "$KEYTABLE" ]; then
        echo_live "$_Configuring_console_for_a_X_keymap_" "$(pquote $KEYTABLE)"
        # install keymap and set console font
        rm -f /etc/console/*
        #/usr/sbin/install-keymap "$KEYTABLE"
        loadkeys -q "$KEYTABLE"
    fi
    

    CONSOLE_FONT=${CMD_FONT:-$CONSOLE_FONT}
    if [ "$CONSOLE_FONT" ]; then
        FFILE=/usr/share/consolefonts/$CONSOLE_FONT
        if [ -e $FFILE -o -e $FFILE.gz -o -e $FFILE.psf.gz -o -e $FFILE.psfu.gz \
            -o -e $FFILE.cp.gz -o -e $FFILE.cp ]; then
            echo_live "$_Setting_console_font_to_X_" $(pquote $CONSOLE_FONT)
            setfont $CONSOLE_FONT
        else
            error "$_Unknown_console_font_X_" confont "$(pquote $CONSOLE_FONT)"
        fi
    fi

} 2>&1 | tee -a $INIT_LOG_FILE 

exit 0
