#!/bin/sh
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

VERSION=${VERSION:-1.4.9}
#DOCV=${DOCV:-2.0.4}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

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"
fi

PKG=$TMP/package-slang1
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf slang-$VERSION
tar xjvf $CWD/slang-$VERSION.tar.bz2
cd slang-$VERSION
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr \
  --includedir=/usr/include/slang1 \
  --disable-static \
  $ARCH-slackware-linux

make -j4 || exit 1
make install DESTDIR=$PKG
make -j4 elf || exit 1
make install-elf DESTDIR=$PKG
chmod 755 $PKG/usr/lib/libslang.so.$VERSION

# We are letting S-Lang version 2 take over as the new default:
mv $PKG/usr/lib/libslang.so $PKG/usr/lib/libslang1.so

# --disable-static doesn't always work, but this is guaranteed:
rm -f $PKG/usr/lib/*.a

( 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
)

# Prepare the documentation:
rm -r $PKG/usr/doc/slang
mkdir -p $PKG/usr/doc/slang-$VERSION
cp -a \
  COPYING COPYING.ART COPYING.GPL COPYRIGHT INSTALL.pc INSTALL.unx INSTALL.vms NEWS README UPGRADE.txt \
  $PKG/usr/doc/slang-$VERSION

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

cd $PKG
makepkg -l y -c n $TMP/slang1-$VERSION-$ARCH-$BUILD.tgz