#!/bin/sh
. /etc/pkghelpers

PKGNAM=file
PKGVER=4.21
BUILD=1

pkghelpers_env

rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf file-$PKGVER
tar xjvf $CWD/file-$PKGVER.tar.bz2 || exit 1
cd file-$PKGVER

cd src
zcat $CWD/patch-4.20-REG_STARTEND.gz | patch -p0 --verbose || exit 1
cd ..

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
zcat $CWD/file.quiet.diff.gz | patch -p1 -E --verbose || exit 1
zcat $CWD/file.short.diff.gz | patch -p1 -E --verbose || exit 1
zcat $CWD/file.zisofs.magic.gz >> magic/Magdir/compress

./configure \
  --prefix=/usr \
	--libdir=/usr/lib$LIBSUFFIX \
  --sysconfdir=/etc \
  --datadir=/etc \
  --enable-fsect-man5 \
  --build=$ARCH-$DISTRO-linux

make -j6 || 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 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
)

strip -g $PKG/usr/lib$LIBSUFFIX/libmagic.a

mkdir -p $PKG/usr/doc/file-$PKGVER
cp -a \
  LEGAL.NOTICE README \
  $PKG/usr/doc/file-$PKGVER

cd $PKG
pkghelpers_fixup

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

pkghelpers_makepkg
