#!/usr/bin/env bash
set -e
case "`uname -m`" in # ensure we see x86 as machine type
i86pc) ;; # we're good
*) if type i386 2>&1 >/dev/null; then
	echo "Re-exec as x86"
	exec i386 "$0" "$@"
fi ;;
esac
# Stack Spur VM with VM profiler and threaded heartbeat
INSTALLDIR=sqstkspursunosht/usr
# GNU configure AC_SYS_LARGEFILE is not adding -D_FILE_OFFSET_BITS (config.h)
# to use lseek64 instead of lseek, must -D it here
# Some gcc versions create a broken VM using -O2
OPT="-DAIO_DEBUG -DNDEBUG -DDEBUGVM=0 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
LIBS="-lmapmalloc"
MAKE=gmake

if [ $# -ge 1 ]; then
	INSTALLDIR="$1"; shift
fi
if ../../../../scripts/checkSCCSversion ; then exit 1; fi
echo -n "clean? "
read a
case $a in
n|no|N|NO)	echo "ok but this isn't safe!!";;
*)			rm -f config.h; test -f Makefile && $MAKE reallyclean
esac
test -f plugins.int || (test -f ../plugins.int && cp -p ../plugins.int . || cp -p ../../plugins.int .)
test -f plugins.ext || (test -f ../plugins.ext && cp -p ../plugins.ext . || cp -p ../../plugins.ext .)
test -f config.h || ../../../../platforms/unix/config/configure \
		--with-vmversion=5.0 \
		--disable-dynamicopenssl \
		--without-libtls \
		--without-vm-sound-Sun \
		--without-vm-sound-OSS \
	--with-src=src/spur32.stack --disable-cogit \
	--without-vm-display-fbdev --without-npsqueak \
	--with-scriptname=spur32 \
	TARGET_ARCH="-m32" \
	LIBS="$LIBS" \
	CFLAGS="$OPT"
rm -f vm/sqUnixMain.o # nuke version info
rm -rf ../../../../products/$INSTALLDIR
# prefer $MAKE install prefix=`readlink -f \`pwd\`/../../../../products/$INSTALLDIR`
# but older linux readlinks lack the -f flag
$MAKE install-squeak install-doc install-plugins prefix=`(cd ../../../../;pwd)`/products/$INSTALLDIR 2>&1 | tee LOG
