#!/bin/bash

# Set up conda environment, if necessary,
# then in each module:
# 1. run setup
# 2. if there is a test/ subdirectory, execute pytest therein
# Caveat Programmor: this is just a convenience thing Janet created
# for her own use -- it's NOT production-ready! A robust implementation
# of this functionality is in progress for
# https://open-jira.nrao.edu/browse/SSA-6727.

TOP_LEVEL=/Users/jgoldste/Projects/data/
cd $TOP_LEVEL

if [ -z $CONDA_DEFAULT_ENV ]
then
  echo '>>> Updating conda environment....'
  conda env update
  echo '>>> Activating....'
  conda activate data
else
  echo "conda environment '${CONDA_DEFAULT_ENV}' is active"
fi

SETUP=setup.py
echo ">>> looking for ${SETUP} in all modules...."
SETUPS=$(find . -name $SETUP -exec echo '{}' \;)
for item in $SETUPS
do
  dir=$(dirname $item)
  dir=${TOP_LEVEL}${dir:1}
  cd ${dir}
  if [ -z $? ]
  then
    exit $?
  fi
  echo ">>> running ${SETUP} in ${dir}...."
  python setup.py develop
  if [ -d $dir/test ]
  then
    cd test
    pytest --log-level=DEBUG --showlocals
  fi
done


