#!/usr/bin/env bash

set -e

if [[ `basename "$PWD"` == *"-bundle" ]]; then

    echo "Running functional tests ..."
    curl -sS https://getcomposer.org/installer | php -- --filename=composer

    BUNDLE_PATH=`pwd`
    BUNDLE_NAME=endroid/"$(basename "$BUNDLE_PATH")"
    APPLICATION_PATH=`pwd`/vendor/endroid/quality/application
    COMPOSER="php -d memory_limit=-1 composer"
    SYMFONY_VERSION=^5.0

    if [ ! -z $1 ]; then
        SYMFONY_VERSION=$1
    fi

    rm -rf $APPLICATION_PATH

    $COMPOSER create-project --no-progress symfony/website-skeleton:$SYMFONY_VERSION $APPLICATION_PATH

    cp composer $APPLICATION_PATH/composer
    cp -r tests/application "$(dirname "$APPLICATION_PATH")"
    mkdir -p $APPLICATION_PATH/vendor/endroid
    rsync -a --exclude tests --exclude vendor $BUNDLE_PATH $APPLICATION_PATH/vendor/endroid

    (
        cd $APPLICATION_PATH
        $COMPOSER config extra.symfony.allow-contrib true
        $COMPOSER config prefer-stable true
        $COMPOSER config repositories.$BUNDLE_NAME path $APPLICATION_PATH/vendor/$BUNDLE_NAME
        $COMPOSER require $BUNDLE_NAME @dev
        bin/phpunit
    )

fi
