#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# SlackBuild for udns
# http://www.corpit.ru/mjt/udns.html
# By Luke88 <luke88@slacky.it>
# 11/10/2008
#
# Notes:
#   This lib does NOT have a nice make/make install script,
#   so we have to install it by hand.
#   have a look at the gentto ebuild:
#   http://sources.gentoo.org/viewcvs.py/gentoo-x86/net-libs/udns/udns-0.0.9.ebuild?view=markup
#   that however gives you just the libraries, look at any rpm resource to find more.

# Set variables
CWD=`pwd`
TMP=/tmp

NAME=udns
VERSION=0.0.9
ARCH=i486
BUILD=3lke
SOURCEARCH=$NAME-$VERSION
PKG=$TMP/package-$NAME

# Print a welcome screen
echo "+-----------------+"
echo "| $NAME-$VERSION |"
echo "+-----------------+"

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

#just be sure not to have an other half-built package
if [ -d $PKG ]
then
	echo "Previous package build directory found, please remove it and"
        echo "restart the SlackBuild script: $PKG"
	exit 1
else
	mkdir -p $PKG
fi

# Decompress
cd $TMP
#udns_0.0.9, not udns-0.0.9
tar zxvf $CWD/$NAME\_$VERSION.tar.gz
cd $NAME-$VERSION

# Build. Not many options given either... 
CFLAGS="-O2 -march=i486 -mtune=i686" \
CXXFLAGS="-O2 -march=i486 -mtune=i686" \
./configure --enable-ipv6
#only sharedlib/utils
make shared||(echo 'Make failed!'&&exit 1)

#I miss the easy make install :,(
mkdir -p $PKG/usr/bin/
cp dnsget_s $PKG/usr/bin/dnsget
cp rblcheck_s $PKG/usr/bin/rblcheck
mkdir -p $PKG/usr/lib
cp libudns.so.0 $PKG/usr/lib/
cd $PKG/usr/lib
ln -s libudns.so.0 ./libudns.so
cd $TMP/$NAME-$VERSION
mkdir -p $PKG/usr/include
cp udns.h $PKG/usr/include/

#install doc & man
mkdir -p $PKG/usr/doc/$NAME-$VERSION/
cp NEWS NOTES TODO COPYING.LGPL $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/man/man3
cp udns.3 $PKG/usr/man/man3
gzip -9 $PKG/usr/man/man3/udns.3
mkdir -p $PKG/usr/man/man1
cp dnsget.1 $PKG/usr/man/man1
gzip -9 $PKG/usr/man/man1/dnsget.1
cp rblcheck.1 $PKG/usr/man/man1
gzip -9 $PKG/usr/man/man1/rblcheck.1

# Set permissions
chown -R root:root $PKG/usr/bin
chmod 755 $PKG/usr/bin/*
chown -R root:root $PKG/usr/doc
chmod -R 755 $PKG/usr/doc/*
find $PKG/usr/doc -type f -exec chmod 644 {} \;
chmod 755 $PKG/usr/lib/libudns.so.0
chmod 644 $PKG/usr/man/man3/udns.3.gz

# Copy Slackware package files
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cp $CWD/udns.SlackBuild $PKG/usr/doc/$NAME-$VERSION/udns.SlackBuild

# Create package
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
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz
echo "Package created in $TMP"

# Clean up
if [ "$1" = "--cleanup" ]; then
	echo "Cleaning up temp files"
	rm -rf $TMP/$NAME-$VERSION
	rm -rf $PKG
fi
