#!/bin/bash

# fftw.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for ARMedslack.

# 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-$VERSION || exit 1
slackhousekeeping

if [ "${PORTABLE:-yes}" = "no" ]; then
    # According to doc/fftw3.pdf the configure script should choose the best
    # value for $CFLAGS.  These variables must be unset so that the configure
    # script will decide.

    # If you are interested in further optimizations such as alternative
    # multithreading and support for other processors, check this page for
    # details:
    # http://www.fftw.org/fftw3_doc/Installation-on-Unix.html
    unset CFLAGS
    unset CXXFLAGS
    unset FFLAGS
    PKGARCH="custom"
    do_portable=""
else
    PKGARCH=$ARCH
    do_portable="--enable-portable-binary"
fi

if [ "${SSE:-no}" = "no" ]; then
    do_sse=""
else
    do_sse="--enable-sse"
fi

if [ "${SSE2:-no}" = "no" ]; then
    do_sse2=""
else
    do_sse2="--enable-sse2"
fi

# compile libfftw3
./configure \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --mandir=/usr/man \
    --infodir=/usr/info \
    --docdir=/usr/doc/fftw-$VERSION \
    --enable-shared \
    --disable-static \
    --enable-threads \
    $do_sse2 \
    $do_portable || exit 1

make $NUMJOBS || exit 1
make install-strip DESTDIR=$PKG || exit 1

# compile libfftw3f
./configure \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --mandir=/usr/man \
    --infodir=/usr/info \
    --docdir=/usr/doc/fftw-$VERSION \
    --enable-shared \
    --disable-static \
    --enable-threads \
    --enable-float \
    $do_sse \
    $do_portable || exit 1
make $NUMJOBS || exit 1
make install-strip DESTDIR=$PKG || exit 1

# compile libfftw3l
./configure \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --mandir=/usr/man \
    --infodir=/usr/info \
    --docdir=/usr/doc/fftw-$VERSION \
    --enable-shared \
    --disable-static \
    --enable-threads \
    --enable-long-double \
    $do_portable || exit 1
make $NUMJOBS || exit 1
make install-strip DESTDIR=$PKG || exit 1

DOCS="AUTHORS CONVENTIONS COPY* INSTALL NEWS README* TODO doc/html"
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PKGNAM-$VERSION
cat $CWD/$PKGNAM.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.SlackBuild

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply 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      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Replace version number with a _ so it doesn't get confused with
# the package name.
export VERSION="$( echo $VERSION | sed 's?-?_?g' )"
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.tgz
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links



