#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# Requirements: qt4
# http://www.lyx.org
# packager: mauro.sacchetto@alice.it

set -e

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-lyx
NAME=lyx
VERSION=1.6.4
ARCH=i486
BUILD=1ms

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

SOURCE="ftp://ftp.lyx.org/pub/lyx/stable/1.6.x/lyx-${VERSION}.tar.bz2"

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

echo "+---------------------------------+"
echo "| Start SlackBuild $NAME-$VERSION      |"
echo "+---------------------------------+"

cd $TMP
tar xjvf $CWD/$NAME-$VERSION.tar.bz2

cd $NAME-$VERSION
chown -R root:root .
CFLAGS="-O2 -march=i486 -mtune=i686" \
CXXFLAGS="-O2 -march=i486 -mtune=i686" \
./configure --prefix=/usr \
--with-qt4-dir=/opt/qt4 \
--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 ABOUT-NLS ANNOUNCE COPYING INSTALL* NEWS README* UPGRADING \
$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

gzip $PKG/usr/man/*/*

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
ADD=$(ls /var/log/packages/ | sed -ne 's/\(qt4\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2/p') \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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