#!/usr/bin/env bash
set -e
# Spur VM with VM profiler and threaded heartbeat
INSTALLDIR=sqcogspur64linuxht
# Some gcc versions create a broken VM using -O2
OPT="-g -O2 -DNDEBUG -DDEBUGVM=0"

# Prefer clang over gcc if available
if [ -n "$(command -v clang || true)" ]; then CC=clang; else CC=gcc; fi

# librt and libpthread funs now supplied by libc
# Many Linux systems supply empty library files 
# but OpenBSD does not.
if [ `uname` = "OpenBSD" ]; then LIBRT=""; else LIBRT="-lrt"; fi

# Special treatment for OpenBSD Linux
if [ `uname` = "OpenBSD" ]; then
	OPT="$OPT -DMUSL"
	CFLAGS="$CFLAGS $OPT -I/usr/local/include"
	LIBS="$LIBS -lexecinfo"
	LDFLAGS="$LDFLAGS -L/usr/local/lib"
else
	CFLAGS="$CFLAGS $OPT -msse2 -DCOGMTVM=0"
	LIBS="$LIBS -lrt"
	LDFLAGS="$LDFLAGS"
fi

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!!";;
*)			test -f Makefile && make reallyclean
esac
../../../../scripts/copylinuxpluginspecfiles
test -f config.h || ../../../../platforms/unix/config/configure --without-npsqueak \
		--with-vmversion=5.0 \
		--with-src=src/spur64.cog \
		--with-scriptname=spur64 \
	TARGET_ARCH="-m64" \
	CC=$CC \
	CFLAGS="$CFLAGS" \
	LIBS="$LIBS" \
	LDFLAGS="$LDFLAGS"
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
