#!/bin/bash

scriptRoot="$(dirname "$0")"
cd $scriptRoot

rm -Rf Figures
mkdir  Figures

cd ./Code/Mathematica/Results/BufferStockTheory

echo
echo 'In directory:'
echo ''
echo `pwd`
echo ''
echo "Running Mathematica code which should generate all the paper's main results.  Output goes to unix console as well as doAll.out and doApndxLiqConstr.out"
# the tee command allows the output to go both to the console and to a file:
# http://unix.stackexchange.com/questions/41246/how-to-redirect-output-to-multiple-log-files

# Suppress any line containing Graphics because Graphics are not visible in a text output file
if [ `uname` == "Darwin" ]; then # On a Mac, the kernel is at /Applications/Mathematica.app/Contents/MacOS/
    /Applications/Mathematica.app/Contents/MacOS/WolframKernel -noprompt < doAll.m            | tee doAll.out            | grep -v Graphics 
    /Applications/Mathematica.app/Contents/MacOS/WolframKernel -noprompt < doApndxLiqConstr.m | tee doApndxLiqConstr.out | grep -v Graphics 
else
    ping -c 2 -w 5 jhas.win.ad.jhu.edu > /dev/null # Try to contact the JHUEcon license server for Mathematica
    if [ $? -eq 0 ]; then # the prior command succeeded (producing an 'exit status' of 0 in the $? variable)
                                                 WolframKernel -noprompt < doAll.m            | tee doAll.out            | grep -v Graphics 
                                                 WolframKernel -noprompt < doApndxLiqConstr.m | tee doApndxLiqConstr.out | grep -v Graphics
    else
	echo ''
	echo 'Could not contact the JHU Econ Mathematica license server; make sure you are connected to the campus VPN and retry.'
	echo ''
	exit 1
    fi
fi



