#!/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, 2009, 2010, 2011, 2012, 2014, 2015, 2017, 2018, 2019  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

  # 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

# Without this, the emacs-no-x11 binary won't work with the installed files:
PDUMPER=${PDUMPER:-"--with-pdumper=no --with-dumping=unexec"}

# 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 \
   --with-x \
   --with-x-toolkit=${X_TOOLKIT:-gtk3} \
   $PDUMPER \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Install into package:
make install DESTDIR=$PKG

( cd $PKG/usr/bin
  rm -f emacs
  mv -fv emacs-${VERSION} emacs-${VERSION}-with-x11
  ln -vsf emacs-${VERSION}-with-x11 emacs )

# Unpack source again, so we can start from fresh:
unpacksrc || exit 1

# 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 \
   --with-x=no \
   $PDUMPER \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Install the non-x version:
install -vpm1755 src/emacs $PKG/usr/bin/emacs-${VERSION}-no-x11
# Create unversioned symlinks for both versions of emacs:
( cd $PKG/usr/bin
  ln -sf emacs-${TARBALLVER}-with-x11 emacs-with-x11
  ln -sf emacs-${TARBALLVER}-no-x11 emacs-no-x11
  # Create a plain "emacs" symlink pointing to emacs-with-x11:
  ln -sf emacs-with-x11 emacs
)

# 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

# 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 [ -z "$TAG" ] && [ ! -z "$X_TOOLKIT" ]; then
   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
