#!/bin/bash -e
# Upload archives from products to a web/ftp site via ssh.
RemoteUser=eliotmiranda@highland-park.dreamhost.com
RemoteRoot=mirandabanda.org/files/Cog/VM
IFS="	
"
REV=
TAG=
LCOG=
LTCOG=
LSPUR=
LSPUR64=
LTSPUR=
LASPUR=
MCOG=
MTCOG=
MSPUR=
MSPUR64=
MPSPUR=
MPSPUR64=
MTSPUR=
WCOG=
WTCOG=
WSPUR=
WTSPUR=
LNSSPUR=
MNSSPUR=
WNSSPUR=
LNSSPUR64=
MNSSPUR64=

cd `dirname $0`/../products
VERBOSE=
JustUpload=


function all () {
	CheckDMG=1
	CheckMSI=1
	LCOG=1
	LTCOG=1
	LSPUR=1
	LASPUR=1
	MCOG=1
	MTCOG=1
	MSPUR=1
	WCOG=1
	WSPUR=1
	MPSPUR=1
	LNSSPUR=1
	MNSSPUR=1
	WNSSPUR=1
	LSPUR64=1
	MSPUR64=1
	MPSPUR64=1
	LNSSPUR64=1
	MNSSPUR64=1
}

if [ $# = 0 ]; then
	all
else
	CheckDMG=
	CheckMSI=
	while [ -n "$1" ]; do
		case "$1" in
		-archives)	JustUpload=JustUpload;;
		-all)	all;;
		-spur)	CheckDMG=1;CheckMSI=1;
				LSPUR=1;LASPUR=1;MSPUR=1;WSPUR=1;LNSSPUR=1;MNSSPUR=1;WNSSPUR=1;
				LSPUR64=1;MSPUR64=1;MPSPUR64=1;LNSSPUR64=1;MNSSPUR64=1;;
		-64)	LSPUR64=1;MSPUR64=1;MPSPUR64=1;LNSSPUR64=1;MNSSPUR64=1;;
		-c)		LCOG=1;LTCOG=1;MCOG=1;MTCOG=1;WCOG=1;WMTCOG=1;
				LSPUR=1;LASPUR=1;MSPUR=1;WSPUR=1;;
		-cl)	LCOG=1;LTCOG=1;LSPUR=1;LASPUR=1;;
		-cm)	MCOG=1;MTCOG=1;MSPUR=1;;
		-cw)	WCOG=1;WTCOG=1;WSPUR=1;;
		-ctl)	LTCOG=1;;
		-ctm)	MTCOG=1;;
		-ctw)	WTCOG=1;;
		-nl)	LNSSPUR=1;;
		-nm)	MNSSPUR=1;
				CheckDMG=1;;
		-nw)	WNSSPUR=1;
				CheckMSI=1;;
		-n)		LNSSPUR=1;MNSSPUR=1;WNSSPUR=1;LNSSPUR64=1;MNSSPUR64=1;
				CheckDMG=1;CheckMSI=1;;
		-p)		MPSPUR=1;MPSPUR64=1;;
		-sl64)	LSPUR64=1;LNSSPUR64=1;;
		-l)		LCOG=1;LTCOG=1;LSPUR=1;LASPUR=1;LNSSPUR=1;LSPUR64=1;LNSSPUR64=1;;
		-m)		MCOG=1;MTCOG=1;MSPUR=1;MNSSPUR=1;MSPUR64=1;MPSPUR=1;MPSPUR64=1;
				CheckDMG=1;;
		-w)		WCOG=1;WTCOG=1;WSPUR=1;WNSSPUR=1;
				CheckMSI=1;CheckDMG=1;;
		-v)		VERBOSE=1;;
		-r)		shift;REV="$1";;
		-t)		shift;TAG="$1";;
		-T)		TEST=1;;
		-?|-h)  echo usage: $0 [-all -l -m -w -c -n -p -cl -cm -cw -ctl -ctm -ctw -nl -nm -nw -archives -r REV -t TAG -T]; exit 0;;
		*)	echo usage: $0 [-all -l -m -w -c -n -p -cl -cm -cw -ctl -ctm -ctw -nl -nm -nw -archives -r REV -t TAG -T]; exit 1;;
		esac
		shift
	done
