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

# emacs.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2008-2026  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.
#
# Slackware build script for emacs-22

# Written by DuÅ¡an StefanoviÄ<87> (stefanovic.dusan@gmail.com)
# Modified by Robby Workman <rworkman@slackbuilds.org>
# Modified by Patrick Volkerding <volkerdi@slackware.com>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Extract source:
function unpacksrc() {
  cd $TMPBUILD
  rm -rf $PKGNAM*
  tar xf $CWD/$PKGNAM-$VERSION*.tar.!(*sign|*asc|*sig)
  cd $PKGNAM*/ || failextract
  slackhousekeeping

  # Apply patches:

  # The defaults are a little too worried about adding a few more K of pure
  # memory given the amount available on modern systems:
  sed -i "s/#define SYSTEM_PURESIZE_EXTRA 0/#define SYSTEM_PURESIZE_EXTRA 100000/g" src/puresize.h

  # Replace deprecated tree-sitter function name:
  sed -i "s/ts_language_version/ts_language_abi_version/g" src/treesit.c

}

# Obtain the tarball version from the source in the x86 master tree:
pushd $CWD
TARBALLVER=${TARBALLVER:-$(echo $PKGNAM-*.tar.xz | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
popd

# OK, now what's being used as the source directory version number...  account
# for subversions a, b, c, and d in the $TARBALLVER:
SRCDIRVER=$(echo $TARBALLVER | tr -d a | tr -d b | tr -d c | tr -d d)
echo "Source directory version: $SRCDIRVER"
echo "Tar archive version: $TARBALLVER"

# Do we want X support? (causes Emacs to be linked with many X11 libraries):
WITHX=${WITHX:-YES}

# Pass this variable set to "--with-pgtk" to use GTK+3 without linking to X11
# libraries. This might be suitable if you're going to run under Wayland.
PGTK_OPTION=${PGTK_OPTION:-}

# Set the X related options. The default X_TOOLKIT will be GTK+3, but you may
# set the X_TOOLKIT variable to any of these: gtk, gtk2, gtk3, lucid, athena, motif.
if [ "$WITHX" = "YES" ]; then
  if [ "$PGTK_OPTION" = "--with-pgtk" ]; then
    X_TOOLKIT=gtk3
  fi
  X_OPTIONS="--with-x --with-x-toolkit=${X_TOOLKIT:-gtk3}"
else
  X_OPTIONS="--with-x=no"
fi

# "YES", Build Emacs with native compilation. This has better performance, but the
# installed package is larger.
# This package is placed into the main Slackware tree: e/emacs
# "NO": build Emacs without native compilation.
# This package is placed into /extra/emacs-regular-build/
# If there's no directory within /extra to hold the package, we'll assume
# we don't want to build it.  This handles stable releases (e.g. 15.0) that
# don't have this package.
nativecomp_list="YES"
[ -d "$PORTROOTDIR/extra/emacs-regular-build" ] && {
   echo "Will also build emacs regular build for /extra"
   nativecomp_list+=" NO" ;}

for NATIVECOMP in ${nativecomp_list} ; do

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Set the proper options for native compilation or not:
if [ "$NATIVECOMP" = "YES" ]; then
  PDUMPER="--with-dumping=pdumper --with-native-compilation"
else
#  PDUMPER=${PDUMPER:-"--with-pdumper=auto --without-native-compilation"}
  PDUMPER=${PDUMPER:-"--with-pdumper=auto --with-native-compilation=no"}
fi

echo "Native compilation for LISP: ${NATIVECOMP}"
echo "Configuration args: $PDUMPER"

# Configure:
unpacksrc || failextract
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --program-prefix="" \
   --program-suffix="" \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --without-gconf \
   --without-gsettings \
   --with-modules \
   $X_OPTIONS \
   $PGTK_OPTION \
   $PDUMPER \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
if [ "$NATIVECOMP" = "YES" ]; then
  make NATIVE_FULL_AOT=1 bootstrap $NUMJOBS || make NATIVE_FULL_AOT=1 || failmake
else
  make $NUMJOBS || make || failmake
fi

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

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$TARBALLVER
cp -fav \
  AUTHORS* COPYING* INSTALL* README* \
  $PKG/usr/doc/$PKGNAM-$TARBALLVER
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Link the latest NEWS file:
( cd $PKG/usr/doc/$PKGNAM-$TARBALLVER
  if [ -r $PKG/usr/share/emacs/$SRCDIRVER/etc/NEWS ]; then
    ln -vsf /usr/share/emacs/$SRCDIRVER/etc/NEWS .
  fi )

# This avoids a collision with Exuberant Ctags...
mv -fv $PKG/usr/bin/ctags $PKG/usr/bin/ctags-emacs
if [ -r $PKG/usr/man/man1/ctags.1 ]; then
  mv -fv $PKG/usr/man/man1/ctags.1 $PKG/usr/man/man1/ctags-emacs.1
elif [ -r $PKG/usr/man/man1/ctags.1.gz ]; then
  mv -fv $PKG/usr/man/man1/ctags.1.gz $PKG/usr/man/man1/ctags-emacs.1.gz
fi

# 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

# Set special permissions:
# Removed no-x11 binary, but retained for future reference.
#chmod 1755 $PKG/usr/bin/emacs-${TARBALLVER}-no-x11

# Seems like this nonsense is finally obsolete:
if [ -d $PKG/var/games/emacs ]; then
  # I don't care for broken permissions.
  chmod 755 $PKG/var/games/emacs
  chown -R root:games $PKG/var/games/emacs
  chmod 664 $PKG/var/games/emacs/*
fi

# Tag packages that use a toolkit other than default (currently gtk3):
# Not supported on ARM. You'd have to adjust the BUILD variable.
#if [ -z "$TAG" ] && [ ! -z "$X_TOOLKIT" ]; then
#  if [ "$PGTK_OPTION" = "--with-pgtk" ]; then
#    TAG="_pgtk"
#  else
#    TAG="_$X_TOOLKIT"
#  fi
#fi

# If doing the 'regular' build, tag the build as such and place the package into /extra
if [ "$NATIVECOMP" = "NO" ]; then
   ( PKGSERIES=extra
     SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-${BUILD}_regular.txz
     PKGSTORE="$PORTROOTDIR/$PKGSERIES/emacs-regular-build"
     mkdir -vpm755 $PKGSTORE
     slackmp )
 else
   # "normal" native build (for the 'e' Slackware package series):
   slackmp         # run makepkg -l y -c n
fi

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

# 'done' from the for NATIVECOMP.. loop
done
