#!/bin/bash

# gpm.SlackBuild
# by Stuart Winter <mozes@slackware.com> 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/$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 $TMP

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     SLKCFLAGS="-O2 -march=armv4t -D_REENTRANT"
            SLKLDFLAGS="-lm" ;;
   powerpc) SLKCFLAGS="-O2" ;;
   sparc)   SLKCFLAGS="-O2" ;;
   *)       SLKCFLAGS="-O2" ;;
esac

# Functions:
# Determine which level a patch needs in order to apply.
function apply_patch () {
 patchfile=$1
 # Most patches should not require more levels than this:
 success=0
 for (( pl=0 ; pl<=5 ; pl++ )) ; do
   echo "Patch : $patchfile , trying patch level $pl"
     patch --fuzz=20 -t --dry-run -p$pl < $patchfile > /dev/null 2>&1 && success=1 && break
   done
 if [ $success = 1 ]; then
    echo "Patch: $patchfile will apply at level $pl"
    patch --fuzz=20 --verbose -p$pl < $patchfile
    return 0
  else
    echo "Patch: $patchfile failed to apply at levels 0-5"
    return 1
 fi
}

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

# Create Package framework:
mkdir -p $PKG/usr/{share/emacs/site-lisp,include,lib,info,/doc/$PKGNAM-$VERSION,sbin,bin,man/man{1,7,8}} \
         $PKG/{lib,etc/rc.d,var/log/setup}

# Need a Debian patch:
zcat $PORTCWD/sources/*diff* | patch -p1
for i in debian/patches/* ; do
  apply_patch $i 
done

# Configure:
autoconf
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
# Only one parallel build -- more causes explosions.
make -j1 LDFLAGS=$SLKLDFLAGS || failmake
zcat $CWD/inputattach.c.gz > inputattach.c
gcc $SLKCFLAGS -o inputattach inputattach.c || falimake

# Install into package:
# This takes care of the old manual way which is commented below:
make install DESTDIR=$PKG || failinstall

# Slackware keeps these in /lib:
#( cd $PKG/usr/lib ; ln -vsf ../../lib/libgpm.so.1 libgpm.so )
#( cd $PKG/lib ; ln -vsf libgpm.so.1.*.* libgpm.so.1 )
( cd $PKG
  mkdir -vpm755 lib
  mv -fv usr/lib/*.so* lib/
  cd usr/lib
  ln -vsf ../../lib/libgpm.so.2 libgpm.so )

#cat inputattach > $PKG/usr/bin/inputattach
#( cd src
#  cat gpm > $PKG/usr/sbin/gpm
#  cat prog/disable-paste > $PKG/usr/bin/disable-paste
#  cat prog/gpm-root > $PKG/usr/bin/gpm-root
#  cat prog/hltest > $PKG/usr/bin/hltest
#  cat prog/mev > $PKG/usr/bin/mev
#  cat prog/mouse-test > $PKG/usr/bin/mouse-test
#  install -vpm644 headers/gpm.h $PKG/usr/include
#  install -vpm644 lib/libgpm.a $PKG/usr/lib/
#  install -vpm755 lib/libgpm.so.1.*.* $PKG/lib/ )

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

# Install docs:
cp -a BUGS COPYING Changelog Changes MANIFEST README TODO \
      $PKG/usr/doc/$PKGNAM-$VERSION
( cd doc
  for page in *.1 *.7 *.8 ; do
    CHAPTER=`echo $page | cut -f 2 -d .`
    mkdir -p $PKG/usr/man/man$CHAPTER
    cat $page | gzip -9c > $PKG/usr/man/man${CHAPTER}/$page.gz
  done
  cat gpm.info | gzip -9c > $PKG/usr/info/gpm.info.gz
  cp -a \
    Announce FAQ HACK_GPM README* \
    $PKG/usr/doc/$PKGNAM-$VERSION )


# Install Slackware mouse config tool:
zcat $CWD/mouseconfig.gz > $PKG/usr/sbin/mouseconfig
chmod 755 $PKG/usr/bin/* $PKG/usr/sbin/*
zcat $CWD/setup.mouse.gz > $PKG/var/log/setup/setup.mouse
chmod 755 $PKG/var/log/setup/setup.mouse

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

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