#!/bin/sh

# Copyright 2009  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.

VERSION=5.00
BUILD=${BUILD:-1}

. /etc/pkghelpers
pkghelpers_env

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf file-$VERSION
tar xvf $CWD/file-$VERSION.tar.bz2 || exit 1
cd file-$VERSION || exit 1
pkghelpers_permissions

# 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

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --sysconfdir=/etc \
  --datadir=/etc \
  --mandir=/usr/man \
  --infodir=/usr/info \
  --docdir=/usr/doc/file-$VERSION \
  --enable-fsect-man5 \
  --disable-static \
  --build=$ARCH-$DISTRO-linux

make -j$NUMJOBS || make || exit 1
make install DESTDIR=$PKG || exit 1

# 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 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...

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

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
pkghelpers_fixup
pkghelpers_makepkg