#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://jags.sourceforge.net/

TMP=/tmp

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

PKG=$TMP/jags-package

CWD=`pwd`

VERSION=0.22.1
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1loo}

rm -fr $PKG
mkdir -p $PKG

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

cd jags-0.22.1
zcat $CWD/jags.diff.gz | patch -p1 -E --backup --verbose

chown -R root:root .

SLKCFLAGS="-O2 -march=i486 -mtune=i686"
CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./configure --prefix=/usr \
--program-prefix= \
--program-suffix= \
--build=i486-slackware-linux

make -j3
make install DESTDIR=$PKG

(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
)

mkdir -p $PKG/usr/doc/jags-$VERSION
    cp -a AUTHORS COPYING INSTALL NEWS README \
    $PKG/usr/doc/jags-$VERSION

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

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

if [ "$1" == "--cleanup" ]; then
    rm -fr $TMP/jags-$VERSION
    rm -fr $TMP/jags-package
fi

