#!/bin/bash

# gpm.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 01-Jun-2004

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

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

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="-O2" ;;
   sparc)   SLKCFLAGS="-O2" ;;
   *)       SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xjvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Package framework:
mkdir -p $PKG/usr/{share/emacs/site-lisp,include,lib,info,/doc/$PACKAGE-$VERSION,sbin,bin,man/man{1,7,8}} \
         $PKG/{lib,etc}

# Grab the Slackware mouse setup scripts:
# (the rest of the package framework contains old cruft with version numbers)
tar zxvvf $CWD/_gpm.tar.gz -C$PKG var/log/setup/setup.mouse usr/sbin/mouseconfig 

# For Splack (SPARC port) we'll use David Cantrell's gpm framework:
if [ "${PORTARCH}" = "sparc" ]; then
   tar pzxvvf $PORTCWD/sparc/_gpm.tar.gz -C$PKG var/log/setup/setup.mouse
fi

# Patch the mouse configuration utility for ARM-based machines:
if [ "${PORTARCH}" = "arm" ]; then
   ( cd $PKG/var/log/setup
     patch -p0 < $PORTCWD/arm/setupdiff/setup.mouse.diff || failpatch )
fi

# Apply patches (to see the purpose of these patches, please see gpm.SlackBuild.orig)
#zcat $CWD/gpm_$VERSION-10.diff.gz | patch -p1 --backup --suffix=.orig -E
#zcat $CWD/gpm-1.19.6.time.diff.gz | patch -p1 --backup --suffix=.orig -E
# Let's apply Debian's patch set instead:
zcat $PORTCWD/sources/gpm*diff*.gz | patch -p1
for i in debian/patches/* ; do patch -p1 < $i ; done
# Patch for building with gcc 3.4 (Thanks to Jim Hawkins):
#zcat $PORTCWD/sources/gcc3.4.patch.gz | patch -p0 --verbose || failpatch

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --with-curses \
  --sysconfdir=/etc || failconfig

# Build:
make -i || failmake

# Install into package:
( cd src
  strip disable-paste gpm mev gpm-root libgpm.so.$VERSION
  install -m755 gpm-root disable-paste mev $PKG/usr/bin
  install -m755 gpm $PKG/usr/sbin
  install -m644 headers/gpm.h $PKG/usr/include
  install -m644 libgpm.a $PKG/usr/lib
  install -m755 libgpm.so.$VERSION $PKG/lib )

( cd conf
  install -m644 gpm-root.conf $PKG/etc
  install -m644 gpm-syn.conf $PKG/etc
  install -m644 gpm-twiddler.conf $PKG/etc )
( cd contrib/emacs
  install -m644 t-mouse.el $PKG/usr/share/emacs/site-lisp )

# Install docs:
cp -a BUGS COPYING ChangeLog Changes MANIFEST README TODO \
      doc/Announce doc/FAQ doc/README* doc/gpmdoc.txt \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Install man pages:
( cd doc
  install -m644 *.1 $PKG/usr/man/man1
  install -m644 *.7 $PKG/usr/man/man7
  install -m644 *.8 $PKG/usr/man/man8
  install -m644 gpm.info $PKG/usr/info )

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

# 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 (root:bin), slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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