#!/bin/bash

# clisp.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 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/$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="-march=armv4t" 
            export LIBDIRSUFFIX="" 
# I never used this line:
#            export ARCH_CONFARGS="--with-module=clx/mit-clx --without-dynamic-ffi" 
# In armedslack-11.0 we used this:
#            export ARCH_CONFARGS="--with-module=clx/new-clx --without-dynamic-ffi" 
# and now we try with this to try and include dynamic-ffi:
             export ARCH_CONFARGS="--with-dynamic-ffi --with-module=clx/new-clx" 
            ;;
   *)       export SLKCFLAGS="-O2" 
            export ARCH_CONFARGS="--with-dynamic-ffi --with-module=clx/new-clx" ;;
esac

# Bundled libraries:
FFCALLVER=20080704cvs
LIBSIGSEVVER=2.6

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

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/libsigsegv-$LIBSIGSEVVER.tar.bz2
cd libsigsegv-$LIBSIGSEVVER
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --enable-static=yes \
   --build=$ARCH-slackware-linux-gnueabi \
   --host=$ARCH-slackware-linux-gnueabi \
   --target=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake
make install
# Now add to the package:
make install prefix=$PKG/usr

# Install docs:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/libsigsegv-$LIBSIGSEVVER
install -vpm644 AUTHORS COPYING ChangeLog NEWS PORTING README \
                $PKG/usr/doc/$PKGNAM-$VERSION/libsigsegv-$LIBSIGSEVVER

######################### Build ffcall ####################################
# Build ffcall - needed for --dynamic-ffi with clisp.
cd $TMPBUILD
# ffcall hasn't been ported to the eabi yet.
# so when it does we'll need to update..
#tar xvf $CWD/ffcall-$FFCALLVER.tar.*
tar xvvf $PORTCWD/sources/ffcall*orig*
cd ffcall*
zcat $PORTCWD/sources/ffcall*diff* | patch -p1 || exit 1
chown -R root:root .

#cp -favv /usr/share/misc/config.* ffcall/autoconf/.

cd ffcall* || exit 1

# Configure:
#CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --build=$ARCH-slackware-linux-gnueabi || exit 1

# Don't do anymore than 1 parallel build - it breaks.
make -j1 || exit 1
make install DESTDIR=$PKG
# We need ffcall installed in order to build clisp:
make install

# Don't need this:
rm -f $PKG/usr/lib/*.la

# Move docs around:
mkdir -pm755 $PKG/usr/doc/clisp-$VERSION/ffcall-$FFCALLVER
install -vpm644 README NEWS COPYING \
                $PKG/usr/doc/clisp-$VERSION/ffcall-$FFCALLVER
mv -f $PKG/usr/share/html/* $PKG/usr/doc/clisp-$VERSION/ffcall-$FFCALLVER
rmdir $PKG/usr/share/html/
rmdir $PKG/usr/share/

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

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

# 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.
# we used to but I try again including fybamicffi
CFLAGS="$SLKCFLAGS" \
./configure $ARCH_CONFARGS \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --docdir=/usr/doc/clisp-$VERSION \
   --with-module=pcre \
   --with-module=rawsock \
   --with-module=wildcard \
   --with-module=zlib \
   --cbc with-gcc-wall || failconfig

cd with-gcc-wall
./makemake $ARCH_CONFARGS \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-module=pcre \
   --with-module=rawsock \
   --with-module=wildcard \
   --with-module=zlib \
   --srcdir=../src \
   > Makefile || failconfig

# Build:
make config.lisp
make init
make allc
make $NUMJOBS lisp.run || failmake
make interpreted.mem
make halfcompiled.mem
make lispinit.mem
make manual
make modular

# Install into package:
make install DESTDIR=$PKG

# Move docs around:
#mv $PKG/usr/share/doc/clisp/* $PKG/usr/doc/clisp-$VERSION
rm -r $PKG/usr/share/doc
mv $PKG/usr/share/man $PKG/usr/man

# 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
