#!/usr/bin/env bash
set -e
# Create the 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 cogspurlinux cogspurlinuxht cogspurlinuxhtARM; do
			rm -rf $archive-$TAG.tgz
			echo $archive '=>' $archive-$TAG.tgz
			COPYFILE_DISABLE=1 tar czf $archive-$TAG.tgz $archive
			EXES="$EXES	$archive/lib/squeak/[0-9.-]*/squeak"
		done)
	done
fi
if [ -n "$NL" ]; then
	for d in . assert debug; do
		(cd $d
		set -- nscogspurlinux nscogspurlinuxht nscogspurlinuxhtARM
		for archive in nsvmspurlinux nsvmspurlinuxht nsvmspurlinuxhtARM; do
			rm -rf $archive-$TAG.tgz
			echo $1 '=>' $archive-$TAG.tgz
			COPYFILE_DISABLE=1 tar czf $archive-$TAG.tgz $1
			EXES="$EXES	$1/lib/nsvm/[0-9.-]*/nsvm"
			shift
		done)
	done
fi
if [ -n "$CM" ]; then
	set -- CocoaFast.app CocoaAssert.app CocoaDebug.app
	for app in CogSpur.app assert/CogSpur.app debug/CogSpur.app; do
		test -d $app || mkdir $app
		if [ -d ../building/macos32x86/$SQB/$1 ]; then
			rm -rf $app/* $app-$TAG.tgz
			echo building/macos32x86/$SQB/$1 '=>' $app '=>' $app-$TAG.tgz
			(cd ../building/macos32x86/$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/macos32x86/$SQB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$NM" ]; then
	set -- CocoaFast.app CocoaAssert.app CocoaDebug.app
	for app in Newspeak\ Spur\ Virtual\ Machine.app \
				assert/Newspeak\ Spur\ Virtual\ Machine.app\
				debug/Newspeak\ Spur\ Virtual\ Machine.app; do
		test -d $app || mkdir $app
		if [ -d ../building/macos32x86/$NSB/$1 ]; then
			rm -rf $app/* $app-$TAG.tgz
			echo building/macos32x86/$NSB/$1 '=>' $app '=>' $app-$TAG.tgz
			(cd ../building/macos32x86/$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/macos32x86/$NSB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$PM" ]; then
	set -- CocoaFast.app CocoaAssert.app CocoaDebug.app
	for app in CogPharoSpur.app assert/CogPharoSpur.app debug/CogPharoSpur.app; do
		test -d $app || mkdir $app
		if [ -d ../building/macos32x86/$PHB/$1 ]; then
			rm -rf $app/* $app-$TAG.tgz
			echo building/macos32x86/$PHB/$1 '=>' $app '=>' $app-$TAG.tgz
			(cd ../building/macos32x86/$PHB/$1 >/dev/null;tar cf - *) \
			| (cd $app;tar xf -)
			tar czf $app-$TAG.tgz $app
			EXES="$EXES	$app/Contents/MacOS/Squeak"
		else
			echo building/macos32x86/$PHB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$CW" ]; then
	set -- build/vm buildast/vm builddbg/vm
	for dir in cogspurwin assert/cogspurwin debug/cogspurwin; do
		test -d $dir || mkdir $dir
		if [ -d ../building/win32x86/$SQB/$1 ]; then
			rm -rf $dir/* $dir-$TAG.zip
			echo building/win32x86/$SQB/$1 '=>' $dir '=>' $dir-$TAG.zip
			(cd ../building/win32x86/$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/win32x86/$SQB/$1 is missing
		fi
		shift
	done
fi
if [ -n "$NW" ]; then
	set -- build/vm buildast/vm builddbg/vm
	for dir in nsvmspurwin assert/nsvmspurwin debug/nsvmspurwin; do
		test -d $dir || mkdir $dir
		if [ -d ../building/win32x86/$NSB/$1 ]; then
			rm -rf $dir/* $dir-$TAG.zip
			echo building/win32x86/$NSB/$1 '=>' $dir '=>' $dir-$TAG.zip
			(cd ../building/win32x86/$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/win32x86/$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
