#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
#http://mldonkey.sourceforge.org/

CWD=`pwd`
if ["$TMP" = ""]; then
TMP=/tmp
fi
# ----------- Package Informations -----------
PKG=$TMP/package-mldonkey
NAME=mldonkey
VERSION=2.9.6
ARCH=i486
BUILD=1sid
#COMP=targz
COMP=tarbz2

# ----------- CFLAGS -------------
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"
  LADD=--libdir=/usr/lib64
fi
# ----------- Compilation ----------
if [ ! -d $TMP ]; then
mkdir -p $TMP
fi

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

cd $TMP
if [ "$COMP" = "tarbz2" ]; then
    tar xvjf $CWD/$NAME-$VERSION.tar.bz2
elif [ "$COMP" = "targz" ]; then
    tar xvzf $CWD/$NAME-$VERSION.tar.gz
fi

cd $NAME-$VERSION

CFLAGS="$SLKCFLAGS" \
./configure \
--prefix=/usr \
--enable-multinet \
--enable-batch \
--enable-gui \
--enable-ocamlver=3.10.1 \
--build=$ARCH-slackware-linux
make -j4
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION
(cd distrib && cp [A-Z]* $PKG/usr/doc/$NAME-$VERSION)

strip --strip-unneeded $PKG/usr/bin/mlnet

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
mkdir -p $PKG/{etc,etc/rc.d}
cat $CWD/rc.mldonkey > $PKG/etc/rc.d/rc.mldonkey
chown root $PKG/etc/rc.d/rc.mldonkey && chmod +x $PKG/etc/rc.d/rc.mldonkey
cat $CWD/mldonkey.conf > $PKG/etc/mldonkey.conf
chown root $PKG/etc/mldonkey.conf && chgrp root $PKG/etc/mldonkey.conf

cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
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
fi
