#!/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=armv5te" 
            export LIBDIRSUFFIX="" 
            export CLISP_CONFARGS="--without-dynamic-ffi --with-module=clx/new-clx --with-module=bindings/glibc" 
            ;;
   *)       export SLKCFLAGS="-O2" 
            export CLISP_CONFARGS="--with-dynamic-ffi --with-module=clx/new-clx" ;;
esac

# Bundled libraries:
FFCALLVER=20100619cvs
LIBSIGSEVVER=2.9

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

# Extract source:
cd $TMPBUILD
#tar xvvf $CWD/libsigsegv-$LIBSIGSEVVER.tar.?z*
tar xvvf $PORTCWD/sources/libsigsegv*orig*
cd libsigsegv* || exit 1
slackhousekeeping

# Apply patches:
tar xvvf $PORTCWD/sources/libsigsegv*debian*z
#auto_apply_patch debian/patches/SIGSTKSZ-adjust.patch || exit 1
auto_apply_patch $PORTCWD/sources/SIGSTKSZ-adjust.patch.xz || exit 1

# Generate aclocal.m4.
aclocal -I m4
# Generate configure.
autoconf
# Generate config.h.in.
autoheader && touch config.h.in
# Generate Makefile.in, src/Makefile.in, tests/Makefile.in, and some
# build-aux/* files.
automake --add-missing --copy

# 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* || exit 1
chown -R root:root .

#cp -favv /usr/share/misc/config.* ffcall/autoconf/.
# cd ffcall* || exit 1

#auto_apply_patch $PORTCWD/sources/ffcall*patch* || exit 1

# Generate aclocal.m4.
aclocal -I m4
# Generate configure.
autoconf
# Generate config.h.in.
autoheader && touch config.h.in
# Generate Makefile.in, src/Makefile.in, tests/Makefile.in, and some
# build-aux/* files.
automake --add-missing --copy

# 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 $NUMJOBS || 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
#tar xvvf $PORTCWD/sources/$PKGNAM.tar.xz
cd $PKGNAM* || exit 1
slackhousekeeping

# Apply patches:
for i in $PORTCWD/sources/clisp*patch* ; do
  auto_apply_patch $i || exit 1
done

# Run autoconf for patches
#make -f Makefile.devel configures

ulimit -s unlimited
# Configure:
# For ARM we disable dynamic-ffi. Slackware uses --with-dynamic-ffi.
# we used to but I try again including fybamicffi
FORCE_UNSAFE_CONFIGURE=1 \
CFLAGS="$SLKCFLAGS" \
./configure $CLISP_CONFARGS \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --docdir=/usr/doc/clisp-$VERSION \
   --with-module=pcre \
   --with-module=rawsock \
   --with-module=zlib \
   --cbc builddir || failconfig
#   --with-module=wildcard \

# Install into package:
make -C builddir install DESTDIR=$PKG || exit 1

# 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
