#!/bin/sh
# SlackBuild by Matteo Rossini (zerouno @ slacky.it)
# All rights reserved.
#
# Heavily based on the Slackware 13.0 SlackBuild
# http://ntlmaps.sourceforge.net/
#

set -e

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=ntlmaps
PKG=$TMP/package-$NAME
VERSION=1.0
ARCH=noarch
BUILD=1mt


SOURCE=http://ntlmaps.svn.sourceforge.net/viewvc/ntlmaps/trunk.tar.gz

[ ! -e $NAME-$VERSION.tar.gz ]&&wget $SOURCE -O $NAME-$VERSION.tar.gz
[ -d $TMP/$NAME-$VERSION ]&&rm -r $TMP/$NAME-$VERSION;mkdir -p $TMP/$NAME-$VERSION
[ -d $PKG ]&&rm -r $PKG;mkdir -p $PKG

tar xzvf $CWD/$NAME-$VERSION.tar.gz -C $TMP/$NAME-$VERSION
cd $TMP/$NAME-$VERSION/trunk
patch -p0 < $CWD/config.diff  
patch -p0 < $CWD/logdir.diff


chmod -R u+w,go+r-w,a-s .
chown -R root:root .

python packaging/setup.py build
python packaging/setup.py install -O1 --skip-build --root $PKG

mkdir -p $PKG/usr/lib/$NAME
mkdir -p $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
mkdir -p $PKG/var/log/ntlmaps/debug

cp doc/* COPYING $PKG/usr/doc/$NAME-$VERSION
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG

mv etc/ntlmaps/server.cfg etc/ntlmaps/server.cfg.new
rm -r etc/rc.d/init.d/
cp $CWD/rc.ntlmaps etc/rc.d/rc.ntlmaps.new
chmod -x etc/rc.d/rc.ntlmaps.new

requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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

