#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# ispell.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for ARMedslack.
#
# Copyright 2002-2009  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
#tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Apply patches:
cat $CWD/local.h.linux.diff | patch -p1 --verbose || exit 1
sed -i -e "s#/usr/lib/#/usr/lib${LIBDIRSUFFIX}/#" local.h.linux
cp -a local.h.linux local.h

# Apply FTBFS patches from Debian:
#tar xvvf $PORTCWD/sources/*debian* || exit 1
#for i in \
#  0006-Fix-sq-and-unsq.patch \
#  0008-Tex-backslash.patch \
#  0009-Fix-FTBFS-on-glibc.patch \
#  0011-Missing-prototypes.patch \
#  0013-Fix-man-pages.patch \
#  0015-CFLAGS-from-environment.patch \
#  0020-Mark-Rcs_Id-as-unused.patch \
#  0021-Fix-gcc-warnings.patch \
#  0022-Ispell-man-page.patch \
#  0023-Exclusive-options.patch \
#  0028-Fix-hardening-warnings.patch \
#  0029-Generate-hex-in-fix8bit.patch \
#  0030-Display-whole-multibyte-character.patch \
#  0031-Initialize-table.patch \
#  0032-Check-munchlist-errors.patch \
#  0033-Fix-NULL-pointer-dereference.patch \
#  ; do
#  auto_apply_patch debian/patches/$i || exit 1
#done

# Fix man locations:
grep -Flr -- '/usr/local/man/' . | xargs sed -i 's?/usr/local/man/?/usr/man/?g'

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
sleep 2 # to make absolutely sure that config will have a later timestamp
        # than local.h and will not get rebuilt by `make install`
sed -i -e "s#/usr#$PKG&#g" config.sh
mkdir -p $PKG/usr/{bin,lib${LIBDIRSUFFIX},man}
make install

# Get rid of /tmp/package-ispell path in munchlist:
#sed -i "s|/tmp/package-ispell||g" $PKG/usr/bin/munchlist
sed -i 's?'"$PKG"'?/?g' $PKG/usr/bin/munchlist

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

# Symlink to ispell.1 manpage for included utilities.
( cd $PKG/usr/man/man1
  # Don't include sq.1 or unsq.1 (unshipped internal utilities).
  rm -f sq.1 unsq.1
  for manpage in buildhash munchlist findaffix tryaffix icombine ijoin ; do
    rm -f ${manpage}.1
    ln -vsf ispell.1 ${manpage}.1
  done )

# Add documentation to the package:
mkdir -p $PKG/usr/doc/ispell-$VERSION
cp -a \
  Contributors Magiclines README WISHES \
  $PKG/usr/doc/ispell-$VERSION
chown -R root:root $PKG/usr/doc/ispell-$VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R 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
