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

VERSION=5.0
ARCH=i486
BUILD=4

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

cd $TMP
rm -rf coreutils-$VERSION
tar xjvf $CWD/coreutils-$VERSION.tar.bz2
cd coreutils-$VERSION
chown -R root.root .
# The GNU version still hasn't caught up with the features of the Slackware dircolors from long ago:
zcat $CWD/dircolors.c.gz > src/dircolors.c
zcat $CWD/dircolors.h.gz > src/dircolors.h
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
CFLAGS="-O2 -march=i486 -mcpu=i686" ./configure \
           --prefix=/usr \
           --sysconfdir=/etc \
           i486-slackware-linux
make
make install DESTDIR=$PKG
# Now, we do some cleanup:
cd $PKG
( cd usr/bin ; rm -rf [ )
( cd usr/bin ; ln -sf  test [ )
# Replace this manpage:
zcat $CWD/dircolors.1.gz > usr/man/man1/dircolors.1
rmdir usr/lib
# Strip binaries:
find . | xargs file | grep "ELF 32-bit LSB executable" | grep 80386 | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
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
# 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/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