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

# fftw.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2017-2027  Patrick J. Volkerding, Sebeka, Minnesota, 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.
#
# Slackware build script for fftw
# Written by Kyle Guinn <elyk03@gmail.com>

# 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 xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract
slackhousekeeping

# Allow build to use CMake 4.x:
export CMAKE_POLICY_VERSION_MINIMUM=3.5

# Apply patches:
sed -i "s|-mtune=native|$SLKCFLAGS|" configure

echo "****************************"
echo "**** Building libfftw3 **** "
echo "****************************"
./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-openmp \
    $SIMD \
    --build=${SLK_ARCH_BUILD} || failconfig
make $NUMJOBS || make || failmake
make install-strip DESTDIR=$PKG || failinstall
make clean

echo "****************************"
echo "**** Building libfftw3f **** "
echo "****************************"
./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-openmp \
    --enable-float \
    $SIMD \
   --build=${SLK_ARCH_BUILD} || failconfig
make $NUMJOBS || make || failmake
make install-strip DESTDIR=$PKG || failinstall
make clean

echo "****************************"
echo "**** Building libfftw3l **** "
echo "****************************"
./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-openmp \
    --enable-long-double \
    --build=${SLK_ARCH_BUILD} || failconfig
make $NUMJOBS || make || failmake
make install-strip DESTDIR=$PKG || failinstall
make clean

#
# Not for ARM, as we don't have the quadmath library in gcc.
#    --enable-quad-precision \
#
echo "****************************"
echo "**** Building libfftw3q **** "
echo "****************************"
./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-openmp \
    --build=${SLK_ARCH_BUILD} || failconfig
make $NUMJOBS || make || failmake
make install-strip DESTDIR=$PKG || failinstall

# Generate the cmake files and install any that are missing:
# No quadmath lib in gcc for ARM
QUAD_PRECISION=OFF

cmake \
   -DCMAKE_C_FLAGS="$SLKCFLAGS" \
   -DCMAKE_CXX_FLAGS="$SLKCFLAGS" \
   -DCMAKE_INSTALL_PREFIX=/usr \
   -DLIB_SUFFIX="$LIBDIRSUFFIX" \
   -DDOC_INSTALL_DIR="doc" \
   -DMAN_INSTALL_DIR=/usr/man \
   -DENABLE_OPENMP=ON \
   -DENABLE_THREADS=ON \
   -DENABLE_FLOAT=ON \
   -DENABLE_LONG_DOUBLE=ON \
   -DENABLE_QUAD_PRECISION=${QUAD_PRECISION} \
   -DENABLE_SSE=ON \
   -DENABLE_SSE2=ON \
   -DENABLE_AVX=OFF \
   -DENABLE_AVX2=OFF \
   . || failconfig
if [ ! -d $PKG/usr/lib${LIBDIRSUFFIX}/cmake/fftw3 ]; then
  mkdir -vpm755 $PKG/usr/lib${LIBDIRSUFFIX}/cmake/fftw3
fi
for file in FFTW3*cmake ; do
  if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/cmake/fftw3/$file ]; then
    echo "Copying cmake file $file:"
    cp -fav $file $PKG/usr/lib${LIBDIRSUFFIX}/cmake/fftw3
  fi
done

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  AUTHORS CONVENTIONS COPY* INSTALL NEWS README* TODO doc/html \

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
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-$ARCH-$BUILD.txz

slackmp         # run makepkg -l y -c n

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