#!/bin/bash

# brltty.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

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

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

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac 

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Configure:
sh autogen
./configure \
   --prefix=/usr \
   --bindir=/bin \
   --sbindir=/sbin \
   --libdir=/lib \
   --infodir=/usr/info \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --disable-java-bindings \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

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

# Install into package:
make install INSTALL_ROOT=$PKG 
rm -r $PKG/dev
chmod 755 $PKG/bin/* $PKG/lib/brltty/*

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  README* COPYING* \
  Documents/{Manual-*,TODO,CONTRIBUTORS,HISTORY,README.*} \
  $PKG/usr/doc/brltty-$VERSION
mv Documents/ChangeLog .
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

( cd $PKG/usr/doc/brltty-$VERSION
  find . -name "Makefile*" -exec rm -f {} \;
  find . -name "*.sgml" -exec rm -f {} \;
  find . -size 0 -type f -exec rm -f {} \;
)

# Write out the doinst.sh:
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...
}
config etc/brltty.conf.new

EOF

# 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

# /extra has packages in separate dirs:
export PKGSERIES=$PKGSERIES/$PKGNAM
slackmp         # run makepkg

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