#!/bin/bash

set -e

DIR=${1?Error: no extension dir given}
VENDOR_BIN=${2}

MAGENTO_PATH=$(cd $DIR/../../.. && pwd)
EXTENSION_PATH=$MAGENTO_PATH/vendor/algolia/algoliasearch-magento-2
THIS="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

${VENDOR_BIN}magento2-update

GREEN='\033[0;32m'
NC='\033[0m' # No Color

printf "\n1. Analysing ${GREEN}Coding Style${NC} \n\n"

${VENDOR_BIN}php-cs-fixer fix --allow-risky=yes $EXTENSION_PATH --dry-run

printf "\n2. Analysing ${GREEN}PHP Compatibility${NC} \n\n"

PHP_COMPATIBILITY_PATH=$THIS/../../../vendor/phpcompatibility/php-compatibility

if [ ! -d "$PHP_COMPATIBILITY_PATH" ]; then
  PHP_COMPATIBILITY_PATH=$THIS/../../../phpcompatibility/php-compatibility
fi

${VENDOR_BIN}phpcs --config-set installed_paths $PHP_COMPATIBILITY_PATH

${VENDOR_BIN}phpcs -p --standard=PHPCompatibility --runtime-set testVersion 7.4- --runtime-set ignore_warnings_on_exit true --ignore=vendor/*,*/view $EXTENSION_PATH
