#!/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=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.*
cd $PKGNAM-$VERSION
slackhousekeeping

# 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 || 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.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

# 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 || 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* ChangeLog INSTALL README \
          $PKG/usr/doc/$PKGNAM-$VERSION

# Make the .desktop file
mkdir -p $PKG/usr/share/applications
cat << "EOF" > $PKG/usr/share/applications/$PKGNAM.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Exec=emacs
Icon=emacs.png
Terminal=0
Name=Emacs
Comment=Text Editor
Categories=Application;Development;
EOF

# Make the package freedesktop compliant
mkdir -vpm755 $PKG/usr/share/icons/hicolor/48x48/apps
install -vpm644 $PKG/usr/share/$PKGNAM/$VERSION/etc/images/icons/emacs_48.png $PKG/usr/share/icons/hicolor/48x48/apps/emacs.png

# 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
