set -x
set -o errexit

export ALLENSDK_PATH=/shared/bioapps/infoapps/lims2_modules/lib/allensdk

NEURON_HOME=/shared/utils.x86_64/nrn-7.4-1370
export PYTHONPATH=${NEURON_HOME}/lib/python:${ALLENSDK_PATH}:${PYTHONPATH}

export PYTHON_HOME=/shared/utils.x86_64/python-2.7
export PYTHON=${PYTHON_HOME}/bin/python
export PATH=${PYTHON_HOME}/bin:${NEURON_HOME}/x86_64/bin:${PATH}

IN_JSON=$1
OUT_JSON=$2

IN_JSON_ABSOLUTE=$(readlink -f ${IN_JSON})
BASEDIR=$(dirname ${IN_JSON_ABSOLUTE})
cd ${BASEDIR}
echo 'Directory changed to ' `pwd`

export RUN_OPTIMIZE="${PYTHON} -W ignore -m allensdk.internal.model.biophysical.run_optimize"
MANIFEST=manifest_sdk.json

rm -rf x86_64 modfiles work cell.hoc ${MANIFEST}

${RUN_OPTIMIZE} generate_manifest_lims ${IN_JSON} ${MANIFEST}
${RUN_OPTIMIZE} copy_local ${MANIFEST} ${OUT_JSON}
${RUN_OPTIMIZE} nrnivmodl ${MANIFEST} ${OUT_JSON} 
${RUN_OPTIMIZE} start_specimen ${MANIFEST} ${OUT_JSON}
${RUN_OPTIMIZE} make_fit ${MANIFEST} ${OUT_JSON}

# clean up
rm -rf x86_64 modfiles work cell.hoc debug.log ${MANIFEST}
