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

VERSION=5.2.1
ARCH=i486
BUILD=1

# 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
chown -R root.root .
CFLAGS=-O2 ./configure \
           --prefix=/usr \
           --sysconfdir=/etc \
           $ARCH-slackware-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