#!/bin/bash

# pilot-link.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 20-Nov-2004

# 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 & 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" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

zcat $CWD/pilot-link.serial.group.is.uucp.diff.gz | patch -p1 --verbose || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --localstatedir=/var/lib \
  --docdir=/usr/doc/pilot-link-$VERSION \
  --mandir=/usr/man \
  --enable-static=no \
  --enable-conduits \
  --enable-threads \
  --enable-libusb \
  --with-libpng \
  --with-perl \
  --with-python \
  --program-prefix="" \
  --program-suffix="" \
  --build=$ARCH-slackware-linux || failconfig

# Build:
make -i $NUMJOBS || failmake

# Install into package:
make -i install DESTDIR=$PKG || failinstall
# Get rid of perllocal.pod:
rm -f $PKG/usr/lib/perl5/$(perl -v | head -2 | tail -1 | cut -f 2 -d v | cut -f 1 -d ' ')/*/perllocal.pod

# Right.
mkdir -p $PKG/lib/udev/rules.d
mv $PKG/usr/share/pilot-link/udev/60-libpisock.rules \
  $PKG/lib/udev/rules.d/80-libpisock.rules
rmdir $PKG/usr/share/pilot-link/udev

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  AUTHORS COPYING* INSTALL NEWS README \
  $PKG/usr/doc/$PKGNAM-$VERSION

# Install man pages:
mkdir -p $PKG/usr/man/man{1,7}
cp -a doc/man/*.1 $PKG/usr/man/man1
cp -a doc/man/*.7 $PKG/usr/man/man7

# Dropped one.
if [ -d $PKG/usr/share/man/man3 ]; then
  mkdir -p $PKG/usr/man/man3
  mv $PKG/usr/share/man/man3/* $PKG/usr/man/man3
  rmdir $PKG/usr/share/man/man3 2> /dev/null
  rmdir $PKG/usr/share/man 2> /dev/null
  rmdir $PKG/usr/share 2> /dev/null
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
