#!/bin/sh

PATH=/bin:/usr/bin
SYSFONTDIR=/etc/sysconfig/console/consolefonts
SYSTRANSDIR=/etc/sysconfig/console/consoletrans
I18NFILE=/etc/sysconfig/i18n

_fpath()
{
    if [ -f $1/$2.$3.gz ]; then
	echo -n "$1/$2.$3.gz"
    elif [ -f $1/$2.$3 ]; then
	echo -n "$1/$2.$3"
    else
	echo "$2"
    fi
}

#
# This function finds the encoding of the current locale (it can
# be overwritten by a "CHARSET" variable if needed); the call
# to "locale charmap" should return the proper value; in case it
# fails, a guess from the locale name is done.
# this function is also defined in /etc/rc.d/init.d/functions
# if you change it here change it there too, to have the same behaviour
#
_get_locale_encoding() {
    CHARSET=${CHARSET=`locale charmap 2> /dev/null`}
    case "$CHARSET" in 
	ANSI_X3.4-1968)
	    # this means that the locale has not been
	    # correctly initialized; we will look at the
	    # locale naming and use the known
	    # default values for various languages
	    case "${LC_ALL}${LC_CTYPE}${LANG}" in 
		# the locale tells it is in UTF-8, or it is a language
		# we know must be in UTF-8 by default
		*.utf8*|*.UTF-8*| \
		am*|an*|ar*|as*|az*|bn*|cy*|eo*|fa*|gn*|gu*|he*|hi*|hy*| \
		id*|ka*|kn*|ku*|ky*|lo*|m*|ne*|om*|pa*|ph*|se*|sr*|st*| \
		ta*|te*|tg*|ti*|tk*|tl*|tt*|ug*|ur*|uz*|vi*|xh*|y*)
		    CHARSET="UTF-8" ;;
	    esac
	    ;;
    esac
    echo "$CHARSET"
}


case $1 in
    '') : # Don't do anything, just proceed.
        ;;
    *)

        DOCDIR=$(ls -d /usr/share/doc/initscripts-*)
        cat << EOF

Error: $0 doesn't take any arguments. It is a part of the system
initscripts. It reads $I18NFILE to set the font in the
console.

Read $DOCDIR/sysconfig.txt for detailed documentation of
$I18NFILE

EOF
        exit 1 ;;
esac

# read the system-wide i18n and consoel settings
if [ -f $I18NFILE ]; then
    . $I18NFILE
fi

# if CHARSET is not defined, try to see what the current
# locale encoding is (we only want to know if it is UTF-8 or not)
if [ -z "$CHARSET" ]; then CHARSET="`_get_locale_encoding`" ; fi

# define full paths for font and other files
if [ -n "$SYSFONT" ]; then SYSFONT="$(_fpath $SYSFONTDIR $SYSFONT psf)" ; fi 
if [ -n "$UNIMAP" ]; then UNIMAP="$(_fpath $SYSTRANSDIR $UNIMAP sfm)" ; fi 
if [ -n "$SYSFONTACM" ]; then
       	SYSFONTACM="$(_fpath $SYSTRANSDIR $SYSFONTACM acm)" ; fi 


if [ -x /bin/consolechars -o -x /usr/bin/consolechars ]; then
  case "$CHARSET" in 
  UTF-8)
    if [ -x /bin/unicode_start ] && /sbin/consoletype fg ; then
        exec unicode_start $SYSFONT $SYSFONTACM
    fi
    ;;
  *)	 
    if [ -n "$SYSFONT" ]; then
    	    ARGS="-f $SYSFONT"
        if [ -n "$UNIMAP" ]; then
	    ARGS="$ARGS --sfm $UNIMAP"
        fi
        if [ -n "$SYSFONTACM" ]; then
	    ARGS="$ARGS --acm $SYSFONTACM"
        fi
        consolechars $ARGS
    fi
    ;;
  esac    
fi

exit 0
