#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# http://savannah.nongnu.org/projects/flvstreamer
# Packager: ulisse89 (riccardo.trebbi89 AT gmail DOT com)
# http://www.slacky.it
#
# Slackware build script for flvstreamer.
# 
# This script is mainly taken from slackbuilds.org,
# I just adapted it in order to make it "slacky compliant".

NAME=flvstreamer
VERSION=${VERSION:-2.1c}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-uls}

CWD=$(pwd)
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf $NAME
tar xvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

export CFLAGS="$SLKCFLAGS"
make linux

mkdir -p $PKG/usr/bin
install -m 755 flvstreamer streams rtmpsrv rtmpsuck $PKG/usr/bin

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null || true
)

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a COPYING ChangeLog{,.rtmpdump} README $PKG/usr/doc/$NAME-$VERSION
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

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

cd $PKG
requiredbuilder -y -v -s $CWD $PKG
/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD$TAG.txz

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