#!/bin/sh
# Set initial variables:
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-coreutils
VERSION=5.2.1
ARCH=alpha
BUILD=1
TARGET=$ARCH-alphaslack-linux

mkdir -p $PKG
( cd $TMP
rm -rf coreutils-$VERSION
tar xjf $CWD/coreutils-$VERSION.tar.bz2
cd coreutils-$VERSION
chown -R root.root .
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   $TARGET
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
)
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
# 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
cd $PKG
makepkg -l y -c n $TMP/coreutils-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/coreutils.build.log
