#!/bin/sh
# Heavily based on the Slackware 13.37 SlackBuild
# http://www.inter7.com/index.php?page=eps
# http://www.slacky.eu


CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=eps
PKG=$TMP/package/$NAME
VERSION=1.7
ARCH=${ARCH:-i486}
BUILD=${BUILD:-6}
TAG=${TAG:-sl}

SOURCE=http://www.inter7.com/eps/$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 -Wall"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686 -Wall"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC -Wall"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"; CHOST="x86_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

echo -e "\E[0;32m+----------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild EPS |\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 {} \;

sed -i "s|-Wall|$SLKCFLAGS|g" Makefile
make
make install LIBDIR=$PKG/usr/lib${LIBDIRSUFFIX} INCDIR=$PKG/usr/include

mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a 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 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

