#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# file.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2005-2008, 2009, 2010, 2013, 2015  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# 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="$SLKCFLAGS -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
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 default data directory patch above:
autoreconf -vif || 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

# Output on ELF objects should be "dynamically linked" for PT_DYNAMIC too, not
# just for PT_INTERP:
zcat $CWD/file.PT_DYNAMIC.is.dynamically.linked.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

# Add some regression fixes from upstream:

# 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=${SLK_ARCH_BUILD} || 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 || make || failmake

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

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

# Install the flat files.
# We'll generate /etc/file/magic.mgc in the doinst.sh.
rm -f $PKG/etc/file/magic.mgc
mkdir -p $PKG/etc/file/magic
cp -a magic/Magdir/* $PKG/etc/file/magic
chmod 644 $PKG/etc/file/magic/*

# Add a script to recompile the flat files in the obvious location.
# The package install script will compile the flat files initially.
cp -a $CWD/recompile_magic.mgc.sh.gz $PKG/etc/file
gzip -d $PKG/etc/file/recompile_magic.mgc.sh.gz
chown root:root $PKG/etc/file/recompile_magic.mgc.sh
chmod 755 $PKG/etc/file/recompile_magic.mgc.sh

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

# Apply generic Slackware packaging policies:
cd $PKG
slack_delete_lafiles # delete usr/lib{,64}/*.la
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
