#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-bind
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_bind.tar.gz

echo "+================+"
echo "| bind-4.9.7-T1B |"
echo "+================+"
cd $TMP
mkdir bind-4.9.7
cd bind-4.9.7
tar xzvf $CWD/bind-4.9.7-T1B.tar.gz
zcat $CWD/bind-4.9.7-T1B.diff.gz | patch
mkdir -p $PKG/usr/doc/bind-4.9.7-T1B
cp -a README TODO OPTIONS INSTALL CHANGES $PKG/usr/doc/bind-4.9.7-T1B
cp -a doc $PKG/usr/doc/bind-4.9.7-T1B
cp -a conf/master $PKG/usr/doc/bind-4.9.7-T1B/etc-examples
chown -R root.root $PKG/usr/doc/bind-4.9.7-T1B
find $PKG/usr/doc/bind-4.9.7-T1B -type f -exec chmod 644 {} \;
# These RFCs take up a _lot_ of room, and are easy to find on the net if
# need be.  (IMHO, feel free to correct me :)
rm -rf $PKG/usr/doc/bind-4.9.7-T1B/doc/rfc
# Fake this header file... bind thinks we're compiling on a Sun :^)
#mkdir include/rpc
#touch include/rpc/netdb.h
make
cd named
strip named named-xfer
cat named > $PKG/usr/sbin/named
cat named-xfer > $PKG/usr/sbin/named-xfer
cat named.reload > $PKG/usr/sbin/named.reload
cat named.restart > $PKG/usr/sbin/named.restart
cat ndc > $PKG/usr/sbin/ndc
cd ../tools
strip dig dnsquery host 
cat dig > $PKG/usr/bin/dig
cat dnsquery > $PKG/usr/bin/dnsquery
cat host > $PKG/usr/bin/host
cd nslookup
strip nslookup
cat nslookup > $PKG/usr/bin/nslookup
cat nslookup.help > $PKG/usr/lib/nslookup.help
cd ../../man
for page in *.out1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/`basename $page .out1`.1.gz
done
for page in *.out3 ; do
  cat $page | gzip -9c > $PKG/usr/man/man3/`basename $page .out3`.3.gz
done
cat resolver.out5 | gzip -9c > $PKG/usr/man/man5/resolver.5.gz
for page in *.out7 ; do
  cat $page | gzip -9c > $PKG/usr/man/man7/`basename $page .out7`.7.gz
done
for page in *.out8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/`basename $page .out8`.8.gz
done

# Build the package:
cd $PKG
tar czvf $TMP/bind.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/bind-4.9.7-T1B
  rm -rf $PKG
fi
