#! /bin/bash

# Copyright (C) 2013-2017, Red Hat, Inc.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html

# The first argument of this script is the location of the tests to run
# If no argument is given, the platform tests are run
tests=$1

# Location of test machinery
eclipse_testing_dir=$(cd $(dirname $(readlink -f $(which eclipse)))/../../share/eclipse-testing && pwd)
# Location of platform tests
platform_tests=$(cd $(dirname $(readlink -f $(which eclipse)))/../../share/java/eclipse-tests && pwd)

# If tests are not specified, then find the platform tests
if [ -z "$tests" ] ; then
  if [ ! -d "$platform_tests" ] ; then
    echo "No tests specified and platform tests are not installed."
    exit 2
  else
    tests=$(cd $platform_tests && pwd)
  fi
else
  if [ ! -d "$tests" ] ; then
    echo "Specified tests are not installed."
    exit 2
  fi
fi

# Tests might exist in a sub-directory
if [ -d "$tests/eclipse" ] ; then
  tests="$tests/eclipse"
fi

# Prepare test execution environment
mkdir target
cp -rp $eclipse_testing_dir/* target/
. target/label.properties
executionDir=$(pwd)/target/workarea/$buildId/eclipse-testing
mkdir -p $executionDir
cp -p target/library.xml target/JUNIT.XSL target/splitter.xsl target/*.txt $executionDir

# Create an SDK of all system OSGi bundles plus specified test bundles
$eclipse_testing_dir/testbundle/gatherBundles.sh "$(pwd)/target/sdk" "$tests"

# Gather extra IUs to install when running tests that have extra requirements
# specified in the pom.xml
$eclipse_testing_dir/testbundle/gatherExtraIUs.sh "$tests"

# If a test location was specified, prep for and use those tests
if [ -n "$1" ] ; then
  $eclipse_testing_dir/testbundle/prepRuntimeLocation.sh || exit 1
  $eclipse_testing_dir/testbundle/prepAllTestBundles.sh "$tests" || exit 1
fi

# Build a p2 repository from the constructed SDK
java -jar $(dirname $(readlink -f $(which eclipse)))/plugins/org.eclipse.equinox.launcher_*.jar \
 -nosplash \
 -application org.eclipse.equinox.p2.publisher.FeaturesAndBundlesPublisher \
 -metadataRepository file:$executionDir/testRepo \
 -artifactRepository file:$executionDir/testRepo \
 -source $(pwd)/target/sdk \
 -compress -append -publishArtifacts

# Get arch of running platform
if uname -m > /dev/null 2>&1; then
  arch=$(uname -m)
else
  arch=$(uname -p)
fi

# Massage arch for Eclipse-uname differences
case $arch in
  i[0-9]*86)
    arch=x86 ;;
  ppc64p7)
    arch=ppc64 ;;
  arm*)
    arch=arm ;;
esac

# Set some values expected by runtests.sh
export jvm=/usr/lib/jvm/java-1.8.0/jre/bin/java
export stableEclipseInstallLocation=$executionDir/platformLocation

# Create platform tarball used during testing
mkdir -p $stableEclipseInstallLocation
pushd $(dirname $(readlink -f $(which eclipse)))/.. 2>&1 >/dev/null
tar caf $stableEclipseInstallLocation/eclipse-SDK-${buildId}-linux-gtk-x86_64.tar.gz eclipse
cp -p $stableEclipseInstallLocation/eclipse-SDK-${buildId}-linux-gtk-x86_64.tar.gz $executionDir
popd 2>&1 >/dev/null

# Kick off tests
pushd target 2>&1 >/dev/null
./runtests.sh -os linux -ws gtk -arch $arch
popd 2>&1 >/dev/null
