#!/bin/bash

# coreutils.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Patch in the uname for Linux enhancements and contains x86 assembler code.
# It provides no other non-x86 arch improvements.
#zcat $CWD/coreutils.uname.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch

# Configure:
CFLAGS="$SLKCFLAGS" \
DEFAULT_POSIX2_VERSION=199209 \
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --bindir=/bin \
   --sysconfdir=/etc \
   $ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Install into package directory:
make install DESTDIR=$PKG
mkdir -p $PKG/etc/profile.d
mkdir -p $PKG/bin $PKG/usr/{sbin,bin}

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a ABOUT-NLS AUTHORS COPYING NEWS README THANKS THANKS-to-translators TODO \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Move info and man pages into Slackware locations:
mv $PKG/usr/share/{info,man} $PKG/usr || exit

# These are important enough that they should probably all go into /bin at this
# point...   Having some of them unavailable when /usr isn't mounted is just a
# source of unending bug reports for various third party applications.
# Time to end those reports.  :-)
( cd $PKG/usr/bin
  for file in ../../bin/* ; do
    ln  --verbose -sf $file .
  done )

# Tidy up:
rm -f $PKG/usr/info/dir

# Add some defaults, although a very slack-like set of default options are built
# into /bin/ls now anyway:
zcat $CWD/DIR_COLORS.gz > $PKG/etc/DIR_COLORS.new

# Since dircolors no longer provides any default aliases these scripts
# will be needed for ls to act as expected:
zcat $CWD/coreutils-dircolors.csh.gz > $PKG/etc/profile.d/coreutils-dircolors.csh
zcat $CWD/coreutils-dircolors.sh.gz > $PKG/etc/profile.d/coreutils-dircolors.sh
chmod 755 $PKG/etc/profile.d/*

# Remove things that are provided by other Slackware packages:
( cd $PKG
  for dupe in hostname kill su uptime ; do
     rm -f bin/${dupe} usr/bin/${dupe} usr/sbin/${dupe} usr/man/man?/${dupe}.*
  done )

# Add ginstall links (there's still a lot of stuff that needs this to compile):
( cd bin ; ln -sf install ginstall )
( cd usr/bin ; ln -sf ../../bin/ginstall ginstall )
( cd usr/man/man1 ; ln -sf install.1.gz ginstall.1.gz )

# Set generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
