#!/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-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.
#
# 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

# 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:
function unpacksrc() {
  cd $TMPBUILD
  rm -rf $PKGNAM*
  tar xvvf $CWD/$PKGNAM-$VERSION*.tar.!(*sign|*asc|*sig)
  cd $PKGNAM*/ || exit 1
  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

  # Apply patches:
#  autoreconf -vif
}

unpacksrc || exit 1

# 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

# Build Emacs with native compilation. This has better performance, but the
# installed package is larger. Pass NATIVECOMP=NO to do a normal build.
NATIVECOMP=${NATIVECOMP:-NO}

# 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

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

# Configure:
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 || failmake

# This avoids a collision with Exuberant Ctags...
mv $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

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

# Link the latest NEWS file:
( cd $PKG/usr/doc/$PKGNAM*/
  if [ -r $PKG/usr/share/emacs/*/etc/NEWS ]; then
    ln -vsf /usr/share/emacs/*/etc/NEWS .
  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:
# 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):
if [ "$PGTK_OPTION" = "--with-pgtk" ]; then
   export BUILD="${BUILD}_pgtk"
 else
   export BUILD="${BUILD}_$X_TOOLKIT"
fi

slackmp         # run makepkg -l y -c n

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