#!/bin/sh

# Copyright (c) 1999-2001 Yann Dirson <dirson@debian.org>


CONFDIR=/etc/console
CONFFILEROOT=boottime
KMAPEXT=kmap

# Set up i18n if possible
TEXTDOMAIN=install-keymap
export TEXTDOMAIN

if which gettext >/dev/null
then
   GETTEXT="gettext -e -s"
else
   GETTEXT="echo -e"
fi

if [ $# != 1 ]
then
    $GETTEXT >&2 "Usage: install-keymap [ keymap_file | NONE | KERNEL ]"
    exit 1
fi

if [ "$1" =  "--help" ]
then
    $GETTEXT "Usage: install-keymap [ keymap_file | NONE | KERNEL ]"
    exit 0
fi
if [ "$1" = "--version" ]
then
   VERSION=`dpkg -l console-common | tail -n 1 | awk '{print $3}' `
   #  This string can not be localized, and it is not worth trying to
   echo "install-keymap: (console-common) " $VERSION
   exit 0
fi

keymap="$1"
if [ "$keymap" = NONE ]
then
    exit 0
fi

CONSOLE_TYPE=`fgconsole 2>/dev/null`
if  [ "$?" != "0" ]
then
   $GETTEXT >&2 "Deferring virtual terminal setup until console accessible."
   exit 0
fi

check_if_serial_console()
{
	if [ $CONSOLE_TYPE = "serial" ] 
	then
	   $GETTEXT >&2 "Warning: cannot install keymap on a serial console.\n deferring until non-serial console present."
	   exit 0
	fi
}

if ! which loadkeys >/dev/null
then
  $GETTEXT >&2 "Warning: no console utilities installed yet.\n deferring keymap setting until either console-tools or kbd is installed."
  exit 0
fi

do_preserving_keymap()
# execute shell command-line preserving the keymap
{
    # save keymap
    TMP=`tempfile`
    if ! dumpkeys >${TMP}; then
    	check_if_serial_console
	$GETTEXT >&2 "Failed to dump keymap!"
	$GETTEXT >&2 "This might be because your console cannot be opened.  Perhaps you don't have\na video card, are connected via the serial console or ssh.\nNot loading keymap!"

         rm -f ${TMP}
         exit 0
    fi

    if ! (eval "$*" && loadkeys -q ${TMP}); then
    	 check_if_serial_console
         $GETTEXT >&2 "Failed to preserve keymap!"
         rm -f ${TMP}
         exit 0
    fi

    rm -f ${TMP}
}

 # Be extra safe
mkdir -p ${CONFDIR}

NEW=`tempfile --suffix .gz`
CONFFILE=${CONFDIR}/${CONFFILEROOT}.${KMAPEXT}.gz
OLD_CONFFILE=${CONFDIR}/${CONFFILEROOT}.old.${KMAPEXT}.gz


backup()
{
    # If symlink, then don't overwrite, just provide new one
    if [ -h ${CONFFILE} ]
    then
	mv $NEW ${CONFFILE}.dpkg
	$GETTEXT >&2 "conffile ${CONFFILE} is a symlink : not overwriting"
	$GETTEXT >&2 "It is recommended that ${CONFFILE} is not a symlink; instead\nedit /etc/console-tools/remap to include any local changes."
	$GETTEXT >&2 "The new keymap has been placed in ${CONFFILE}.dpkg ;\nPlease move it as required."
    fi
    
    # Keep a backup if no backup already exists
    [ -f ${CONFFILE} -a ! -f ${OLD_CONFFILE} ] && 
	mv ${CONFFILE} ${OLD_CONFFILE}
    # Also backup this one, although it should probably not exist
    [ -f ${CONFFILE}%.gz} -a ! -r ${OLD_CONFFILE%.gz} ] && 
	mv ${CONFFILE%.gz} ${OLD_CONFFILE%.gz}
    
    if [ "$keymap" != KERNEL -a -f ${NEW} ]
    then
	mv $NEW $CONFFILE
    fi
    # Make sure perms are correct 
    [ -f ${CONFFILE} ] && chmod -f 644 ${CONFFILE} || true
    [ -f ${OLD_CONFFILE} ] && chmod -f 644 ${OLD_CONFFILE} || true
}

if [ "$keymap" = KERNEL ]
then
    backup
    rm -f ${CONFFILE}
else
    if [ "`dpkg --print-architecture`" = "arm" ] && 
	grep -i '^Hardware.*: \(Acorn-\|RiscStation-\)' /proc/cpuinfo >/dev/null &&
	find /usr/share/keymaps/i386/ -type f -name ${keymap} -o \
		    -name ${keymap}.gz -o -name ${keymap}.kmap.gz
    then
	# we're pretty sure now we were asked to load a PC keymap on a RiscPC
	$GETTEXT >&2 "Notice: doing keycode translation to use PC keymap on RiscPC"
	TRANSLATOR=/usr/share/console/ps2-to-riscpc
    else
	TRANSLATOR=cat
    fi

    # Running the translator at this point is the best way to look for problems, but
    # running it prior to "loadkeys" (where it would belong really) is mostly not possible
    # because of include files and include-like directives.
    # It may turn out that we really need to do keymap-file expansion differently because
    # of this.  Sigh.
    do_preserving_keymap "loadkeys -q $keymap ; dumpkeys | $TRANSLATOR | gzip -9n > $NEW"

    # if diff is available, use it to install only if needed
    if [ -r $CONFFILE ] && which diff >/dev/null
    then
	if diff -q $CONFFILE $NEW >/dev/null
	then
	    rm $NEW
	else
	    backup
	fi
    else
	backup
    fi

    if ! loadkeys -q $CONFFILE; then
        $GETTEXT >&2 "Failed to load keymap!"
    fi
    # Store md5sum. debconf code uses this to track keymaps it has installed,
    # so it doesn't change ones installeed by user. Its compressed, so we
    # don't want to use the usual conffiles mechanism.
    . /usr/share/debconf/confmodule
    MD=`md5sum $CONFFILE | cut -f1 -d' ' `
    db_set console-data/bootmap-md5sum $MD
fi
