#!/usr/bin/env bash
set -e
# Create the 64-bit Spur VM archives in products.  Output a check of version
# numbers etc to products/MSVALOG
IFS="	
"
cd `dirname $0`/../products
NSB=newspeak.cog.spur
SQB=squeak.cog.spur
PHB=pharo.cog.spur

if [ $# = 0 ]; then
CL=1
CM=1
CW=1
NL=1
NM=1
NW=1
PM=1

else

CL=
CM=
CW=
NL=
NM=
NW=
PM=
	while [ -n "$1" ]; do
		case $1 in
		-c)		CL=1;CM=1;CW=1;;
		-cl)	CL=1;;
		-cm)	CM=1;;
		-cw)	CW=1;;
		-n)		NL=1;NM=1;NW=1;;
		-nl)	NL=1;;
		-nm)	NM=1;;
		-nw)	NW=1;;
		-pm)	PM=1;;
		-l)		CL=1;NL=1;;
		-m)		CM=1;NM=1;PM=1;;
		-w)		CW=1;NW=1;;
		-r)		shift;REV="$1";;
		-t)		shift;TAG="$1";;
		-?|-h)  echo usage: $0 [-c -n -l -m -w -c[lmw] -n[lmw] -pm -r REV -t TAG]
				exit 0
		esac
		shift
	done
fi
test -n "$REV" || REV=`grep 'GitRawRevisionString.*Rev:' ../platforms/Cross/vm/sqSCCSVersion.h \
	| sed 's/^.*Rev: \([0-9][0-9]*\).* $";/\1/'`
test -n "$TAG" || TAG=`date +%y.%U.`$REV
echo REV=$REV TAG=$TAG

if [ -n "$CL" ]; then
	for d in . assert debug; do
		(cd $d
		for archive in cogspur64linux cogspur64linuxht cogspur64linuxhtARM; do
			rm -rf $archive-$TAG.tgz
			if [ -d $archive ]; then
				echo $archive '=>' $archive-$TAG.tgz
				COPYFILE_DISABLE=1 tar czf $archive-$TAG.tgz $archive
				EXES="$EXES	$archive/lib/squeak/[0-9.-]*/squeak"
			fi
		done)
	done
fi
if [ -n "$NL" ]; then
	for d in . assert debug; do
		(cd $d
		set -- nscogspur64linux nscogspur64linuxht nscogspur64linuxhtARM
		for archive in nsvmspur64linux nsvmspur64linuxht nsvmspur64linuxhtARM; do
			rm -rf $archive-$TAG.tgz
			if [ -d $1 ]; then
				echo $1 '=>' $archive-$TAG.tgz
				COPYFILE_DISABLE=1 tar czf $archive-$TAG.tgz $1
				EXES="$EXES	$1/lib/nsvm/[0-9.-]*/nsvm"
			fi
			shift
		done)
	done
