#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://developer.apple.com/opensource/internet/bonjour.html
# Packagers Andrea Sciucca Gohanz ( gohanz@infinito.it)
# http://www.slacky.it

CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp/pkg
fi

PKG=$TMP/package-mDNSResponder
NAME=mdnsresponder
VERSION=107.6
ARCH=${ARCH:-i686}
BUILD=1as

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

cd $TMP
tar xvzf $CWD/mDNSResponder-107.6.tar.gz

echo "+------------------------------------------+"
echo "| Starting SlackBuild  $NAME-$VERSION |"
echo "+------------------------------------------+"

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

cd mDNSResponder-107.6

mkdir -p $PKG/lib
mkdir -p $PKG/usr/sbin
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/include
mkdir -p $PKG/usr/lib
mkdir -p $PKG/usr/man/man5
mkdir -p $PKG/usr/man/man8
mkdir -p $PKG/etc/rc.d

( cd mDNSPosix
make os=linux
             ( cd build/prod
               cp -a libnss*    $PKG/lib
               cp -a libdns*    $PKG/usr/lib
               cp -a mDNS*   $PKG/usr/bin
               cp -a dnsextd $PKG/usr/sbin
               cp -a mdnsd   $PKG/usr/sbin
              )

cp *.5        $PKG/usr/man/man5
cp *.8        $PKG/usr/man/man8
cp -a nss_mdns.conf $PKG/etc
install -m 755 mdnsd.sh $PKG/etc/rc.d/rc.mdns
)

( cd mDNSShared
  cp dns_sd.h  $PKG/usr/include
)

( 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
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a README.txt $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh  > $PKG/install/doinst.sh
cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION

gzip -9 $PKG/usr/man/*/*

cd $PKG
chown -R root:root .
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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