#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://id3lib.sourceforge.net/
# Packager Vincenzi Loris - slacky@slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=id3lib
PKG=$TMP/package/$NAME
VERSION=3.8.3
ARCH=${ARCH:-i486}
BUILD=5sl
SOURCE=http://ovh.dl.sourceforge.net/sourceforge/id3lib/$NAME-$VERSION.tar.gz

UBUNTU_PATCHFILE=id3lib3.8.3_3.8.3-7.2ubuntu2.diff.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; 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.tar.gz || exit 1

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

# Applying ALL Ubuntu patches ;)
zcat $CWD/$UBUNTU_PATCHFILE | patch -p1 --verbose || exit 1
echo "Applying patches now..."
for i in `cat debian/patches/series | grep -v ^#` ; do \
  echo "--- $i ---" ; patch -p1 --verbose < debian/patches/$i ; \
done

# strictly necessary after all these patches...
#aclocal && automake && autoconf
aclocal
libtoolize --copy --force --automake
autoheader
automake --gnu --add-missing --copy --force
autoconf

LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure 	--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--disable-static \
		--mandir=/usr/man \
		--program-prefix= \
		--program-suffix= \
		--build=$ARCH-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1


mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a AUTHORS ChangeLog COPYING doc HISTORY INSTALL NEWS THANKS TODO $PKG/usr/doc/$NAME-$VERSION
find $PKG/usr/doc/$NAME-$VERSION -type d -exec chmod 755 '{}' ';'
find $PKG/usr/doc/$NAME-$VERSION -type f -exec chmod 644 '{}' ';'


mkdir -p $PKG/usr/man/man1
cp $PKG/usr/doc/$NAME-$VERSION/doc/man/* $PKG/usr/man/man1
rm -r $PKG/usr/doc/$NAME-$VERSION/doc


( 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


find $PKG/usr/man -type f -exec gzip -9 {} \;


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

