#!/bin/bash

# emacs.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for ARMedslack.

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

zcat $CWD/emacs.safe.noautoeval.diff.gz | patch -p1 --verbose || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --program-prefix="" \
   --program-suffix="" \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --enable-static=no \
   --enable-shared=yes \
   --with-x \
   --with-x-toolkit=gtk \
   --build=${ARCH}-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || 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 )

# Also add a version of the binary that is not linked to X11:
cd $TMPBUILD
rm -rf $PKGNAM-$VERSION
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

zcat $CWD/emacs.safe.noautoeval.diff.gz | patch -p1 --verbose || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --program-prefix="" \
   --program-suffix="" \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --enable-static=no \
   --enable-shared=yes \
   --with-x=no \
   --build=${ARCH}-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Install the non-x version:
install -vpm1755 src/emacs $PKG/usr/bin/emacs-${VERSION}-no-x11

# I don't care for broken permissions.
chmod 755 $PKG/var/games/emacs
chown -R games:root $PKG/var/games/emacs

# This avoids a collision with Exuberant Ctags...
mv $PKG/usr/bin/ctags $PKG/usr/bin/ctags-emacs
mv $PKG/usr/man/man1/ctags.1 $PKG/usr/man/man1/ctags-emacs.1

# 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
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
