#!/bin/sh

VERSION=5.43
TCL=8.4.11
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1tukaani}

set -e
PKG=/tmp/package-expect
rm -rf "$PKG"

CWD=`pwd`

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

export CFLAGS="$SLKCFLAGS"

# We need includes from this:
cd /tmp
rm -rf tcl$TCL
tar xzvf $CWD/tcl$TCL-src.tar.gz
cd tcl$TCL/unix
./configure --prefix=/usr

## Build and install static version:
#( cd /tmp
#tar xzvf $CWD/expect-$VERSION.tar.gz
#cd expect-$VERSION
#./configure --prefix=/usr --with-tclconfig=/usr/lib \
#  --with-tclinclude=/tmp/tcl$TCL/generic
#make
#make install )
#mv /tmp/expect-$VERSION /tmp/expect-$VERSION-static

# Build and install shared version:
cd /tmp
rm -rf expect-$VERSION
tar xzvf $CWD/expect-${VERSION}.0.tar.gz
cd expect-$VERSION
./configure --prefix=/usr --with-tclconfig=/usr/lib \
  --with-tclinclude=/tmp/tcl$TCL/generic --enable-shared
make
make INSTALL_ROOT=$PKG install
mkdir -p $PKG/usr/doc/expect-$VERSION
cp -a \
  FAQ HISTORY INSTALL NEWS README example \
  $PKG/usr/doc/expect-$VERSION

cd $PKG/usr/lib
strip -g *.so *.a */*.a
strip $PKG/usr/bin/expect*
rm -rf libexpect.a
ln -sf libexpect${VERSION}.a libexpect.a
rm -rf libexpect.so
ln -sf libexpect${VERSION}.so libexpect.so

gzip -9 $PKG/usr/man/*/*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd "$PKG"
makepkg -c y -l y ../expect-$VERSION-$ARCH-$BUILD.tlz
