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

# Temporary build location.  This should not be a directory
# path a non-root user could create later...
TMP=${TMP:-/glibc-tmp-`mcookie`}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi

VERSION=${VERSION:-2.5}
CVSVER=2.5
HEADERSTWOFOUR=2.4.33.4

# $ARCH may be preset, otherwise i486 compatibility with i686 binary
# structuring is the Slackware default now, since this is what gcc-3.2+
# requires for binary compatibility with previous releases.
ARCH=${ARCH:-i486}

# This should be i486 for all 32-bit x86 arch:
TARGET=${TARGET:-i486}

BUILD=${BUILD:-1}

# Since glibc is built twice (for linuxthreads and NPTL versions), it is
# probably a good idea to make a patch function to put all patches in the
# build script in a single location.
apply_patches() {
  # Use old-style locale directories rather than a single (and strangely
  # formatted) /usr/lib/locale/locale-archive file:
  zcat $CWD/glibc.locale.no-archive.diff.gz | patch -p1 --verbose || exit 1
  # Fix NIS netgroups:
  zcat $CWD/glibc.nis-netgroups.diff.gz | patch -p1 --verbose || exit 1
  # Evidently glibc never expected Linux kernel versions to be in the
  # format 1.2.3.4.  This patch makes glibc consider the kernel version
  # to be only the first three digit groups found, and drops any
  # trailing non-digit characters:
  zcat $CWD/glibc.kernelversion.diff.gz | patch -p1 --verbose || exit 1
  # Support ru_RU.CP1251 locale:
  zcat $CWD/glibc.ru_RU.CP1251.diff.gz | patch -p1 --verbose || exit 1
  # Support sa_IN locale:
  zcat $CWD/glibc.sa_IN.diff.gz | patch -p1 --verbose || exit 1
  # Fix missing MAX macro in getcwd.c:
  zcat $CWD/glibc.getcwd.max.macro.diff.gz | patch -p1 --verbose || exit 1
  if [ $CVSVER = 2.5 ]; then
    # Update the timezone information.
    ( cd timezone
      tar xzf $CWD/tzdata2006o.tar.gz
      chown root:root *
      mv yearistype.sh yearistype
      chmod 644 *
      chmod 755 yearistype
      mkdir tzcode
      cd tzcode
      tar xzf $CWD/tzcode2006o.tar.gz
      chown -R root:root .
      chmod 644 *
      cp -a *.c *.h ..
    )
  else
    exit 1
  fi
}

# I'll break this out as an option for fun  :-)
if [ "$ARCH" = "i386" ]; then
  OPTIMIZ="-O3 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  OPTIMIZ="-O3 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i586" ]; then
  OPTIMIZ="-O3 -march=i586"
elif [ "$ARCH" = "i686" ]; then
  OPTIMIZ="-O3 -march=i686"
elif [ "$ARCH" = "athlon" ]; then
  OPTIMIZ="-O3 -march=athlon"
elif [ "$ARCH" = "s390" ]; then
  OPTIMIZ="-O3"
elif [ "$ARCH" = "x86_64" ]; then
  OPTIMIZ="-O3"
else
  OPTIMIZ="-O3"
fi

# This is going to be the initial $DESTDIR:
export PKG=$TMP/package-glibc-incoming-tree

if [ -d $TMP/glibc-$VERSION ]; then
  rm -rf $TMP/glibc-$VERSION
fi

# Create an incoming directory structure for glibc to be built into:
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
mkdir -p $PKG/var/db/nscd
mkdir -p $PKG/var/run/nscd

cd $TMP
rm -rf $CWD/glibc-$CVSVER
tar xjvf $CWD/glibc-$CVSVER.tar.bz2
cd glibc-$CVSVER

tar xjf $CWD/glibc-libidn-$VERSION.tar.bz2
mv glibc-libidn-$VERSION libidn || exit 1
tar xjvf $CWD/glibc-linuxthreads-$VERSION.tar.bz2

chown -R root:root .

# Clean up leftover CVS directories:
find . -type d -name CVS -exec rm -r {} \;

# Apply patches; exit if any fail.
apply_patches
if [ ! $? = 0 ]; then
  exit 1
fi

# Make build directory:
mkdir build-glibc-$VERSION
cd build-glibc-$VERSION

## Start with the linuxthreads version, as it's what is compiled against by default (until
## such time as 2.4.x kernels are no longer in use).  Added "--disable-sanity-checks" since
## evidently supporting 2.4.x kernels is no longer "sane"...  <sigh>.
echo "BUILDING DAS LINUXTHREADS"
CFLAGS="-g $OPTIMIZ" \
../configure \
  --prefix=/usr \
  --enable-kernel=2.4.33 \
  --with-headers=/usr/src/linux-${HEADERSTWOFOUR}/include \
  --disable-sanity-checks \
  --enable-add-ons=libidn,linuxthreads \
  --with-tls \
  --without-__thread \
  --without-cvs \
  $TARGET-slackware-linux

make -j4 || exit 1
make install install_root=$PKG

cd $PKG || exit 1
rm -r etc sbin usr var
mkdir -p lib/obsolete/linuxthreads
mv lib*.* lib/obsolete/linuxthreads
mkdir install
cat slack-desc.glibc-solibs-linuxthreads > install/slack-desc
makepkg $PKG/glibc-solibs-linuxthreads-$ARCH-$BUILD.tgz