#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://www.webmproject.org/
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.eu
# Thanks to <matteo.bernardini {at} sns.it>
#
# Required: Yasm only compilation
#
#
#
# Exit on most errors
set -e 


# Set variables:
CWD=`pwd`
OUTPUT=${OUTPUT:-$CWD}
PKGNAME=libvpx
TMP=${TMP:-/tmp/txz/$PKGNAME}
PKG=$TMP/package
VERSION=${VERSION:-0.9.5}
ARCH=${ARCH:-i486}
JOBS=${JOBS:--j2}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}

# Download sorce tarball if still not present
SOURCE=http://webm.googlecode.com/files/$PKGNAME-v$VERSION.tar.bz2

if [ ! -e $CWD/$PKGNAME-v$VERSION.tar.bz2 ]; then
wget -c $SOURCE
fi


# Set compiling FLAGS
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
  ARCHOPTS="--target=x86-linux-gcc"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  ARCHOPTS="--target=x86-linux-gcc"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  ARCHOPTS="--target=x86_64-linux-gcc --enable-pic"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  ARCHOPTS="--target=${ARCH}-linux-gcc"
fi


rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT


# Decompress the source tarball
cd $TMP
tar xvjf $CWD/$PKGNAME-v$VERSION.tar.bz2

echo -e "\E[0;32m+-------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild Libvpx |\E[0;0m"
echo -e "\E[0;32m+-------------------------+\E[0;0m"

# Adjust permissions and ownerships
cd $PKGNAME-v$VERSION
chmod -R u+w,go+r-w,a-s .
chown -R root:root .

# Building Libvpx package
LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure 	--prefix=/usr \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--disable-debug-libs \
		--disable-debug \
		--enable-postproc \
		--enable-vp8 \
		--enable-shared \
		$ARCHOPTS
make $JOBS
make install DESTDIR=$PKG


# Strip binaries and shared objects, if present
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
)


#Add documentation files: readme, changelog and so on
mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild
cp -a AUTHORS CHANGELOG LICENSE PATENTS README  $PKG/usr/doc/$PKGNAME-$VERSION
chmod 644 $PKG/usr/doc/$PKGNAME-$VERSION/*



# Also add the slack-desk file and this slackbuild to the package
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild/$PKGNAME.SlackBuild



# Find and print dependencies into slack-required file
cd $PKG
chown -R root:root $PKG
if [ -x /usr/bin/requiredbuilder ];then
requiredbuilder -y -v -s  $CWD $PKG
fi
# Create txz package
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}


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


