#!/bin/bash
#
# Package:      Language configuration
# Summary:      Agent for changing language selector in DefaultFont of running
#               fvwm2
# Author:       Olaf Dabrunz <od@suse.de> 2004
# See:          fvwm(1), FvwmCommand(1)
#
# $Id: ag_fvwm2_setlang 32340 2006-08-11 09:10:06Z jsuchome $
#
# Sets the fvwm2 font according to language and area code.
#
#    /* change "lang=" parameter in "xft:"-Font selector for fvwm2 */
#    SCR::Write(.FvwmCommand.lang, lang-area);
#

# do we need to discard irritating but expected error-messages?
#exec 2>/dev/null

. /etc/YaST2/XVersion

FVWMRC=$Xsharedir/fvwm/fvwmrc.yast2

# we stay alive and keep reading commands from YaST
while true ; do
    IFS=
    read COMMAND || exit
    unset IFS
    # strip leading backquote introduced by NI -- FIXME : do I need this?
    COMMAND=${COMMAND#\`}

    case "$COMMAND" in
        "result ("*)
            exit
            ;;

        'Dir (.)')
            echo '[ "lang" ]'
            ;;

        "Read (.lang)")
            exit
            ;;

        "Write (.lang,"*)
            # look for FvwmCommandS control socket
            shopt -s nullglob
            SOCKET=`echo /var/tmp/FvwmCommand*C /tmp/FvwmCommand*C`
            shopt -u nullglob

            # if we actually have an fvwm2 running, call FvwmCommand
            # (we flag success to the caller anyway, though)
            if [ -n "$SOCKET" ] ; then
                LANGUAGE=$(echo "$COMMAND" | sed 's/^Write (\.lang, *"\(.*\)")/\1/')
                LANGUAGE=${LANGUAGE/_/-}

                CMD=`grep "^[    ]*DefaultFont" $FVWMRC | sed -e "s/lang=[^:]*//" -e "s/\(xft:[^:]*\)/\1:lang=$LANGUAGE/"`
                $Xbindir/FvwmCommand "$CMD"
            fi
            echo "true"
            ;;

        *)
            echo nil
    esac
done

# EOF