fi

if [ -z "$REV" ]; then
    REV="`git log --format=%ad --date=format:%Y%m%d%H%M`"
    if git status --porcelain | grep M; then
	echo -n "source tree is modified, are you sure you want to continue? "
	read a
	case $a in
	    y|Y)	;;
	    *)		exit 1
	esac
    fi
fi

if [ -z "$TAG" ]; then
	TAG=`date +%y.%U.`$REV
fi
echo REV=$REV TAG=$TAG

ARCHIVES=
test -n "$LCOG" && ARCHIVES="$ARCHIVES	coglinux-$TAG.tgz	coglinuxht-$TAG.tgz"
test -n "$LTCOG" && ARCHIVES="$ARCHIVES	cogmtlinuxht-$TAG.tgz"
test -n "$LSPUR" && ARCHIVES="$ARCHIVES	cogspurlinux-$TAG.tgz	cogspurlinuxht-$TAG.tgz"
test -n "$LSPUR64" && ARCHIVES="$ARCHIVES	cogspur64linux-$TAG.tgz	cogspur64linuxht-$TAG.tgz"
test -n "$LASPUR" && ARCHIVES="$ARCHIVES	cogspurlinuxhtARM-$TAG.tgz"
test -n "$LTSPUR" && ARCHIVES="$ARCHIVES	cogspurmtlinuxht-$TAG.tgz"
test -n "$MCOG" && ARCHIVES="$ARCHIVES	Cog.app-$TAG.tgz	CogSpur.app-$TAG.tgz"
test -n "$MTCOG" && ARCHIVES="$ARCHIVES	CogMT.app-$TAG.tgz"
test -n "$MSPUR" && ARCHIVES="$ARCHIVES	CogSpur.app-$TAG.tgz"
test -n "$MSPUR64" && ARCHIVES="$ARCHIVES	CogSpur64.app-$TAG.tgz"
test -n "$MTSPUR" && ARCHIVES="$ARCHIVES	CogMTSpur.app-$TAG.tgz"
test -n "$MPSPUR" && ARCHIVES="$ARCHIVES	CogPharoSpur.app-$TAG.tgz"
test -n "$MPSPUR64" && ARCHIVES="$ARCHIVES	CogPharoSpur64.app-$TAG.tgz"
test -n "$WCOG" && ARCHIVES="$ARCHIVES	cogwin-$TAG.zip"
test -n "$WTCOG" && ARCHIVES="$ARCHIVES	cogmtwin-$TAG.zip"
test -n "$WSPUR" && ARCHIVES="$ARCHIVES	cogspurwin-$TAG.zip"
test -n "$WTSPUR" && ARCHIVES="$ARCHIVES	cogspurmtwin-$TAG.zip"

test -n "$LNSSPUR" && ARCHIVES="$ARCHIVES	nsvmspurlinux-$TAG.tgz	nsvmspurlinuxht-$TAG.tgz	nsvmspurlinuxhtARM-$TAG.tgz"
test -n "$LNSSPUR" && ARCHIVES="$ARCHIVES	nsvmspurlinux-$TAG.tgz	nsvmspurlinuxht-$TAG.tgz	nsvmspurlinuxhtARM-$TAG.tgz"
test -n "$LNSSPUR64" && ARCHIVES="$ARCHIVES	nsvmspur64linux-$TAG.tgz	nsvmspur64linuxht-$TAG.tgz"
test -n "$MNSSPUR" && ARCHIVES="$ARCHIVES	Newspeak Spur Virtual Machine-$TAG.dmg	Newspeak Spur Virtual Machine.app-$TAG.tgz"
test -n "$WNSSPUR" && ARCHIVES="$ARCHIVES	nsvmspurwin-$TAG.zip	nsvm-spur-$TAG.msi"
test -n "$MNSSPUR64" && ARCHIVES="$ARCHIVES	Newspeak Spur 64-bit Virtual Machine-$TAG.dmg	Newspeak Spur 64-bit Virtual Machine.app-$TAG.tgz"

