#!/bin/sh

# Copyright 2009  Patrick Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-clisp

VERSION=2.47
DIRNAME=2.47
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-1}
DISTRO=${DISTRO:-slamd64}

# Bundled libraries:
FFCALLVER=20080704cvs
LIBSIGSEVVER=2.6

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

if [ $DISTRO = slamd64 ]; then
	LIBSUFFIX=64
	SLKLDFLAGS="-L/usr/lib64"
else
	LIBSUFFIX=
	SLKLDFLAGS=
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG # place for the package to be built

#=================================================================
# First we need this.  It used to ship with CLISP, but no longer does.
cd $TMP
rm -rf libsigsegv-$LIBSIGSEVVER
tar xvf $CWD/libsigsegv-$LIBSIGSEVVER.tar.bz2 || exit 1
cd libsigsegv-$LIBSIGSEVVER
chown -R root:root .

# Nah.
# --enable-shared=yes 

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --enable-static=yes
make  || exit 1
# Now I must install this.  Sorry, it's gotta happen.
make install
# Now add to the package:
mkdir -p $PKG/usr
make install prefix=$PKG/usr libdir=$PKG/usr/lib$LIBSUFFIX
strip --strip-unneeded $PKG/usr/lib$LIBSUFFIX/*.so
strip -g $PKG/usr/lib$LIBSUFFIX/*.a
mkdir -p $PKG/usr/doc/clisp-$VERSION/libsigsegv-2.4
cp -a \
  AUTHORS COPYING ChangeLog NEWS PORTING README \
  $PKG/usr/doc/clisp-$VERSION/libsigsegv-$LIBSIGSEVVER

#=================================================================
# Build ffcall - needed for --dynamic-ffi with clisp.
cd $TMP
rm -rf ffcall*
tar xvf $CWD/ffcall-$FFCALLVER.tar.*
cd ffcall*
chown -R root:root .

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --mandir=/usr/man \
  --build=$ARCH-$DISTRO-linux || exit 1

make || 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$LIBSUFFIX/*.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/

#=================================================================
 
# OK, now we compile CLISP:
cd $TMP
rm -rf clisp-$VERSION
tar xvf $CWD/clisp-$VERSION.tar.bz2 || exit 1
cd clisp-$DIRNAME || exit 1
chown -R root:root .

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --mandir=/usr/man \
  --docdir=/usr/doc/clisp-$VERSION \
  --with-module=clx/new-clx \
  --with-module=pcre \
  --with-module=rawsock \
  --with-module=wildcard \
  --with-module=zlib \
  --cbc with-gcc-wall \
  --with-dynamic-ffi || exit 1

cd with-gcc-wall
./makemake \
  --with-dynamic-ffi  \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --with-module=clx/new-clx \
  --with-module=pcre \
  --with-module=rawsock \
  --with-module=wildcard \
  --with-module=zlib \
  --srcdir=../src \
  > Makefile
make config.lisp
make init
make allc
make -j4 lisp.run
make interpreted.mem
make halfcompiled.mem
make lispinit.mem
make manual
make modular

make install DESTDIR=$PKG

# Strip stuff:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

#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
gzip -9 $PKG/usr/man/man?/*.?
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/clisp-$VERSION-$PKGARCH-$BUILD.tgz