#!/bin/sh

# ALT's /sbin/setsysfont.
# Agrees with RH's initscripts-7.06-1.
# Last change: by imz@altlinux.ru on 2003 Feb 7;
# 2003 Feb 10: 
# - wrap unicode_start to hide it from dependency-seekers.

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

addLoadableData() {
# An alternative to FindFile() and the corresponding readbility test could be
# a test performed by consolechars themselves, like this:
#
#   "$CONSOLECHARS" --no-act "$option" "$f"
#
# But I don't think such complexity is reasonable here.
# imz@altlinux.ru, Feb 2003.
  local option="$1" name="$2" type
  case "$option" in
  --font) type=psf ;;
  *) type="${option#--}" ;;
  esac
  if [ -n "$name" ]; then
      args[${#args[@]}]="$option"
      local found
      if found="$(FindFile "/etc/sysconfig/console/$name.$type")"; then
	  args[${#args[@]}]="$found"
      else
	  args[${#args[@]}]="$name"
      fi
  fi
}

checkTools() {
# At least one of the two tools is required to work:
# consolechars (console-tools pkg) or setfont (kbd pkg).
    if [ -z "$CONSOLECHARS" -a -z "$SETFONT" ]; then
    # If neither tool of the two is present.
	echo "${0##*/}: cannot set font because no tools found." >&2
	exit 4 # 3rd bit
    fi
}

# Read /etc/sysconfig/i18n mainly for compatibility (SYSFONT used to be there)
# but also LANG is referred further.
# Do not care about the locale from the environment
# because we work with system-wide configuration.
SourceIfNotEmpty /etc/sysconfig/i18n
# The main configuration file:
SourceIfNotEmpty /etc/sysconfig/consolefont

# Our parameters:
readonly SYSFONT UNIMAP SYSFONTACM

# Use absolute() from new functions library.
readonly \
    CONSOLECHARS=`absolute consolechars` \
    SETFONT=`absolute setfont` \
    UNICODE_START=`absolute unicode_start`


let 'RETVAL = 0'

if [ -n "$SYSFONT" -o -n "$UNIMAP" ]; then
############### Set font (+ the map for its encoding) #################
    checkTools
    if [ -n "$CONSOLECHARS" ]; then
	args=()
	addLoadableData --font "$SYSFONT"
	addLoadableData --sfm "$UNIMAP"
	"$CONSOLECHARS" "$@" "${args[@]}"
    else # if [ -n "$SETFONT" ]; then
	if [ -n "$SYSFONT" ]; then
	    "$SETFONT" "$SYSFONT" ${UNIMAP:+-u "$UNIMAP"}
	fi
    fi || let 'RETVAL |= 1'
# Font set. If errors occured, the 1st bit of RETVAL is set.
fi

############## Set ACM ################
# for newer fonts and ACMs it's value is independent of the system font:
# stage 1: application --> ACM (charmap -> Unicode) --> Unicode data -->
# stage 2: --> Unicode data --> SFM (Unicode -> font encoding) --> data 
#                                encoded corresponding to the console font.
#
# Perhaps this should be done in a separate script.
# Another enhancement would be to fall back on $(locale charmap) 
# (or its lower-case variant) -- but the utilities are in /usr/bin/;
# something like this is done only for the Unicode case now.


# We examine only LANG here. What about LC_CTYPE, LC_ALL, LANGUAGE 
# -- they can also indicate a Unicode console.
case "$LANG" in 
    *.utf8|*.UTF-8)
	# In this case, there is no ACM: applications output data
	# already in Unicode; the first stage is not needed.
	# The font and SFM has been set already; 
	# unicode_start will use the current settings
	"$UNICODE_START" || let 'RETVAL |= 2'
	exit $RETVAL
	;;
esac

if [ -n "$SYSFONTACM" ]; then 
  if [ -n "$CONSOLECHARS" ]; then
    args=()
    addLoadableData --acm "$SYSFONTACM"
    "$CONSOLECHARS" "$@" "${args[@]}"
  else # if [ -n "$SETFONT" ]; then
    "$SETFONT" -m "$SYSFONTACM".acm
    echo -ne '\033(K' > /dev/console
  fi
fi || let 'RETVAL |= 2'

# ACM set. If errors occured, the 2nd bit of RETVAL is set.

exit $RETVAL
