#!/bin/bash

# kbd.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 12-Mar-2004

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

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

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

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply a euro fix for the nl.map from the map's author:
zcat $CWD/nl.euro.diff.gz | patch -p1

# Configure:
./configure \
   --prefix=/usr || failconfig

# Build:
make || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall
( cd $PKG/usr/bin ; rm -f loadkeys ; ln -sf ../../bin/loadkeys . )
mkdir -pm755 $PKG/var/log/setup $PKG/install
install -pm755 $CWD/setup.setconsolefont \
               $PKG/var/log/setup/
install -m755 $CWD/setconsolefont $PKG/usr/bin

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION/openvt
cp -a  CHANGES COPYING CREDITS README doc/* \
       $PKG/usr/doc/$PACKAGE-$VERSION
cp -a openvt/README* $PKG/usr/doc/kbd-$VERSION/openvt
rm -f $PKG/usr/doc/$PACKAGE-$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 zxvvf $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

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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