#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://www.ffado.org
# Packagers Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
# Thanks to Heinz Wiesinger
# Required: Glibmm, Libiec61883, Libsigc++, Libxml++. 


CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=libffado
PKG=$TMP/package/$NAME
VERSION=2.0.0
ARCH=${ARCH:-i686}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}
SOURCE=http://www.ffado.org/files/$NAME-$VERSION.tar.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

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

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

cd $TMP
tar xvzf $CWD/$NAME-$VERSION.tar.gz

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

cd $NAME-$VERSION

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 {} \;

# Use our CFLAGS
sed -i "s/-O2 -DNDEBUG/$SLKCFLAGS -DNDEBUG/" SConstruct

scons \
  PREFIX=/usr \
  LIBDIR=/usr/lib${LIBDIRSUFFIX} \
  DIST_TARGET="$TARGET" \
  DEBUG="False"

scons \
  --implicit-deps-unchanged \
  PREFIX=/usr \
  LIBDIR=/usr/lib${LIBDIRSUFFIX} \
  DIST_TARGET="$TARGET" \
  DEBUG="False" \
  DESTDIR=$PKG \
  install


mkdir -p $PKG/usr/share/{applications,pixmaps}
install -m 0644 support/xdg/ffado.org-ffadomixer.desktop \
  $PKG/usr/share/applications/ffadomixer.desktop
install -m 0644 support/xdg/hi64-apps-ffado.png \
  $PKG/usr/share/pixmaps/ffado.png



mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a AUTHORS ChangeLog LICENSE* NEWS README TODO $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


	echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/gtk-update-icon-cache ]; then
  /usr/bin/gtk-update-icon-cache  -f -q /usr/share/icons/hicolor &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh


cd $PKG
chown -R root:root $PKG
if [ -x "$(which depfinder 2>/dev/null)" ];then
  depfinder -f -s -a -p $PKG
  mv slack-required $CWD/
elif [ -x "$(which requiredbuilder 2>/dev/null)" ];then
  requiredbuilder -y -v -s $CWD $PKG
fi

/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

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