#!/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=armv5te -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"
            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

# Since we're going to include the compiled magic database now (otherwise
# there seem to be libmagic related crashes), this patch probably doesn't
# need to be applied.
#
## Don't spew warnings about using the flat text files
#zcat $CWD/file.quiet.diff.gz | patch -p1 -E --verbose || exit 1

# Keep the output format for ELF binaries the same as it was in earlier
# versions of file to avoid breaking existing scripts:
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

# Update:  We're going to use magic.mgc now as the flat files cause
# libmagic crashes with versions newer than file-5.11.  Maybe that
# wasn't being tested as well...  in any case, using magic.mgc works.
#
#
## 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
