#!/bin/bash

# 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:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# 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/extraf.tgz )

# Extract source:
#tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION
slackhousekeeping

# 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 )


# Apply patches:
patch --verbose -p1 < $PORTCWD/sources/kbd-1.15-po.patch
patch --verbose -p1 < $PORTCWD/sources/kbd-1.15-keycodes-man.patch
patch --verbose -p1 < $PORTCWD/sources/kbd-1.15-sparc.patch
patch --verbose -p1 < $PORTCWD/sources/kbd-1.15-unicode_start.patch
patch --verbose -p1 < $PORTCWD/sources/kbd-1.15-resizecon-x86_64.patch
patch --verbose -p1 < $PORTCWD/sources/kbd-1.15-quiet_doc.patch
# 

# Configure:
./configure \
   --prefix=/usr \
   --localedir=/usr/share/locale/ \
   --mandir=/usr/man \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --datadir=/usr/share/kbd \
   --enable-nls || failconfig

# Build:
make $NUMJOBS || 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 . )
mkdir -pm755 $PKG/var/log/setup $PKG/install

#install -pm755 $CWD/setup.setconsolefont \

# Use the ARMedslack version which exits if found running on 
# a SheevaPlug which has no VGA, so we don't need to set a console
# font from the installer.
# This should make it back into Slackware for kbd-1.15.
install -vpm755 $PORTCWD/sources/setup.setconsolefont \
               $PKG/var/log/setup/
install -m755 $CWD/setconsolefont $PKG/usr/bin
# Patch setconsole font to use chroot so that 'setfont' works
# inside the installer:
( cd $PKG
  patch --verbose -p0 < $PORTCWD/sources/setconsolefont.chroot.diff )

# ro_win.map.gz is useless
rm -f $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 -s us.map.gz $PKG/usr/share/kbd/keymaps/i386/qwerty/ko.map.gz

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/openvt
cp -a  CHANGES COPYING CREDITS 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

# This is the keymap for Speakup (http://linux-speakup.org) users:
cat $CWD/speakupmap.map.gz > $PKG/usr/share/kbd/keymaps/i386/qwerty/speakupmap.map.gz

# Another keymap for Speakup from Thomas Ward, for JFW users.
tar xvvf $CWD/speakup-jfw.tar.gz
( cd speakup-jfw
  gzip -9c speakup-jfw.map > $PKG/usr/share/kbd/keymaps/i386/qwerty/speakup-jfw.map.gz
  install -pm644 readme $PKG/usr/share/kbd/keymaps/i386/qwerty/speakup-jfw.readme )
mkdir -p $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 755 $PKG/etc/rc.d/rc.font.new
mv $PKG/usr/share/man $PKG/usr

# 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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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
