#!/bin/bash
#  Copyright (C) 2009-2010  Matias A. Fonzo, <selk@dragora.org>
#  Copyright (C) 2012  Matias A. Fonzo, <selk@dragora.org>
#
#  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 3 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/>.

set -e

CWD=$(pwd)

TMP=${TMP:-/tmp/sources}
OUT=${OUT:-/tmp/packages}

V=1.15.3
ARCH=${ARCH:-i486}
B=1

# Flags for the compiler:
DCFLAGS=${DCFLAGS:=-O2 -march=i486 -mtune=i686}

PKG=${TMP}/package-kbd

rm -rf $PKG
mkdir -p $PKG $OUT

echo "Uncompressing the tarball..."
rm -rf ${TMP}/kbd-${V}
lzip -cd ${CWD}/kbd-${V}.tar.lz | tar -xvf - -C $TMP

cd ${TMP}/kbd-${V}

# Set sane ownerships and permissions:
chown -R 0:0 .
find . \
 \( -perm 2777 -o \
    -perm 777  -o \
    -perm 775  -o \
    -perm 711  -o \
    -perm 555  -o \
    -perm 511     \
 \) -exec chmod 755 {} + \
  -o \
 \( -perm 666 -o \
    -perm 664 -o \
    -perm 600 -o \
    -perm 444 -o \
    -perm 440 -o \
    -perm 400    \
 \) -exec chmod 644 {} +

# The behaviour of the Backspace and Delete keys is not consistent across the
# keymaps in the Kbd package.  The following patch fixes this issue for
# i386 keymaps:
zcat ${CWD}/patches/kbd-1.15.1-backspace-1.patch.gz | patch -p1 --verbose

# Set the default font for unicode_start:
zcat ${CWD}/patches/kbd-default-unicode_start-font.patch.gz | \
 patch -p0 --verbose

zcat ${CWD}/patches/kbd-1.15.3-fix-es-translation.patch.gz | patch -p1 --verbose
zcat ${CWD}/patches/kbd-1.15.3-dumpkeys-man.patch.gz | patch -p1 --verbose
zcat ${CWD}/patches/kbd-1.15.3-loadkeys-d.patch.gz | patch -p1 --verbose

# This is from the Fedora's spec file:
# http://cvs.fedoraproject.org/viewvc//rpms/kbd/devel/kbd.spec?view=markup
( cd data/keymaps/i386
  # 7-bit maps are obsolete; so are non-euro maps:
  mv qwerty/fi.map qwerty/fi-old.map
  cp -a qwerty/fi-latin9.map qwerty/fi.map
  cp -a qwerty/pt-latin9.map qwerty/pt.map
  cp -a qwerty/sv-latin1.map qwerty/se-latin1.map
  mv azerty/fr.map azerty/fr-old.map
  cp -a azerty/fr-latin9.map azerty/fr.map
  # Legacy alias:
  cp -a azerty/fr-latin9.map azerty/fr-latin0.map

  # Rename conflicting keymaps:
  mv dvorak/no.map dvorak/no-dvorak.map
  mv fgGIod/trf.map fgGIod/trf-fgGIod.map
  mv olpc/es.map olpc/es-olpc.map
  mv olpc/pt.map olpc/pt-olpc.map
  mv qwerty/cz.map qwerty/cz-qwerty.map
)

# The Kbd package doesn't come shipped with the standard configure scripts, so:
#autoreconf --install --force --verbose

CFLAGS="$DCFLAGS" \
./configure \
 --prefix=/usr \
 --mandir=/usr/man \
 --docdir=/usr/doc/kbd-${V} \
 --localedir=/usr/share/locale \
 --build=${ARCH}-dragora-linux-gnu

make
make install DESTDIR=$PKG

# Create additional name for Serbian latin keyboard:
ln -sf sr-cy.map.gz ${PKG}/usr/share/keymaps/i386/qwerty/sr-latin.map.gz

# The rhpl keyboard layout table is indexed by kbd layout names,
# so we need a Korean keyboard:
ln -sf us.map.gz ${PKG}/usr/share/keymaps/i386/qwerty/ko.map.gz

# Strip binaries & libraries (if any):
( cd $PKG
  find . -type f | xargs file | awk '/ELF/ && /executable/ || /shared object/' | \
   cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Add the "keyconfig" script:
mkdir -p ${PKG}/usr/sbin
cat ${CWD}/keyconfig/keyconfig > ${PKG}/usr/sbin/keyconfig
chmod 750 ${PKG}/usr/sbin/keyconfig

# Translation files:
( cd ${CWD}/keyconfig/po
  for file in * ; do
    ext=${file#*.*.}

    mkdir -p ${PKG}/usr/share/locale/${ext}/LC_MESSAGES
    msgfmt -o ${PKG}/usr/share/locale/${ext}/LC_MESSAGES/keyconfig.mo $file

    # Convert our translation files to UTF-8:
    mkdir -p ${PKG}/usr/share/locale/${ext}.utf8/LC_MESSAGES
    iconv --from-code=ISO8859-1 --to-code=UTF8 $file | msgfmt - -o \
     ${PKG}/usr/share/locale/${ext}.utf8/LC_MESSAGES/keyconfig.mo
  done
)

# Compress man-pages (if any):
if [[ -d ${PKG}/usr/man ]]; then
  gzip -9N ${PKG}/usr/man/man?/*
fi

# Add the documentation:
mkdir -p ${PKG}/usr/doc/kbd-${V}
cp -a \
 AUTHORS COPYING ChangeLog NEWS README doc/ \
 ${PKG}/usr/doc/kbd-${V}

# Copy the description files:
mkdir -p ${PKG}/description
cp ${CWD}/description/* ${PKG}/description

cd $PKG
makepkg -l ${OUT}/kbd-${V}-${ARCH}-${B}.tlz

