#!/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/a/$PKGNAM
export PORTCWD=$PWD

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

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$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 \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --sysconfdir=/etc \
   --build=$ARCH-slackware-linux || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package directory:
make install DESTDIR=$PKG
mkdir -p $PKG/etc/profile.d
mkdir -p $PKG/bin $PKG/usr/{sbin,bin}
# This seems wrong, and it stomps on files in the ksh93 package.  Though, I'm
# not sure the placement of those is correct, either...
( cd $PKG/usr/share/locale
  rm -rf */LC_TIME )
mv $PKG/bin/mktemp $PKG/bin/mktemp-gnu
( cd $PKG/usr/bin ; ln -vsf ../../bin/mktemp-gnu mktemp-gnu )
mv -fv $PKG/usr/man/man1/mktemp.1 $PKG/usr/man/man1/mktemp-gnu.1

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

# 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 $PKG/bin          ; ln -sf install ginstall )
( cd $PKG/usr/bin      ; ln -sf ../../bin/ginstall ginstall )
( cd $PKG/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
slackmp         # run makepkg -l y -c n

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