#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://www.ladspa.org/
# Packager Vincenzi Loris - slacky@slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=ladspa_sdk
PKG=$TMP/package/$NAME
VERSION=1.13
ARCH=${ARCH:-i486}
BUILD=5slacky
SOURCE=http://www.ladspa.org/download/$NAME'_'$VERSION.tgz

if [ ! -e $NAME'_'$VERSION.tgz ]; then
wget -c $SOURCE
fi


if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
fi



if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi


cd $TMP
tar xzvf $CWD/$NAME'_'$VERSION.tgz || exit 1
cd $NAME
chown -R root:root .
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 {} \;

( cd src
  make \
    CFLAGS="$SLKCFLAGS -I. -Wall -Werror -fPIC" \
    INSTALL_PLUGINS_DIR="/usr/lib${LIBDIRSUFFIX}/ladspa" \
    INSTALL_BINARY_DIR="/usr/bin"

  make install \
    INSTALL_PLUGINS_DIR="$PKG/usr/lib${LIBDIRSUFFIX}/ladspa" \
    INSTALL_BINARY_DIR="$PKG/usr/bin" \
    INSTALL_INCLUDE_DIR="$PKG/usr/include"
 )


mkdir -p $PKG/etc/profile.d/
cat << EOF > $PKG/etc/profile.d/ladspa.csh
#!/bin/csh
setenv LADSPA_PATH /usr/lib${LIBDIRSUFFIX}/ladspa
EOF
cat << EOF > $PKG/etc/profile.d/ladspa.sh
#!/bin/sh
export LADSPA_PATH=/usr/lib${LIBDIRSUFFIX}/ladspa
EOF
chmod 0755 $PKG/etc/profile.d/*

mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a README 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 . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)


mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/$NAME.SlackBuild

cd $PKG
chown -R root:root $PKG
if [ -x "$(which requiredbuilder)" ]; then
requiredbuilder -y -v -s $CWD $PKG
fi
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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