#!/bin/bash

# Test build rules against several Magento versions 
# to find possible false positives. 
# 
# Initial version: Fabio Ros
# Conditional execution to speed up Travis: Willem de Groot

# This will exit as soon as a child exits with code > 0
set -e 

# For debugging:
# set -x

echo "Exiting if we are not running under Python 3..."
python --version |& grep 'Python 3' || exit 0

# echo "Did rules files change compared to master?"
# git diff --name-only $TRAVIS_COMMIT_RANGE |& egrep 'build.+yar$' || exit 0

echo 'All conditions green, now testing ruleset against multiple Magento versions!'

MAGENTO_VERSIONS="
    1.4.2.0
    1.9.3.1
"

ROOT=/tmp

for VERSION in $MAGENTO_VERSIONS;
do
	echo "Testing Magento $VERSION ..."
	DEST="$ROOT/$VERSION"
	mkdir -p $DEST
	curl -Lsk https://github.com/OpenMage/magento-mirror/archive/$VERSION.tar.gz | tar xz -C $DEST --strip-components=1 
	YARA_OUTPUT=$(yara -r build/mwscan.yar $DEST)
	if [[ $YARA_OUTPUT ]]; then
		echo "Test for $VERSION failed at $DEST"
		echo $YARA_OUTPUT
		exit 1;
	fi
done
