#!/usr/bin/env bash
set -e
# PharoVM with VM profiler and itimer heartbeat
THIRDPARTYLIBS="libsdl2 openssl libssh2 libgit2"
INSTALLDIR=phcogspur64linux
# Some gcc versions create a broken VM using -O2
OPT="-g -O2 -DNDEBUG -DDEBUGVM=0"
OPT="$OPT -DPharoVM"

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
#	for lib in ${THIRDPARTYLIBS}; do
#		../../third-party/mvm ${lib} clean
#	done
esac
../../../../scripts/copylinuxpluginspecfiles

for lib in ${THIRDPARTYLIBS}; do
	../../third-party/mvm ${lib}
done

test -f config.h || ../../../../platforms/unix/config/configure \
		--without-npsqueak \
		--with-vmversion=5.0 \
		--with-src=src/spur64.cog \
	TARGET_ARCH="-m64" \
	CC=clang \
	CFLAGS="$OPT -msse2 -DCOGMTVM=0 -DITIMER_HEARTBEAT=1" \
	LDFLAGS="-Wl,-rpath,'\$\$ORIGIN' "
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
productDir=`find ../../../../products/$INSTALLDIR -name "5.0*"`
productDir=`(cd $productDir;pwd)`
for lib in ${THIRDPARTYLIBS}; do
	../../third-party/mvm ${lib} install $productDir
done
../../editpharoinstall.sh ../../../../products/$INSTALLDIR "$@"
