#!/bin/bash
#
# SlackBuild for rpy2
#
# Copyright 2020-2026 Rob van Nues (sborg63@disroot.org)
#
# 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=rpy2
VERSION=${VERSION:-3.6.9}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
SRCREF=RELEASE
SRCDELIM="_"
SRCNAM=${SRCNAM:-3_6_9}
SRCVERSION=$SRCREF$SRCDELIM$SRCNAM
MODULE1=${MODULE1:-rinterface}
MODULE2=${MODULE2:-robjects}
PKGTYPE=${PKGTYPE:-tgz}

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

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

if [ "$ARCH" = "i586" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
else
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
#tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
tar xvf $CWD/$PRGNAM-$SRCVERSION.tar.gz
#cd $PRGNAM-$VERSION
cd $PRGNAM-$SRCVERSION
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 {} + \) \)

# Make sure there is R
if ! [ -x "$(command -v R)" ]; then
  echo 'Error: R is not installed.' >&2
  exit 1
fi

echo -e "\n# rpy2_SlackBuild messages:"
# R should have been compiled with
# --enable-R-shlib
# --enable-BLAS-shlib
# check for these libraries
RHOMLIB="$(R RHOME)/lib"
echo "Found R home: ${RHOMLIB%/lib} with shared libraries:"
for i in $RHOMLIB/*; do echo "  ${i#RHOMLIB}"; done
if ! [ "$(readelf -h $RHOMLIB/libR*.so | grep DYN)" ]; then
  echo "Error: R is not compiled with shared libraries." >&2
  exit 1
fi

# check where we build, on Slackware stable or on current (stable+)
# support for python-3.9 will be dropped https://github.com/rpy2/rpy2/pull/1257
OSVERSION="$(cat /etc/os-release | grep "VERSION_CODENAME" | cut -c 18-)"
PYVER=$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')

if [[ $OSVERSION == "current" ]]; then
  echo "Installing on current, into python$PYVER/site-packages/ "
elif [[ $OSVERSION == "stable" ]]; then
  echo "Installing on stable, into python$PYVER/site-packages/ "
  export PYTHONPATH=/opt/python$PYVER/site-packages/
else 
  echo "Stopping; $OSVERSION is not recognized as 'stable' or 'current', sorry." 
  exit 1
fi

# this removes an error message from setup.py
# https://github.com/rpy2/rpy2/issues/675
export LD_LIBRARY_PATH="${RHOMLIB}:${LD_LIBRARY_PATH}"
echo "LD_LIBRARY_PATH set to ${LD_LIBRARY_PATH}"
echo -e "\n# Python build messages:"

cd $PRGNAM-$MODULE1
python3 -m build --no-isolation
python3 -m installer -d "$PKG" dist/*.whl

cd ../$PRGNAM-$MODULE2
python3 -m build --no-isolation
python3 -m installer -d "$PKG" dist/*.whl

cd ..

# Indicate change from python build to slackbuild output for debugging purposes
echo -e "\n# rpy2.SlackBuild messages:"

# Strip debug symbols from ELF binaries
for file in $(find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF | cut -f 1 -d : 2> /dev/null) ; do
  if [ -e $file ]; then
  echo "ELF binary: ${file#$PKG}"
    strip --strip-unneeded $file 2> /dev/null || true ;
    echo "  '--strip-unneeded'"
  fi
#  and remove (hardcoded) rpaths ('ld.so' will resolve paths to linked binaries instead):
  if [ ! "$(patchelf --print-rpath $file 2> /dev/null)" = "" ]; then
    patchelf --remove-rpath $file
    echo "  '--remove-rpath'"
  fi
done

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
mkdir -p $PKGDOC/$PRGNAM-$MODULE1
mkdir -p $PKGDOC/$PRGNAM-$MODULE2
cp -a \
  AUTHORS gpl-2.0.txt NEWS README* \
  $PKGDOC
cp  $PRGNAM-$MODULE1/README* $PKGDOC/$PRGNAM-$MODULE1
cp  $PRGNAM-$MODULE2/README* $PKGDOC/$PRGNAM-$MODULE2
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

PKGSHR=$$PKG/usr/share/$PRGNAM-$VERSION/scripts
mkdir -p $PKGSHR
cp -a scripts/* $PKGSHR

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

# To facilitate dynamic linking with R,
# libR.so and libRblas.so still need to be found
# https://github.com/rpy2/rpy2/issues/675
#
# For this, create "/etc/ld.so.conf.d/Rlibs.conf" with the line
# "/usr/lib${LIBDIRSUFFIX}/R/lib" via doinst.sh.
#
# Directly cat to package to bypass no-write permission on $CWD
# (thanks to cwilling).
cat > $PKG/install/doinst.sh  <<EOF
echo "/usr/lib${LIBDIRSUFFIX}/R/lib" > etc/ld.so.conf.d/Rlibs.conf
sbin/ldconfig
EOF
cat > $PKG/install/douninst.sh <<EOF
rm etc/ld.so.conf.d/Rlibs.conf
sbin/ldconfig
EOF

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