#!/bin/bash

# Slackware build script for PLUMED

# Copyright 2026 Giancarlo Dessi, Cagliari, IT
# Copyright 2022 William PC - Seattle, 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.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=PLUMED
VERSION=${VERSION:-2.10.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
SRCNAM=plumed2

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

# Detect an installation of opempi or mpich
# if found the parallel mode will be enabled
# by default using openmpi or mpich. 
# Otherwise pass MPI=no to the script

# First, detect if mpich or openmpi/openmpi4 is
# installed. This can be skipped passing MPI=no
# to the script
if pkg-config --exists ompi;  then
 mpi="" ; [ "${MPI:-yes}" = "yes" ] && mpi="mpi"
elif pkg-config --exists mpich;  then
 mpi="" ; [ "${MPI:-yes}" = "yes" ] && mpi="mpi"
fi

# Then, verify if the system openmp will be used
# instead a MPI installation or added
# By default, without a MPI installation (or disabled)
# the build will use the system openmp, otherwise will
# be disabled. Passing MP=yes to the script openmp will
# added to the MPI installation
if [ "$mpi" = "" ]; then
 mpi="" ; [ "${MPI:-no}" = "no" ] && mpi="openmp"
else
 mpi=$mpi; [ "${MP:-no}" = "yes" ] && mpi="openmp "$mpi
fi

# Finally, set the configure flags depending on the 
# conditions above
if [ "$mpi" = "mpi" ]; then
 pmode="--enable-mpi --disable-openmp "
elif [ "$mpi" = "openmp" ]; then
 pmode="--disable-mpi --enable-openmp "
elif [ "$mpi" = "openmp mpi" ]; then
 pmode="--enable-mpi --enable-openmp "
else 
 pmode="--disable-mpi --disable-openmp "
fi

# The build of Python is enabled by default if the requirements
# are found in the system. It can be disabled passing PYTHON=no
# to the script
if [ -f "/usr/bin/f2py" ] && [ -f "/usr/bin/pybind11-config" ]; then
  python="" ; [ "${PYTHON:-yes}" = "yes" ] && python=" --enable-python "
fi 

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" -o "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=$ARCH -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz --no-same-owner
cp $CWD/PLUMED.sh $SRCNAM-$VERSION
cd $SRCNAM-$VERSION

# remove symlink to avoid error at find
# File system loop detected.
rm src/include/plumed

find . ! -type l    -a \( \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \) \)
  
sed -i -e "s|usr/lib|usr/lib$LIBDIRSUFFIX|g" PLUMED.sh

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  CPPFLAGS="-I/usr/include" \
  LDFLAGS="-L/usr/lib${LIBDIRSUFFIX}" \
  LIBS="-lopenblas" \
  --prefix=/usr \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --htmldir=/usr/doc/$PRGNAM-$VERSION \
  --pdfdir=/usr/doc/$PRGNAM-$VERSION \
  --enable-shared \
  --enable-fftw \
  $pmode \
  $python \
  --build=$ARCH-slackware-linux
make
make -C vim
make install DESTDIR=$PKG

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true
  
mkdir -p $PKG/etc/profile.d
cp -a PLUMED.sh $PKG/etc/profile.d/PLUMED.sh
chmod 755 $PKG/etc/profile.d/PLUMED.sh

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a \
  COPYING.LESSER README.md PEOPLE VERSION.txt \
  $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

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

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
