#!/bin/bash

# coreutils.SlackBuild
# 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/$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=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.*z
cd $PKGNAM-* || exit 1
slackhousekeeping

# Patch uname to correctly display CPU information:
zcat $CWD/$PKGNAM.uname.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
DEFAULT_POSIX2_VERSION=199209 \
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --bindir=/bin \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --without-gmp \
   --infodir=/usr/info \
   --sysconfdir=/etc \
   --without-gmp \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package directory:
make install DESTDIR=$PKG

# We have had the mktemp from debianutils included with Slackware for quite a
# long time, and certain options are changed here, like changing -u to mean a
# dry-run rather than to unlink the tempfile when finished.  Since this could
# break existing scripts, unless someone can tell me a good reason why we
# should start using a new version of mktemp, we will continue to use the
# one we've been using.  If the new one starts to become expected, let me know.
# We'll figure out what the best options are and go from there.
mv $PKG/bin/mktemp $PKG/bin/mktemp-gnu
mv $PKG/usr/man/man1/mktemp.1 $PKG/usr/man/man1/mktemp-gnu.1

# This seems wrong, and it stomps on files in the ksh93 package, though I'm
# not sure the placement of those is correct, either...  The ksh93 package
# installs them as flat text files, while coreutils installs empty directories
# Oh well, this is what we've done for years, and nobody's complained...
rm -rf $PKG/usr/share/locale/*/LC_TIME

# 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.  :-)
mkdir -p $PKG/bin $PKG/usr/bin
( cd $PKG/usr/bin
  for file in ../../bin/* ; do
    ln --verbose -sf $file .
  done
)

# Add some defaults, although a very slack-like set of default options are built
# into /bin/ls now anyway:
mkdir -p $PKG/etc
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:
mkdir -p $PKG/etc/profile.d
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:
for dupe in hostname kill su uptime ; do
  rm -f $PKG/bin/${dupe} $PKG/usr/bin/${dupe} \
    $PKG/usr/sbin/${dupe} $PKG/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 ginstall.1 )

# Copy 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
changelogliposuction ChangeLog $PKGNAM $VERSION

# 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
