#!/bin/sh

# fetch secrets and write them to their according files
cat $vhr18_db > ../env/vhr18_db.env
cat $vhr18_django > ../env/vhr18_django.env
cat $vhr18_obs > ../env/vhr18_obs.env

cat $emg_db > ../env/emg_db.env
cat $emg_django > ../env/emg_django.env
cat $emg_obs > ../env/emg_obs.env

# use `pvs_testing` bucket instead

sed -i -e 's/emg-data/pvs_testing/g' ../env/emg.env
sed -i -e 's/vhr18-data/pvs_testing/g' ../env/vhr18.env

sed -i -e 's/emg-cache/pvs_testing/g' ../env/emg_obs.env
sed -i -e 's/vhr18-cache/pvs_testing/g' ../env/vhr18_obs.env

# source the env file
set -o allexport
source ../env/emg.env
set +o allexport

mkdir data
docker swarm init
docker network create -d overlay emg-extnet

# create the docker secrets
printf $OS_PASSWORD_DOWNLOAD | docker secret create OS_PASSWORD_DOWNLOAD -
printf $DJANGO_PASSWORD | docker secret create DJANGO_PASSWORD -
printf $OS_PASSWORD | docker secret create OS_PASSWORD -

# create docker configs
printf $sftp_users_emg | docker config create sftp_users_emg -

# override preprocessor default configuration file
cat ./testing_preprocessor_config.yml>../config/emg_preprocessor-config.yml

# deploy the stack
docker stack deploy -c ../docker-compose.emg.yml -c ../docker-compose.emg.dev.yml emg-pvs

./docker-stack-wait.sh -n renderer -n registrar -n preprocessor -n database -n sftp emg-pvs

docker service ls
# perform the testing
bash ./preprocessor_prep.sh preprocessed_list.csv
bash ./registrar_prep.sh product_list.csv

pytest registrar_test.py preprocessor_test.py renderer_test.py

if [ $? -ne 0 ]; then echo "Pytest fail" >&2 && exit 1; fi

swift delete preprocessor_results

# ingest an item
python3 ingestor_prep.py

# wait for the process to complete
sleep 90

# test the existance of ingested item
pytest ingestor_test.py

swift delete preprocessor_results

if [ $? -ne 0 ]
then
  echo "Failure in tests, logging from services:"
  for service in $(docker service ls --format "{{.Name}}"); do
    docker service ps $service --no-trunc >> "/tmp/$service.log"
    docker service logs $service >> "/tmp/$service.log"
    docker service ps $service --no-trunc
    docker service logs $service
  done
  exit 1
fi

