#!/bin/bash

set -eu

readonly source_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly DATA_DIR="${HOME}/.cloudron_test"
readonly DEFAULT_TESTS="./src/test/*-test.js ./src/routes/test/*-test.js"

# fast path: if explicit tests are provided, run them directly and skip setup.
if [[ $# -gt 0 ]]; then
    BOX_ENV=test ./node_modules/.bin/mocha --bail --no-timeouts --exit -R spec "$@"
    exit $?
fi

! "${source_dir}/src/test/check-install" && exit 1

# cleanup old data dirs some of those docker container data requires sudo to be removed
echo "=> Provide root password to purge any leftover data in ${DATA_DIR} and load apparmor profile:"
sudo rm -rf ${DATA_DIR}

# archlinux does not have apparmor
if hash apparmor_parser 2>/dev/null; then
    echo "=> Loading app armor profile"
    sudo apparmor_parser --replace --write-cache ./setup/start/docker-cloudron-app.apparmor
fi

# create dir structure
mkdir -p ${DATA_DIR}
cd ${DATA_DIR}
mkdir -p appsdata
mkdir -p boxdata/{box,mail,certs,mail/dkim/{localhost,foobar.com}}
mkdir -p platformdata/{addons/{mail/banner,},nginx/{cert,applications/dashboard},collectd/collectd.conf.d,logrotate.d,backup,logs/tasks,sftp/ssh,firewall,update,diskusage,oidc}
sudo mkdir -p /mnt/{cloudron-test-music,cloudron-test-music2} /media/cloudron-test-music  # volume test
sudo ln -sf /root /media/cloudron-test-music/root
sudo touch /media/cloudron-test-music/file

# put cert
echo "=> Generating a localhost selfsigned cert"
openssl req -x509 -newkey rsa:2048 -keyout platformdata/nginx/cert/host.key -out platformdata/nginx/cert/host.cert -days 3650 -subj '/CN=localhost' -nodes -config <(cat /etc/ssl/openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:*.localhost"))

cd "${source_dir}"

# clear out any containers if FAST is unset
if [[ -z ${FAST+x} ]]; then
    echo "=> Delete all docker containers first"
    docker ps -qa --filter "label=isCloudronManaged" | xargs --no-run-if-empty docker rm -f
    docker rm -f mysql-server
    echo "==> To skip this run with: FAST=1 ./run-tests"
else
    echo "==> WARNING!! Skipping docker container cleanup, the database might not be pristine!"
fi

# create docker network (while the infra code does this, most tests skip infra setup)
docker network create --subnet=172.18.0.0/16 --ip-range=172.18.0.0/20 --gateway 172.18.0.1 cloudron --ipv6 --subnet=fd00:c107:d509::/64 || true

# create the same mysql server version to test with
OUT=`docker inspect mysql-server` || true
if [[ "${OUT}" = "[]" ]]; then
    echo "=> Starting mysql-server..."
    # ulimit nofile is required to make it work on archlinux https://github.com/docker-library/mysql/issues/579#issuecomment-519495808
    docker run --init --name mysql-server --ulimit nofile=262144:262144 -e MYSQL_ROOT_PASSWORD=password -d mysql:8.0
else
    echo "=> mysql-server already running. If you want to start fresh, run 'docker rm --force mysql-server'"
fi

export MYSQL_IP=`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql-server`

echo "=> Waiting for mysql server ($MYSQL_IP) to be ready..."
while ! docker exec mysql-server mysqladmin ping -h"${MYSQL_IP}" --silent; do
    sleep 1
done

echo "=> Ensure local base image"
docker pull cloudron/base:4.2.0@sha256:46da2fffb36353ef714f97ae8e962bd2c212ca091108d768ba473078319a47f4

echo "=> Create iptables blocklist"
sudo ipset create cloudron_blocklist hash:net || true

echo "=> Ensure database"
docker exec mysql-server mysql -uroot -ppassword -e "ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';"
docker exec mysql-server mysql -uroot -ppassword -e "CREATE DATABASE IF NOT EXISTS box"

# do this before migrations! box code requires oidc files to exist which are imported in migrations
echo "=> Ensure dist/ exists"
if [[ ! -d "dashboard/dist" ]]; then
    (cd dashboard && npm run build)
fi

echo "=> Run database migrations"
BOX_ENV=test DATABASE_URL=mysql://root:password@${MYSQL_IP}/box node_modules/.bin/db-migrate up

TESTS=${DEFAULT_TESTS}

echo "=> Run tests with mocha"
BOX_ENV=test ./node_modules/.bin/mocha --bail --no-timeouts --exit -R spec ${TESTS}
