#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# kbd.SlackBuild
# 12-Mar-2004
#
# Some patches, font data & script was taken from Fedora's
# SRPM.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete and re-create temporary directories

# Add some extra fonts:
( cd $PKG ; explodepkg $CWD/sources/extraf.tgz )

# Extract source:
tar xvvf $CWD/sources/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Apply patches:
# These are taken from Fedora's SRPM:
zcat $CWD/sources/kbd-1.15.2-po.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/sources/kbd-1.15-keycodes-man.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/sources/kbd-1.15-unicode_start.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/sources/kbd-1.15-resizecon-x86_64.patch.gz | patch -p1 --verbose || exit 1

# This is from Mandriva's SRPM:
zcat $CWD/sources/kbd-1.15.3-fix-es-translation.patch.gz | patch -p1 --verbose || exit 1

# 7-bit maps are obsolete; so are non-euro maps
( cd data/keymaps/i386
  mv qwerty/fi.map qwerty/fi-old.map
  cp -fav qwerty/fi-latin9.map qwerty/fi.map
  cp -fav qwerty/pt-latin9.map qwerty/pt.map
  cp -fav qwerty/sv-latin1.map qwerty/se-latin1.map
  mv -fv azerty/fr.map azerty/fr-old.map
  cp -fav azerty/fr-latin9.map azerty/fr.map
  cp -fav azerty/fr-latin9.map azerty/fr-latin0.map # legacy alias

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

# Refresh to support latest architectures:
slackupdatelibtool

# Configure:
CFLAGS="-fcommon" \
./configure \
   --prefix=/usr \
   --localedir=/usr/share/locale/ \
   --mandir=/usr/man \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --datadir=/usr/share/kbd \
   --enable-nls \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# This is where it's always been in Slackware, so let's move it back:
( cd $PKG
  mkdir -vpm755 bin
  mv -fv usr/bin/loadkeys bin/
  cd usr/bin ; ln -vsf ../../bin/loadkeys . )

# Build/install resizecons:
# This doesn't work on ARM because of an #include sys/perm.h
# which does not exist on ARM.
( cd src
  make resizecons
  install -vpm755 resizecons $PKG/usr/bin
)

# ro_win.map.gz is useless
rm -fv $PKG/usr/share/kbd/keymaps/i386/qwerty/ro_win.map.gz

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

# Install the setup script that will be run from the Slackware installer:
mkdir -pm755 $PKG/var/log/setup
install -vpm755 $CWD/setup.setconsolefont $PKG/var/log/setup/
install -vpm755 $CWD/setconsolefont $PKG/usr/bin/

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/openvt
cp -a  ChangeLog COPYING* README* doc/* \
       $PKG/usr/doc/$PKGNAM-$VERSION
cp -a openvt/README* $PKG/usr/doc/kbd-$VERSION/openvt
rm -f $PKG/usr/doc/$PKGNAM-$VERSION/kbd.FAQ.sgml
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Additional keymaps:
# This is the keymap for Speakup (http://linux-speakup.org) users:
cat $CWD/sources/speakupmap.map.gz > $PKG/usr/share/kbd/keymaps/i386/qwerty/speakupmap.map.gz
# Another keymap for Speakup from Thomas Ward, for JFW users.
tar xvf $CWD/sources/speakup-jfw.tar.!(*sign|*asc|*sig)
( cd speakup-jfw
  cat speakup-jfw.map | gzip -9c > $PKG/usr/share/kbd/keymaps/i386/qwerty/speakup-jfw.map.gz
  cat readme > $PKG/usr/share/kbd/keymaps/i386/qwerty/speakup-jfw.readme )

# Create the default run control script which will set the
# console font to the "default"...  though this might not be
# the same state as if setfont were not used at all.
# For that reason, default the script to non-executable.
mkdir -pm755 $PKG/etc/rc.d
cat << EOF > $PKG/etc/rc.d/rc.font.new
#!/bin/sh
#
# This selects your default screen font from among the ones in
# /usr/share/kbd/consolefonts.
#
setfont -v
EOF
chmod 644 $PKG/etc/rc.d/rc.font.new

# Create package post-install script:
mkdir -p $PKG/install
cat << EOF > $PKG/install/doinst.sh

config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then
    # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

# Leave any new rc.font with the same permissions as the old one:
# This is a kludge, but it's because there's no --reference option
# on busybox's 'chmod':
if [ -e etc/rc.d/rc.font ]; then
  if [ -x etc/rc.d/rc.font ]; then
    chmod 755 etc/rc.d/rc.font.new
  else
    chmod 644 etc/rc.d/rc.font.new
  fi
fi
# Then config() it:
config etc/rc.d/rc.font.new

EOF

# We may at some point need to sed 's?SheevaPlug?Marvell?' 
# $PKG/var/log/setup/setup.setconsolefont
# since there will be more devices that don't need keyboard configurations.
#
# Generate acorn legacy keymaps.
# Note that with Linux 2.6, it knows about RiscPC keyboards and these
# other keymaps are not required.
# ARMedslack now uses Linux 2.6 as its default Kernel, so these keymaps are
# no longer being used as the defaults.
#if [ "$PORTARCH" = "arm" ]; then
#   $PORTCWD/arm/gen-acorn-keymaps
#   cat $PORTCWD/arm/doinst.sh >> $PKG/install/doinst.sh
#fi

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
