#!/bin/bash

# file.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@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/$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 $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

# Apply patches:
zcat $CWD/file.quiet.diff.gz | patch -p1 -E --verbose || failpatch
zcat $CWD/file.short.diff.gz | patch -p1 -E --verbose || failpatch
zcat $CWD/file.zisofs.magic.gz >> magic/Magdir/compress

# Configure:
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --datadir=/etc \
   --enable-fsect-man5 \
   --disable-static \
   --build=$ARCH-slackware-linux || failconfig

# Build:
make -j5 || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# Is file really this much of a processing bottleneck?  Doubtful.
# If you really need these (let's say you're doing virus scanning
# and this *would* speed things up quite a bit) you can create
# the pre-parsed files yourself using file's -C option.
rm -f $PKG/etc/file/magic.mgc $PKG/etc/file/magic.mime.mgc

# /etc/magic has been traditional for so long that it seems like a
# real good idea to provide a link:
( cd $PKG/etc
  ln -sf file/magic magic
)

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  LEGAL.NOTICE README \
  $PKG/usr/doc/$PKGNAM-$VERSION

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

# Apply generic Slackware packaging policies:
cd $PKG
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
