#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-findutils
VERSION=4.4.2
ARCH=alpha
BUILD=1
TARGET=$ARCH-alphaslack-linux

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf findutils-$VERSION
tar xzf $CWD/findutils-$VERSION.tar.gz
cd findutils-$VERSION
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
# We use slocate instead of these:
zcat $CWD/findutils.nolocate.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/findutils.no.default.options.warnings.diff.gz | patch -p1 --verbose || exit 1
./configure \
 --prefix=/usr \
 --mandir=/usr/man \
 --infodir=/usr/info \
 --build=$TARGET
make || exit
make install DESTDIR=$PKG

find $PKG | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find $PKG | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

mkdir -p $PKG/usr/doc/findutils-$VERSION
cp -a ABOUT-NLS AUTHORS COPYING NEWS README THANKS TODO $PKG/usr/doc/findutils-$VERSION
# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi
# Compress info pages and purge "dir" file from the package:
if [ -d $PKG/usr/info ]; then
  ( cd $PKG/usr/info
    rm -f dir
    gzip -9 *
  )
fi
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n $TMP/findutils-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/findutils.build.log
