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

# clisp.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 19-Jul-2004
#
# Copyright 2008-2025  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

ulimit -s unlimited

# 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

# Unset them from the defaults set by the slackdev script.
# With the soft float port, clisp was broken for a long time and the whole
# lot was quite fragile.  If in the hard float port we can set the CFLAGS then
# we can try at a later date once we get clisp built.
unset SLKCFLAGS

# Determine the CFLAGS for the known architectures:
#case $ARCH in
#   arm)     export SLKCFLAGS="$SLKCFLAGS -marm -falign-functions=4"
#            ;;
#   *)       export SLKCFLAGS="-O0"
#esac

# Versions of bundled packages:
# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull the version numbers from the x86 trunk:
slack_setvarfromupstream FFCALLVER

######################### Build ffcall ####################################
function build_ffcall() {
# Build ffcall - needed for --dynamic-ffi with clisp.
cd $TMPBUILD

tar xf $CWD/libffcall-$FFCALLVER.tar.!(*sign|*asc|*sig)
cd libffcall*/ || failextract
slackhousekeeping

# Remove prebuilt object files:
find . -name "*.o" | xargs rm -f

# GCC 14 "fix":
SLKCFLAGS="$SLKCFLAGS -Wno-error=incompatible-pointer-types -Wno-error=int-conversion"

# Configure:
CFLAGS="$SLKCFLAGS -fPIC -DMAP_VARIABLE=2" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --enable-static=yes \
   --enable-shared=no \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Install in to package:
make install DESTDIR=$PKG || failinstall

# We need libffcall installed on the running OS in order to build clisp:
make install || failinstall

# Don't need this:
#rm -fv $PKG/usr/lib${LIBDIRSUFFIX}/*.la
# Wipe the ones that got installed to the OS as well as $PKG:
pushd $PKG
find . -name '*.la' | while read lafile ; do rm -fv ${lafile} /${lafile} ; done
popd

# Move docs around:
mkdir -pm755 $PKG/usr/doc/clisp-$VERSION/libffcall-$FFCALLVER
install -vpm644 README NEWS COPYING \
                $PKG/usr/doc/clisp-$VERSION/libffcall-$FFCALLVER
mv -f $PKG/usr/share/html/* $PKG/usr/doc/clisp-$VERSION/libffcall-$FFCALLVER
rmdir $PKG/usr/share/html/
rmdir $PKG/usr/share/
}

######################### Build clisp ####################################

function build_clisp () {
# Extract source:
cd $TMPBUILD
#tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION*.tar.!(*sign|*asc|*sig)
tar xf $CWD/clisp-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM*/ || failextract
slackhousekeeping

# clisp doesn't build with distcc.
# No, No , No - stop thinking "Oh I'm sure it'll work this time".  It won't.
# Disable distcc:
export PATH=$( echo $PATH | sed -e 's?/tmp/DISTCC:??g' )
unset NUMJOBS

# Apply patches:
#
# Fix build failure with missing struct member:
zcat $CWD/clisp.c_data.compact_empty_buckets.diff.gz | patch -p1 --verbose || exit 1

# Fix modules that need access to symbols in libgnu.a:
zcat $CWD/clisp.link.libgnu.whole-archive.diff.gz | patch -p1 --verbose || exit 1

# Apply patches for ARM:
#tar xf $PORTCWD/sources/*debian* || exit 1
#for pf in \
#  bump-fasl-loader-version.patch \
#  ; do auto_apply_patch debian/patches/$pf || exit 1
#done

# Adjust CFLAGS:
#case $ARCH in
#   arm)     export SLKCFLAGS="$SLKCFLAGS -marm -falign-functions=4" ;;
#   *)       export SLKCFLAGS="-O0" # legacy, unused.
#esac

# Bootstrap:
if [ -f /var/lib/pkgtools/packages/gtk+2-[0-9]* ]; then
   WITH_GTK2="--with-module=gtk2"
 else
   echo "Bootstrap mode: building without gtk+2"
   WITH_GTK2=""
fi

# Configure:
export CFLAGS="$SLKCFLAGS -Wa,--noexecstack"
export LDFLAGS="$SLKCFLAGS -Wl,-z,noexecstack"
FORCE_UNSAFE_CONFIGURE=1 \
./configure \
   --config builddir \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --enable-portability \
   --with-dynamic-ffi \
   --with-dynamic-modules \
   --with-module=asdf \
   --with-module=berkeley-db \
   --with-module=bindings/glibc \
   --with-module=clx/new-clx \
   --with-module=dbus \
   --with-module=gdbm \
   --with-module=pcre \
   $WITH_GTK2 \
   --with-module=rawsock \
   --with-module=zlib || failconfig

# Build:
make -C builddir || failmake

# Install in to package:
make -C builddir install DESTDIR=$PKG || failinstall

# Move docs around:
#mv $PKG/usr/share/doc/clisp/* $PKG/usr/doc/clisp-$VERSION
rm -r $PKG/usr/share/doc
mv -fv $PKG/usr/share/man $PKG/usr/man
}

# Build:
build_ffcall
build_clisp

# 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
