#!/bin/bash

# Ensure that phpcs is available on the system.

if ! [ -x "$(command -v phpcs)" ]; then
    echo "This script needs phpcs installed to run!"
    exit 1;
fi

# Load environment and load list of relevant instances.

SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"

# Run PHPCS

PHPCS_RESULT=$(phpcs --standard=~/Rules/md_Rules/md_phpcs_rules.xml "$SCRIPT_DIR/../*.php" | grep " | " | wc -l)

for i in "$SCRIPT_DIR/../"*; do
    if [ "$(echo $i | grep -v "musdb" | wc -l)" = "0" ]; then
        continue
    fi
    PHPCS_RESULT=$(echo "$(phpcs --standard=~/Rules/md_Rules/md_phpcs_rules.xml $i/*.php | grep " | " | wc -l) + $PHPCS_RESULT" | bc)
done

# PHPCS_RESULT=$(echo "$(phpcs --standard=~/Rules/md_Rules/md_phpcs_rules.xml "$SCRIPT_DIR/../*/*.php" | grep " | " | wc -l) + $PHPCS_RESULT" | bc)

echo "phpcs-errors:$PHPCS_RESULT";

# Run PHPUnit

PHPUNIT_TEST_BASE_DIR="/var/www/vhosts/museum-digital.de/about.museum-digital.de"

$(php $PHPUNIT_TEST_BASE_DIR/vendor/bin/phpunit --stderr -c $PHPUNIT_TEST_BASE_DIR/tests/config.xml --testdox $PHPUNIT_TEST_BASE_DIR/tests)
PHPUNIT_EC=$?
echo ""
if [ "$PHPUNIT_EC" = "1" ]; then
    echo "phpunit-status:failing";
else
    echo "phpunit-status:successful";
fi
