#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://gretl.sourceforge.net/gretl_italiano.html

NAME=gretl
VERSION=1.8.0
ARCH=${ARCH:-i486}
BUILD=1mf
CWD=`pwd`

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

PKG=$TMP/package-$NAME

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; 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

mkdir -p $PKG/usr

cd $TMP

tar xvjf $CWD/$NAME-$VERSION.tar.bz2

cd $NAME-$VERSION

CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr --build=i486-slackware-linux

make
make prefix=$PKG/usr install

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a ChangeLog COPYING EXTENDING INSTALL README README.audio README.win32 TODO $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
mkdir -p $PKG/usr/share/pixmaps
mkdir -p $PKG/usr/share/applications
cat $CWD/slack-desc > $PKG/install/slack-desc
cp $CWD/gretl.png $PKG/usr/share/pixmaps/gretl.png
cat $CWD/Gretl.desktop > $PKG/usr/share/applications/gretl.desktop
cp $CWD/$NAME.SlackBuild $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
)

mv $PKG/usr/share/man $PKG/usr
gzip -9 $PKG/usr/man/*/*
chmod 755 $PKG/usr/bin/*

cd $PKG

chown -R root:root .

requiredbuilder -v -y -s $CWD $PKG

cat $CWD/slack-required > $PKG/install/slack-required
cat $CWD/slack-required > $PKG/usr/doc/$NAME-$VERSION/slack-required

makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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