#!/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=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

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


# Build:
make $NUMJOBS || 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 -favv  README* COPYING* \
          Documents/{Manual-*,TODO,CONTRIBUTORS,ChangeLog,HISTORY,README.*} \
         $PKG/usr/doc/$PKGNAM-$VERSION

mkdir -p $PKG/etc
cp -a Documents/brltty.conf $PKG/etc/brltty.conf.new

# 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
