#!/bin/bash

salix_dir="${1:-$HOME/Projects/salix}"
salix_dir=$(eval echo "$salix_dir")

echo "$salix_dir"

current_dir=$(pwd)

cleanup() {
    docker-compose -p e2e --project-directory . -f test/cypress/docker-compose.yml down -v
}

trap cleanup SIGINT

# CLEAN
rm -rf test/cypress/screenshots
rm -f test/cypress/results/*
rm -f test/cypress/reports/*
rm -f junit/e2e-*.xml

# RUN
export CI=true
export TZ=Europe/Madrid

# IMAGES
docker build -t registry.verdnatura.es/salix-back:dev -f "$salix_dir/back/Dockerfile" "$salix_dir"
cd "$salix_dir" && npx myt run -t
docker exec vn-database sh -c "rm -rf /mysql-template"
docker exec vn-database sh -c "cp -a /var/lib/mysql /mysql-template"
docker commit vn-database registry.verdnatura.es/salix-db:dev
docker rm -f vn-database
cd "$current_dir"
docker build -f ./docs/Dockerfile.dev -t lilium-dev .
# END IMAGES

docker-compose -p e2e --project-directory . -f test/cypress/docker-compose.yml up -d

docker run -it --rm \
    -v "$(pwd)":/app \
    --network e2e_default \
    -e CI \
    -e TZ \
    lilium-dev \
    bash -c 'sh test/cypress/cypressParallel.sh 2'

cleanup