#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-coreutils

VERSION=5.2.1
ARCH=sparc
BUILD=0a

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "sparc" ]; then
  SLKCFLAGS="-O2"
fi

# Clean build locations:
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf coreutils-$VERSION
tar xjvf $CWD/coreutils-$VERSION.tar.bz2
cd coreutils-$VERSION

# The optional uname fix diff is included in this distribution.
# You'll have to enable it yourself by uncommenting out the line
# below and rebuilding the package.  Splack doesn't do this by
# default since Pat's packages aren't patched this way either.
# Patch taken from the Slamd64 project.  --jmd
#zcat $CWD/patches-splack/unamefix.diff.gz | patch -p1 --verbose

chown -R root.root .

# Compilation with glibc version later than 2.3.2 needs the environment
# variable DEFAULT_POSIX2_VERSION set to 199209.
# Without the next line, the coreutils will start complaining about 'obsolete'
# command switches, like "tail -20" will be considered obsolete.
# This behaviour breaks many other packages... the 'obsolete' parameters are
# too commonly used to disregard them.  Better to stick with the older more
# widely accepted standards until things begin to demand the new way.
#
# Edit 24-March-2006:  Splack was previously built without the 'obsolete'
# support, instead opting to patch individual packages.  This has proven
# far too difficult to keep up with over the last few months, so we are
# now falling back to using the old options.
CFLAGS="$SLKCFLAGS" \
  DEFAULT_POSIX2_VERSION=199209 \
  ./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  $ARCH-splack-linux

make
make install DESTDIR=$PKG

# The GNU version still hasn't caught up with the features of the Slackware dircolors from long ago,
# so we will replace it with the version Slackware has long used:
zcat $CWD/dircolors.c.gz > src/dircolors.c
zcat $CWD/dircolors.h.gz > src/dircolors.h
( cd src ; make )
cat src/dircolors > $PKG/usr/bin/dircolors
mkdir -p $PKG/usr/doc/coreutils-$VERSION $PKG/usr/sbin
cp -a \
  ABOUT-NLS AUTHORS COPYING NEWS README THANKS THANKS-to-translators TODO \
  $PKG/usr/doc/coreutils-$VERSION
# Now, we do some cleanup:
cd $PKG
# Replace this manpage:
zcat $CWD/dircolors.1.gz > usr/man/man1/dircolors.1
# Strip binaries:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/usr/sbin
chown -R root.bin usr/bin usr/sbin
# Remove things that are provided by other Slackware packages:
for dupe in hostname kill su uptime ; do
  rm -f usr/bin/${dupe} usr/sbin/${dupe} usr/man/man?/${dupe}.*
done
# These things have always been in /bin on Linux:
mkdir -p bin
for move in cat chgrp chmod chown cp cut date dd df dircolors du echo false head ln ls mkdir mkfifo mknod mv pwd rm rmdir shred sleep stty sync touch true uname ; do
  mv usr/bin/${move} bin
  ln -sf ../../bin/${move} usr/bin/${move}
done
chown -R root.bin bin
# Add a chroot link in case any scripts use the historic location:
ln -sf ../bin/chroot usr/sbin/chroot
rm -f usr/info/dir
gzip -9 usr/info/*
gzip -9 usr/man/man?/*.?
mkdir -p etc
zcat $CWD/DIR_COLORS.gz > etc/DIR_COLORS.new
# I changed my mind on this... why complicate things?  It's not much stuff in /etc/profile.
#mkdir -p etc/profile.d
#zcat $CWD/coreutils-color-ls.sh.gz > etc/profile.d/coreutils-color-ls.sh
#zcat $CWD/coreutils-color-ls.csh.gz > etc/profile.d/coreutils-color-ls.csh
#chmod 755 etc/profile.d/*
# Add ginstall links:
( cd usr/bin ; ln -sf install ginstall )
( cd usr/man/man1 ; ln -sf install.1.gz ginstall.1.gz )
mkdir -p install
cat $CWD/doinst.sh > install/doinst.sh
cat $CWD/slack-desc > install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/coreutils-$VERSION-$ARCH-$BUILD.tgz

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