#!/bin/sh
#
# Heavily based on the Slackware 12.0 SlackBuild
# http://oxine.sourceforge.net
# Packager neongen ( neongen at libero.it)
# http://www.slacky.it

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

NAME=oxine
VERSION=0.7.1
ARCH=${ARCH:-i686}
CHOST=i486
BUILD=1ng
PKG=$TMP/package-$NAME

SOURCE=http://garr.dl.sourceforge.net/sourceforge/oxine/$NAME-$VERSION.tar.gz

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

echo "+=============================================================================="
echo "| $NAME-$VERSION"
echo "+=============================================================================="

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

cd $TMP

echo "Unpacking $NAME..."
tar -xzf $CWD/$NAME-$VERSION.tar.gz

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
#cd $NAME
chown -R root:root .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure  --prefix=/usr \
--build=$CHOST-slackware-linux
#--docdir=/usr/doc/$NAME-$VERSION \

make
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
 AUTHORS COPYING ChangeLog INSTALL 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
# )

chown root:root -R $PKG
chmod 644 $PKG/usr/doc/$NAME-$VERSION/*

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

# 	echo "if [ -x /usr/bin/scrollkeeper-update ]; then
#   /usr/bin/scrollkeeper-update -p /var/lib/scrollkeeper &> /dev/null
# fi" >> $PKG/install/doinst.sh
# 	echo "" >> $PKG/install/doinst.sh	
# 	echo "if [ -x /usr/bin/update-mime-database ]; then
#   /usr/bin/update-mime-database /usr/share/mime &> /dev/null
# fi" >> $PKG/install/doinst.sh
# 	echo "" >> $PKG/install/doinst.sh
# 	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

#gzip -9 $PKG/usr/man/*/*

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

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
  rm -rf $TMP/$NAME-$VERSION.tar.gz*
fi
