#!/bin/bash

# file.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>

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

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

# Apply patches:
# Make the default data directory /etc/file like it has always been:
zcat $CWD/file.etc.file.diff.gz | patch -p1 --verbose || exit 1
# This is needed after the patch above:
autoreconf || exit 1

# Don't spew warnings about using the flat text files
zcat $CWD/file.quiet.diff.gz | patch -p1 -E --verbose || exit 1
# I don't know what this one does
zcat $CWD/file.short.diff.gz | patch -p1 -E --verbose || exit 1

# Add zisofs and crda regulatory bin detection
zcat $CWD/file.zisofs.magic.gz >> magic/Magdir/compress
zcat $CWD/file.crdaregbin.magic.gz >> magic/Magdir/crdaregbin

# Configure:
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --datadir=/etc \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --docdir=/usr/doc/file-$VERSION \
   --enable-fsect-man5 \
   --disable-static \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# The generated $TMP/file-$VERSION/libtool is buggy.  I don't know if this is supposed
# to work, but it certainly does, so... :-)
cat $(which libtool) > libtool

# Build:
make $NUMJOBS || failmake

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

# Seems --disable-static has been ignored lately:
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/libmagic.a

# Is file really this much of a processing bottleneck?  Doubtful.
#
# NOTE:  Benchmarked flat files vs. .mgc June 2009, found no measurable
# difference on 3000+ files.
#
# 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 file yourself using file's -C option.
rm -f $PKG/etc/file/magic.mgc
mkdir -p $PKG/etc/file/magic
cp -a magic/Magdir/* $PKG/etc/file/magic
# After building, this works fine.  /etc/file/magic/ takes up 1.2M,
# while the magic.mgc file is 1.7M.  Considering a difference of 500k,
# and the potential speedup, I'm not sure it's worth the effort...
# but it does make it easier for an admin to add a small chunk of
# new magic.

# IMHO, moving this sort of thing does not make sense.
# We'll support both the traditional and new locations.
# I hate to squat on a name as generic as "/etc/misc/",
# but it wasn't my idea.
( cd $PKG/etc ; ln -sf file misc )

# Copy docs:
mkdir -p $PKG/usr/doc/file-$VERSION
cp -a \
  AUTHORS COPYING INSTALL MAINT NEWS README TODO \
    $PKG/usr/doc/file-$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
