#!/bin/sh
## build glibc-$VERSION for Slackware
## by Patrick J. Volkerding <volkerdi@slackware.com>
CWD=`pwd`
export PKG=/tmp/package-glibc

if [ ! "`ls /var/log/packages/automake* 2> /dev/null`" = "" -o ! "`ls /var/log/packages/autoconf* 2> /dev/null`" = "" ]; then
  cat << EOF


Hey!!!!

Having autoconf and/or automake around for this is JUST ASKING FOR TROUBLE.
Unless you magically have the same autoconf/make as this glibc was developed with,
at least nss_db will fail.

EOF
sleep 4700
fi

VERSION=2.2.5

# Empty this first:
rm -rf $PKG
mkdir -p $PKG/lib
mkdir -p $PKG/sbin
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/lib
mkdir -p $PKG/usr/sbin
mkdir -p $PKG/usr/include
mkdir -p $PKG/usr/doc
mkdir -p $PKG/usr/man
mkdir -p $PKG/usr/share

cd /tmp
tar xzvf $CWD/glibc-$VERSION.tar.gz
cd glibc-$VERSION

tar xzvf $CWD/glibc-linuxthreads-$VERSION.tar.gz

# Patch a buffer overflow in DNS network lookups:
zcat $CWD/glibc-2.2.5.dns-network.diff.gz | patch -p1 --verbose --backup --suffix=.orig
# Patch an overflow in xdrmem_getbytes():
zcat $CWD/glibc-2.2.5-xdrmem.diff.gz | patch -p1 --verbose --backup --suffix=.orig

CFLAGS="-O2 -g -march=i386 -mcpu=i686" ./configure --prefix=/usr \
            --enable-add-ons=linuxthreads \
            --without-cvs \
            i386-slackware-linux

make
make install install_root=$PKG
make localedata/install-locales install_root=$PKG

mkdir -p $PKG/etc
cat nscd/nscd.conf > $PKG/etc/nscd.conf-sample

# Since GNU does not provide man pages, I put some together:
mkdir -p $PKG/usr/man/man1
cat $CWD/ldd.1.gz > $PKG/usr/man/man1/ldd.1.gz
# This is now in the man-pages package.
#mkdir -p $PKG/usr/man/man1
#cat $CWD/ldconfig.8.gz > $PKG/usr/man/man1/ldconfig.8.gz

# Not a bad idea to have this here as well:
( cd $PKG ; tar xzvf $CWD/timezone-scripts.tar.gz )

## Install docs:
( mkdir -p $PKG/usr/doc/glibc-$VERSION
  cp -a BUGS CONFORMANCE COPYING COPYING.LIB FAQ INSTALL INTERFACE \
    NAMESPACE NEWS NOTES PROJECTS README README.libm $PKG/usr/doc/glibc-$VERSION
  cd linuxthreads
  mkdir -p $PKG/usr/doc/glibc-$VERSION/linuxthreads
  cp -a Banner FAQ.html LICENSE README README.Xfree3.2 linuxthreads.texi \
    Examples $PKG/usr/doc/glibc-$VERSION/linuxthreads
  cd man
  mkdir $PKG/usr/man/man3
  for file in *.man ; do
    cat $file | gzip -9c > $PKG/usr/man/man3/`basename $file .man`.3.gz
  done 
  chown -R root.root $PKG/usr/doc/glibc-$VERSION 
  find $PKG/usr/doc/glibc-$VERSION -type d | xargs chmod 755 
  find $PKG/usr/doc/glibc-$VERSION -type f | xargs chmod 644 )

### If you want glibc-$VERSION installed, uncomment the lines below.
### NOTE:  This isn't rquired to make the package(s); we build into $TMP/package-glibc/
#make install
#make localedata/install-locales

# Don't forget to add the /usr/share/zoneinfo/localtime -> /etc/localtime symlink! :)
if [ ! -r $PKG/usr/share/zoneinfo/localtime ]; then
  ( cd $PKG/usr/share/zoneinfo ; ln -sf /etc/localtime . )
fi

echo "Building Berkeley DB libraries..."
sleep 5
cd $CWD
./bsd_db.build

echo "Building nss_db-2.2..."
sleep 5
cd $CWD
./nss_db.build
