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

VERSION=4.1
ARCH=i386
BUILD=2

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_fileutils.tar.gz

echo "+===============+"
echo "| fileutils-$VERSION |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/fileutils-$VERSION.tar.gz
cd fileutils-$VERSION
chown -R root.root .
mkdir -p $PKG/usr/doc/fileutils-$VERSION
cp -a ABOUT-NLS AUTHORS COPYING INSTALL NEWS README THANKS TODO \
  $PKG/usr/doc/fileutils-$VERSION
# We're adding back the Slackware-color-ls version of dircolors, since the
# GNU version is missing some nice options.
> src/dircolors.c
> src/dircolors.c.orig
zcat $CWD/fileutils.dircolors.diff.gz | patch -p0 --verbose
zcat $CWD/fileutils.man.diff.gz | patch -p1 --verbose
# IMHO, this doesn't fix a security problem so much as it helps protect
# against idiotic usage of rm.  But, I'm sure there are still many more
# ways to use tools insecurely in public directories.
zcat $CWD/fileutils-4.1.rm.diff.gz | patch -p1 --verbose
CFLAGS="-O2 -march=i386 -mcpu=i686" ./configure \
           --prefix=/usr \
           --sysconfdir=/etc \
           i386-slackware-linux
make
cd src
# We have sln for this...
#rm ln
#make ln LDFLAGS="-static -s"
strip chgrp chmod chown cp dd df dircolors du ginstall ln ls \
  mkdir mkfifo mknod mv rm rmdir shred sync touch
cat chgrp > $PKG/bin/chgrp
cat chmod > $PKG/bin/chmod
cat chown > $PKG/bin/chown
cat cp > $PKG/bin/cp
cat dd > $PKG/bin/dd
cat df > $PKG/bin/df
cat dircolors > $PKG/bin/dircolors
cat du > $PKG/bin/du
cat ginstall > $PKG/usr/bin/ginstall
cat ln > $PKG/bin/ln
cat ls > $PKG/bin/ls
cat mkdir > $PKG/bin/mkdir
cat mkfifo > $PKG/bin/mkfifo
cat mknod > $PKG/bin/mknod
cat mv > $PKG/bin/mv
cat rm > $PKG/bin/rm
cat rmdir > $PKG/bin/rmdir
cat shred > $PKG/bin/shred
cat sync > $PKG/bin/sync
cat touch > $PKG/bin/touch
## The included manpages are purposely lame to "promote" info, the one true
## documentation system.  Well, info is ok, but there's no good reason to
## cripple good manpages, so let's put them back:
#( cd .. ; tar xzvf $CWD/fileutils.man.tar.gz )
# Use only Slackware dircolors.1 page.
( cd .. ; tar xzvf $CWD/fileutils.man.tar.gz man/dircolors.1 )
cd ../man
for page in chgrp.1 chmod.1 chown.1 cp.1 dd.1 df.1 dircolors.1 du.1 \
ginstall.1 ln.1 ls.1 mkdir.1 mkfifo.1 mknod.1 mv.1 rm.1 rmdir.1 touch.1 \
shred.1 sync.1 ; do
  ls -l $page
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cd ../doc
mkdir -p $PKG/usr/info
cat fileutils.info | gzip -9c > $PKG/usr/info/fileutils.info.gz
cd ../po
for locale in *.gmo ; do
  mkdir -p $PKG/usr/share/locale/`basename $locale .gmo`/LC_MESSAGES
  cat $locale > $PKG/usr/share/locale/`basename $locale .gmo`/LC_MESSAGES/fileutils.mo
done
zcat $CWD/DIR_COLORS.gz > $PKG/etc/DIR_COLORS
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/fileutils-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/fileutils-$VERSION
  rm -rf $PKG
fi