if [ -n "$VERBOSE" ]; then
	for a in $ARCHIVES; do
		echo upload $a
	done
fi
if [ -n "$CheckDMG" -a -n "$MNSSPUR" -a ! -f "Newspeak Spur Virtual Machine-$TAG.dmg" ]; then
	NMID=../build.macos32x86/newspeak.cog.spur/installer
	if [ -f "Newspeak Spur Virtual Machine-$TAG.dmg" ]; then
		true
	elif [ -f "$NMID/Newspeak Spur Virtual Machine-$TAG.dmg" ]; then
		ln "$NMID/Newspeak Spur Virtual Machine-$TAG.dmg" .
	else
		echo Newspeak Spur Virtual Machine-$TAG.dmg is missing 1>&2
		exit 1
	fi
fi
if [ -n "$CheckDMG" -a -n "$MNSSPUR64" -a ! -f "Newspeak Spur 64-bit Virtual Machine-$TAG.dmg" ]; then
	NMID=../build.macos64x64/newspeak.cog.spur/installer
	if [ -f "Newspeak Spur 64-bit Virtual Machine-$TAG.dmg" ]; then
		true
	elif [ -f "$NMID/Newspeak Spur 64-bit Virtual Machine-$TAG.dmg" ]; then
		ln "$NMID/Newspeak Spur 64-bit Virtual Machine-$TAG.dmg" .
	else
		echo Newspeak Spur 64-bit Virtual Machine-$TAG.dmg is missing 1>&2
		exit 1
	fi
fi
if [ -n "$CheckMSI" -a -n "$WNSSPUR" -a ! -f nsvm-spur-$TAG.msi ]; then
	NMID=../build.win32x86/newspeak.cog.spur/installer
	if [ -f nsvm-spur-$TAG.msi ]; then
		true
	elif [ -f "$NWID/nsvm-spur-$TAG.msi" ]; then
		ln "$NWID/nsvm-spur-$TAG.msi" .
	else
		echo nsvm-spur-$TAG.msi is missing 1>&2
		exit 1
	fi
fi
if [ -z "$JustUpload" ]; then
  if [ ! -f ../history/README.$REV ]; then
	cat <<THEEND >../history/README.$REV
N.B. For Squeak/Pharo/Croquet please use the archives whose names begin with
     Cog or cog.  The archives whose names begin with nsvm or Newspeak are
     for Newspeak and are missing plugins required by Squeak/Pharo/Croquet.
     VMs with "mt" or "MT" in the name are multi-threaded VMs which support
     non-blocking FFI calls.  The archives containing "Spur" or "spur" are
     VMs using the new Spur object representation and garbage collector and
     should be used with Spur-format Squeak/Pharo/Croquet or Newspeak images.
     Squeak V5, Newspeak and the upcoming Pharo release have moved to Spur.

N.B. Only the Newspeak installers on this site include a sources file.  This
     saves space. To ensure access to the sources file using the other VMs
     - on linux add the sources file to the same directory as the vm, e.g. add
       cogspurlinux/lib/squeak/4.5-3692/SqueakV50.sources
     - on Mac OS add the sources into the Resources directory of the app bundle
     - on Windows add the sources file to the same directory as the vm

     Archives whose names include "64" are 64-bit Spur VMs.  They should be
     used with 64-bit Spur images.  Sample 64-bit images can be found at
         http://www.mirandabanda.org/files/Cog/SpurImages

     Linux
     There are two variants of the Linux VMs; those ending in "ht" have a
     heartbeat thread, while those that don't use an interval timer for the
     heartbeat (the Windows and Mac VMs have a threaded heartbeat).  The
     threaded heartbeat is better (for example, signals from the interval timer
     interfere with system calls, etc), but to use it one must have a kernel
     later than 2.6.12 and configure linux to allow the VM to use multiple
     thread priorities.  To do so, create a file called VM.conf where VM is
     the name of the vm executable ("squeak" for the Squeak vm, "nsvm" for
     the Newspeak vm) in /etc/security/limits.d/ with contents:
