#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# Requirements: xvidcore, divx4linux, lame, libdts, faad2, faac, a52dec, djbfft
# http://avifile.sourceforge.net/
# Packager Vincenzi Loris - slacky@slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package
NAME=avifile
VERSION=0.7.45
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=2sl

rm -rf $PKG
mkdir -p $PKG

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cd $TMP
tar xjvf $CWD/$NAME-0.7-0.7.45.tar.bz2
cd $NAME-0.7-0.7.45
cat $CWD/avifile-0.7-0.7.45-gcc4.patch | patch -p1 -E --backup --verbose
sed -i s/'int\*'/'unsigned int\*'/ $TMP/$NAME-0.7-0.7.45/samples/qtvidcap/kv4lsetup.cpp
sed -i s/'int\*'/'unsigned int\*'/ $TMP/$NAME-0.7-0.7.45/samples/qtvidcap/v4lxif.cpp

./autogen.sh
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
--mandir=/usr/man \
--with-win32-path=/usr/lib/codecs \
--enable-lame \
--program-prefix= \
--program-suffix= \
--build=$CHOST-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1
mkdir -p $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/man/man1
mv doc/*.1 $PKG/usr/man/man1
rm doc/Makefile*
rm doc/avicap/Makefile*
cp -ar doc/* $PKG/usr/doc/$NAME-$VERSION
cd $PKG
find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find $PKG/usr/man -type f -exec gzip -9 {} \;
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

ADD="lame >= 3.97-i486-1sl,libdts >= 0.0.2-i486-1as,faac >= 1.26-i486-2sl,faad2 >= 2.6.1-i486-1sl,a52dec >= 0.7.4-i486-1sl,djbfft >= 0.76-i686-1as" \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
