#!/bin/bash
#
# VI editor settings
# set shiftwidth=4
# set tabstop=4
# ==========================================================
# Title: Matlab FAST installer
# Author: Mike Gore
# Date: 15 Oct 2015 
# Tested with 14.04lts
# Notes:
#   Matlab can be installed simply by copying the directory of a working version
#   from an existing system along with its /usr/local/bin symlinks
#   We have stored such TAR gzipped files for each Matlab release on ASIMOV
#   This utility fetches these TAR files and extracts them making for 
#   a very fast install process.
# References: 
#   https://cs.uwaterloo.ca/twiki/view/CF/MatlabInstallation
# Usage:
#   ./fast_install_matlab R2015b
# ==========================================================


if [ $# -eq 1 ]
then
	RELEASE=$1
else
	echo Usage: $0 RELEASE 
	echo    RELEASE is the MATLAB release version like R2015a
	echo
	echo Example: $0 R2015a
	exit 1
fi

ARCH=`arch`
TARFILE=matlab-$RELEASE-$ARCH.tar.gz
echo RELEASE:     $RELEASE
echo ARCH:        $ARCH
echo TAR File:    $TARFILE

rsync -a cscf-adm@asimov.uwaterloo.ca:/coregroup/images/packages/$TARFILE /tmp
cd /
tar xzf /tmp/$TARFILE

CMD=/usr/local/bin/set_matlab_release

cat <<EOF >$CMD
#!/bin/bash
#
# ==========================================================
# Title: Set Matlab Release
# Author: Mike Gore
# Date: 15 Oct 2015 
# Tested with 14.04lts
# Notes:
#   Sets which Matlab release calls the default mcc mbuild matlab commands in /usr/local/bin
# References: 
#   https://cs.uwaterloo.ca/twiki/view/CF/MatlabInstallation
# Usage:
#   $CMD R2015b
# ==========================================================


if [ $# -eq 1 ]
then
	RELEASE=$1
else
	echo "Usage: $CMD RELEASE"
	echo "   RELEASE is the MATLAB release version like R2015a"
	echo "   Sets which Matlab release calls the default mcc mbuild matlab commands in /usr/local/bin"
	echo
	echo "Example: $CMD R2015a"
	exit 1
fi

for i in mex mcc mbuild matlab
do
	ln -sf /usr/local/MATLAB/$RELEASE/bin/mex /usr/local/bin
done
EOF
chmod 755 $CMD

echo "Updating Active matlab version to $RELEASE"
echo "This sets which Matlab release calls the default mcc mbuild matlab commands in /usr/local/bin"
echo "You can change this with the set_matlab_relase command"
$CMD $RELEASE



rm /tmp/$TARFILE