#!/bin/sh
#
# getkeymaps
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

ARCH=$1
if [ -z "$ARCH" ]; then
    echo "usage: $0 <arch>"
    exit 1
fi

TOPDIR=`pwd`
OUTPUT=$2
if [ -z "$OUTPUT" ]; then
    echo "No output specified, using ${TMPDIR:-/tmp}/keymaps-$ARCH.$$"
    OUTPUT=${TMPDIR:-/tmp}/keymaps-$ARCH.$$
fi

UTILDIR=$3
if [ -z "$UTILDIR" ]; then
    READMAP=../utils/readmap
    MAPSHDR=$TOPDIR/../utils/mapshdr
else    
    READMAP=$UTILDIR/usr/lib/anaconda-runtime/readmap
    MAPSHDR=$UTILDIR/usr/lib/anaconda-runtime/mapshdr
fi

TMP=${TMPDIR:-/tmp}/keymaps.$$

rm -rf $TMP
mkdir -p $TMP

if [ $ARCH = "sparc" ]; then
    PATTERN={i386,sun}
else
    PATTERN=i386
fi

MAPS=$(python -c "import system_config_keyboard.keyboard_models ; system_config_keyboard.keyboard_models.get_supported_models()")

for map in $MAPS ; do 
 eval find /lib/kbd/keymaps/$PATTERN -name "$map.map*.gz" | while read n; do
    /bin/loadkeys `basename $n .gz` >/dev/null
    $READMAP $TMP/`basename $n .map.gz`.map
  done
done

loadkeys us

rm -f $TMP/defkeymap* $TMP/ANSI* $TMP/lt.map

(cd $TMP; $MAPSHDR *.map) > $TMP/hdr
cat $TMP/hdr $TMP/*.map | gzip -9 > $OUTPUT
rm -rf $TMP