fi
if [ -n "$CM" ]; then
	set -- CocoaFast.app CocoaAssert.app CocoaDebug.app
	for app in CogSpur64.app assert/CogSpur64.app debug/CogSpur64.app; do
		test -d $app || mkdir $app
		if [ -d ../building/macos64x64/$SQB/$1 ]; then
			rm -rf $app/* $app-$TAG.tgz
			echo building/macos64x64/$SQB/$1 '=>' $app '=>' $app-$TAG.tgz
			(cd ../building/macos64x64/$SQB/$1 >/dev/null;tar cf - *) \
			| (cd $app;tar xf -)
			tar czf $app-$TAG.tgz $app
			EXES="$EXES	$app/Contents/MacOS/Squeak"
		else
			echo building/macos64x64/$SQB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$NM" ]; then
	set -- CocoaFast.app CocoaAssert.app CocoaDebug.app
	for app in Newspeak\ Spur\ 64-bit\ Virtual\ Machine.app \
				assert/Newspeak\ Spur\ 64-bit\ Virtual\ Machine.app\
				debug/Newspeak\ Spur\ 64-bit\ Virtual\ Machine.app; do
		test -d $app || mkdir $app
		if [ -d ../building/macos64x64/$NSB/$1 ]; then
			rm -rf $app/* $app-$TAG.tgz
			echo building/macos64x64/$NSB/$1 '=>' $app '=>' $app-$TAG.tgz
			(cd ../building/macos64x64/$NSB/$1 >/dev/null;tar cf - *) \
			| (cd $app;tar xf -)
			tar czf $app-$TAG.tgz $app
			EXES="$EXES	$app/Contents/MacOS/Newspeak Virtual Machine"
		else
			echo building/macos64x64/$NSB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$PM" ]; then
	set -- CocoaFast.app CocoaAssert.app CocoaDebug.app
	for app in CogPharoSpur64.app assert/CogPharoSpur64.app debug/CogPharoSpur64.app; do
		test -d $app || mkdir $app
		if [ -d ../building/macos64x64/$PHB/$1 ]; then
			rm -rf $app/* $app-$TAG.tgz
			echo building/macos64x64/$PHB/$1 '=>' $app '=>' $app-$TAG.tgz
			(cd ../building/macos64x64/$PHB/$1 >/dev/null;tar cf - *) \
			| (cd $app;tar xf -)
			tar czf $app-$TAG.tgz $app
			EXES="$EXES	$app/Contents/MacOS/Pharo"
		else
			echo building/macos64x64/$PHB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$CW" ]; then
	set -- build/vm buildast/vm builddbg/vm
	for dir in cogspur64win assert/cogspur64win debug/cogspur64win; do
		test -d $dir || mkdir $dir
		if [ -d ../building/win64x64/$SQB/$1 ]; then
			rm -rf $dir/* $dir-$TAG.zip
			echo building/win64x64/$SQB/$1 '=>' $dir '=>' $dir-$TAG.zip
			(cd ../building/win64x64/$SQB/$1 >/dev/null; \
			 tar cf - Squeak.exe Squeak.map Squeak.exe.manifest \
				SqueakConsole.exe SqueakConsole.map SqueakConsole.exe.manifest \
				*.dll) \
			| (cd $dir;tar xf -)
			zip -qr $dir-$TAG.zip $dir
			EXES="$EXES	$dir/Squeak.exe	$dir/SqueakConsole.exe"
		else
			echo building/win64x64/$SQB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$NW" ]; then
	set -- build/vm buildast/vm builddbg/vm
	for dir in nsvmspur64win assert/nsvmspur64win debug/nsvmspur64win; do
		test -d $dir || mkdir $dir
		if [ -d ../building/win64x64/$NSB/$1 ]; then
			rm -rf $dir/* $dir-$TAG.zip
			echo building/win64x64/$NSB/$1 '=>' $dir '=>' $dir-$TAG.zip
			(cd ../building/win64x64/$NSB/$1 >/dev/null; \
			 tar cf - nsvm*.exe nsvm*.map nsvm*.exe.manifest ) \
			| (cd $dir;tar xf -)
			zip -qr $dir-$TAG.zip $dir
			EXES="$EXES	$dir/nsvm.exe	$dir/nsvmConsole.exe"
		else
			echo building/win64x64/$NSB/$1 is missing
		fi
		shift
	done
fi
for vm in $EXES
do
	echo
	echo checking $vm version
#	strings - $vm | egrep '\$Rev: |Interp.*VMMaker|Cogit.*VMMaker| built on ' | grep -v 'Unix built on "__DATE__ " "__TIME__" Compiler: "__VERSION__' | sed 's/ uuid:.*//'
	strings - $vm | egrep '\$Rev: |Interp.*VMMaker|Cogit.*VMMaker| built on ' | sed 's/ uuid:.*//'
	echo checking $vm for asserts
	strings - $vm | grep "assert[^0-9]*[0-9][0-9][0-9]"
done 2>&1 | tee MSVALOG
