#!/bin/sh
set -e

# because of weird tail/head behaviour
export _POSIX2_VERSION=199209

CWD=`pwd`

TMP=${TMP:-/tmp}
PKG=$TMP/package-ispell

ARCH=${ARCH:-sparc}
VERSION=3.2.06
BUILD=${BUILD:-0}

cd $TMP
rm -rf $PKG
rm -rf ispell-$VERSION
tar xzvf $CWD/ispell-$VERSION.tar.gz
cd ispell-$VERSION
zcat $CWD/ispell.diff.gz | patch -p1 --verbose

find . -type f -name "Makefile" -exec chmod 644 {} \;

# $DESTDIR support
zcat $CWD/ispell.Makefiles.DESTDIR.diff.gz | patch -p1

make
make install DESTDIR=$PKG

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

( cd $PKG/usr/lib/ispell
  ln -sf americanmed.hash american.hash
  ln -sf americanmed.hash english.hash
)

mkdir -p $PKG/usr/doc/ispell-$VERSION
cp -a Contributors Magiclines README WISHES \
  $PKG/usr/doc/ispell-$VERSION
chmod 644 $PKG/usr/doc/ispell-$VERSION/*

chown -R root.root $PKG

# packaging standards
chgrp -R bin $PKG/usr/bin
gzip -9 $PKG/usr/man/man?/*.?

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n $TMP/ispell-$VERSION-$ARCH-$BUILD.tgz

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