#!/bin/bash

# clisp.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 19-Jul-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 $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-march=armv3 -mtune=xscale" 
#            export ARCH_CONFARGS="--with-module=clx/mit-clx --without-dynamic-ffi" ;;
            export ARCH_CONFARGS="--with-module=clx/new-clx --without-dynamic-ffi" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" 
            export ARCH_CONFARGS="--with-dynamic-ffi --with-module=clx/new-clx" ;;
esac

# Version of libsigsegv
SIGSEGVVER=2.4

# Create some package framework:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION/libsigsegv-$SIGSEGVVER

######################### Build libsigsegv ####################################

# Extract source:
cd $TMPBUILD
tar jxvvf $CWD/libsigsegv-$SIGSEGVVER.tar.bz2
cd libsigsegv-$SIGSEGVVER
slackhousekeeping

# Configure:
#CFLAGS="-O2 $SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --enable-static=yes \
   --build=$ARCH-slackware-linux \
   --host=$ARCH-slackware-linux \
   --target=$ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Mark's slack390 script works around this but at this moment in time my brain
# isn't capable of merging Mark's work with Pat's, and I'm not too fussed about
# installing it anyway - my ARMedslack dev boxes are often reinstalled.
make install
make install prefix=$PKG/usr

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION/libsigsegv-$SIGSEGVVER
cp -a AUTHORS COPYING ChangeLog NEWS PORTING README \
      $PKG/usr/doc/$PACKAGE-$VERSION/libsigsegv-$SIGSEGVVER

######################### Build clisp ####################################

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

# Apply patches for ARM:
#if [ $PORTARCH = arm ]; then
#   bzcat $PORTCWD/sources/arm-build.diff.bz2 | patch -p1 || failpatch
##   bzcat $PORTCWD/sources/autotool.diff.bz2  | patch -p1 || failpatch
#fi

# Get rid of this out of the source.  Otherwise it makes the build fail because
# it gets included as a makefile target yet the files don't exist.
# The Changelog says it's been removed anyway so why it's still here I don't know.
# I also don't know why this doesn't fail on Intel because the file isn't there
# either but it's still in the makefile.
#fgrep -lr -- 'split-sequence.lisp' . | xargs sed -i 's?split-sequence.lisp??g'
#fgrep -lr -- 'split-sequence.fas'  . | xargs sed -i 's?split-sequence.fas??g'
#fgrep -lr -- 'split-sequence'      . | xargs sed -i 's?split-sequence??g'

# Configure:
# For ARM we disable dynamic-ffi. Slackware uses --with-dynamic-ffi.
./configure $ARCH_CONFARGS \
   --prefix=/usr \
   --with-module=pcre \
   --with-module=rawsock \
   with-gcc-wall || failconfig


#   --with-module=zlib \
#   --with-module=wildcard \

cd with-gcc-wall
./makemake $ARCH_CONFARGS \
   --prefix=/usr \
   --with-module=pcre \
   --with-module=rawsock \
   --srcdir=../src \
   > Makefile

# Build:
make config.lisp || failmake
make || failmake

# Install into package:
make install DESTDIR=$PKG

# Move docs around:
mv -f $PKG/usr/share/doc/clisp/* $PKG/usr/doc/clisp-$VERSION
rm -f $PKG/usr/doc/clisp-$VERSION/clisp.{dvi,html,ps}
rm -f $PKG/usr/doc/clisp-$VERSION/doc/clisp.1
rm -rf $PKG/usr/share/{doc,dvi,html}

# 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

# 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
