#!/bin/bash
# -*- ksh -*-
# $Id: tmcdb-explorer,v 1.3 2012/03/01 01:04:10 sharring Exp $
#
# DESCRIPTION
#    Starts the Tmcdb Explorer
#
#
# OPTIONS
#    No options
#
#
# AUTHORS:
#    Rodrigo Tobar - original version
#    Steve Harrington - refinements and extensions
#

dir=$(searchFile bin/tmcdb-explorer)

# Find a directory to store the workspace
candidateDirs="${HOME} ${ACSDATA}/tmp /tmp"
for i in $candidateDirs;
do
	if [ -w $i ]
	then
		workspace_dir=${i}/.tmcdbexplorer-workspace-$(whoami)
		break;
	fi
done

current_version="ALMA-9.0.4-built"
echo 
echo "Launching tmcdb explorer..."
echo "current version is: ${current_version}"
version_file=${workspace_dir}/.tmcdb_version

# function which cleans up the old workspace dir, recreates it, and puts a version file into it
cleanUpOldInstallation()
{
	echo "new tmcdb software release requires removal of the previous version's workspace directory..."
	echo "deleting workspace directory: ${workspace_dir}"
	rm -rf ${workspace_dir}
	mkdir ${workspace_dir}
	echo ${current_version} > ${version_file}
}

if [ -f ${version_file} ]
then
	existing_version=`cat ${version_file}`
	echo "pre-existing workspace version detected was: ${existing_version}"
	if [ ${existing_version} != ${current_version} ]
	then
		echo "current and pre-existing workspace versions differ..."
		cleanUpOldInstallation
	else
		echo "current and pre-existing workspace versions are identical..." 
		echo "(re)using existing workspace directory: ${workspace_dir}"
	fi
else
	echo "pre-existing workspace version was not detected..."
	cleanUpOldInstallation
fi

echo

exec $dir/lib/TmcdbExplorer/tmcdb-explorer \
	-consoleLog \
	-data ${workspace_dir} \
	-configuration ${workspace_dir} \
	-vm ${JAVA_HOME}/bin \
	-vmargs \
	-DACS.data=${ACSDATA} \
	-Xmx2048m
