#!/usr/bin/env bash
set -e
case "`uname -m`" in # ensure we see x86 as machine type
i*86) ;; # we're good
*) if type i386 2>&1 >/dev/null; then
	echo "Re-exec as x86"
	exec i386 "$0" "$@"
fi ;;
esac
# Stack VM with VM profiler and threaded heartbeat
INSTALLDIR=sqstklinuxht
# Some gcc versions create a broken VM using -O2
OPT="-g -O2 -DNDEBUG -DDEBUGVM=0"

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
../../../../scripts/copylinuxpluginspecfiles
test -f config.h || ../../../../platforms/unix/config/configure \
	--with-src=src/v3.stack --disable-cogit \
	--without-vm-display-fbdev --without-npsqueak \
	--with-scriptname=cog \
	TARGET_ARCH="-m32" \
	CFLAGS="$OPT -msse2"
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-plugins prefix=`(cd ../../../../;pwd)`/products/$INSTALLDIR 2>&1 | tee LOG ; test ${PIPESTATUS[0]} -eq 0