*       hard    rtprio  2
*       soft    rtprio  2

     e.g.

sudo cat >/etc/security/limits.d/squeak.conf <<END
*       hard    rtprio  2
*       soft    rtprio  2
END
sudo cp /etc/security/limits.d/squeak.conf /etc/security/limits.d/nsvm.conf

     Only new processes will have the new security settings. Users must log
     out and log back in for the limits to take effect.  Services must stop
     and then restart for the changes to take effect.  To use this VM as a
     daemon, e.g. under daemontools, you'll need to raise the limit manually.
     Make sure you're using bash and before your launch command, raise the max
     thread priority limit with ulimit -r 2, e.g. versions of the following
     script will work on ubuntu
        #!/bin/bash -e
        cd /path/to/squeak/directory
        ulimit -r 2
        exec setuidgid <account> ./coglinuxht/squeak -vm display-null -vm sound-null squeak.image

THEEND
if grep -s '<dpiAware>true</dpiAware>' ../build.win32x86/squeak.cog.spur/Squeak.exe.manifest; then
cat <<THEEND >>../history/README.$REV
     Windows
     The Windows VMs are "dpiAware", which means that by default the display is
     not scaled and display pixels are mapped 1 to 1 to the screen.  If this
     behaviour is not desired you can enable scaling by either editing the
     relevant manifest file (e.g. Squeak.exe.manifest), changing the "true" in
     <dpiAware>true</dpiAware> to "false", or simply deleting the manifest file.
THEEND
else
cat <<THEEND >>../history/README.$REV
     Windows
     The Windows VMs are not "dpiAware", which means that by default the display
     is scaled and display pixels are not directly mapped to screen pixels.  If
     this behaviour is not desired you can disable scaling by editing the VM's
     manifest file (e.g. Squeak.exe.manifest), changing the "false" in
     <dpiAware>false</dpiAware> to "true", or simply deleting the manifest file.
THEEND
fi
        echo getting version control log
	git log -- ../platforms/Cross/vm/sqSCCSVersion.h >>../history/README.$REV
	vi ../history/README.$REV
  fi
  ../scripts/compileChangeHistory $REV
fi
if [ -z "$ARCHIVES" ]; then
	echo 'No archives have been specified.  Use -all?' 1>&2
	exit 1
fi

if [ "$T" = 1 ]; then
SSH="echo ssh"
SCP="echo scp"
else
SSH=ssh
SCP=scp
fi
$SSH -x $RemoteUser mkdir $RemoteRoot/VM.r$REV
if [ -z "$JustUpload" ]; then
	echo scp -p ChangeHistory ../history/README.$REV $RemoteUser:$RemoteRoot/VM.r$REV
	$SCP -p ChangeHistory ../history/README.$REV $RemoteUser:$RemoteRoot/VM.r$REV
fi
echo scp -p $ARCHIVES "$@" $RemoteUser:$RemoteRoot/VM.r$REV
$SCP -p $ARCHIVES "$@" $RemoteUser:$RemoteRoot/VM.r$REV
echo ssh $RemoteUser chmod a-w $RemoteRoot/VM.r$REV/* \\\; ls -al $RemoteRoot/VM.r$REV
$SSH $RemoteUser chmod a-w $RemoteRoot/VM.r$REV/* \; ls -al $RemoteRoot/VM.r$REV

echo $SSH $RemoteUser rm $RemoteRoot/latest \; ln -s VM.r$REV $RemoteRoot/latest
$SSH $RemoteUser rm $RemoteRoot/latest \; ln -s VM.r$REV $RemoteRoot/latest

if [ "$T" != 1 ]; then
test -x image/makegetvmscripts.sh && (image/makegetvmscripts.sh)
fi
test -x /usr/bin/pbcopy && (echo "... at http://www.mirandabanda.org/files/Cog/VM/VM.r$REV"; echo; tail -n +39 ../history/README.$REV) | pbcopy
