#!/bin/bash
echo "Tip: you can rebuild individual tests by calling this script like"
echo "./make-tamarin ecma3/Boolean/e15_6_4_3_2.as"
echo "or ./make-tamarin ecma3/Boolean"

TAMARIN=${TAMARIN:-tamarin}
ASC=${ASC:-`pwd`/asc.jar}
AVM=${AVM:-${TAMARIN}/objdir/shell/avmshell}
if [[ ! -d $TAMARIN ]]; then
  echo "Directory tamarin not found, please run"
  echo "hg clone http://hg.mozilla.org/tamarin-redux tamarin"
  echo "to download the source."
  exit 1
fi
if [[ ! -f $ASC ]]; then
  echo "File asc.jar not found, please run"
  echo "wget ftp://ftp.mozilla.org/pub/js/tamarin/builds/asc/latest/asc.jar"
  exit 1
fi

if [[ `java -jar $ASC | sed -n -e 's/version [^ ]* build \(.*\)/\1/p'` -lt 18513 ]]; then
  echo "File asc.jar is too old, please run"
  echo "wget ftp://ftp.mozilla.org/pub/js/tamarin/builds/asc/latest/asc.jar"
  exit 1
fi

export CUR=`pwd`

printCompileErrorAndExit()
{
    echo "Compiling tests failed. Make sure you have the latest tamarin-redux and asc.jar from http://hg.mozilla.org/tamarin-redux and ftp://ftp.mozilla.org/pub/js/tamarin/builds/asc/latest/asc.jar, respectively."
    exit 1;
}

# compile tests from Tamarin before commit 7555
makeTamarin1()
{
    (cd $TAMARIN/test/acceptance && \
    rm -f ats_temp.abc ats_temp.as shell.as && \
    grep USES_SWFVERSION . -R --files-with-matches | xargs --no-run-if-empty rm && \
    echo "override| -optimize -in $CUR/tamarin-lightspark.as -AS3 -swf 200,200 -in ./ats_temp.as" > dir.asc_args && \
    ./runtests.py --asc $ASC --builtinabc ../../generated/builtin.abc --shellabc ../../generated/shell_toplevel.abc --ats --atsdir $CUR/tamarin-SWF $1) \
    || printCompileErrorAndExit;
}

# compile tests from Tamarin commit 7555 and later
makeTamarin2()
{
    if [[ ! -f $AVM ]]; then
        echo "Set shell variable AVM to point to avmshell executable"
        exit 1
    fi

    java -jar $ASC -import $TAMARIN/generated/builtin.abc quit.as

    cd $TAMARIN/test/acceptance
    compiled=$(./runtests.py --asc $ASC --avm $AVM --builtinabc ../../generated/builtin.abc --shellabc ../../generated/shell_toplevel.abc --rebuildtests $1)
    if [[ $? -ne 0 ]]; then
        printCompileErrorAndExit;
    fi

    # The first sed selects lines starting with "compiling" unless the
    # following lines starts with "Excluding".
    #
    # The second sed filters out some strange cases (TODO: check
    # these!).
    echo "$compiled" | \
        sed -n '/^compiling/{N; /\nExcluding/ b excluded; P; D; b; :excluded d}' | \
        sed -n '/ascompiling\|mmgc\/outofmemory\.as\|mmgc\/memlimit\.as\|bug_515935.as\|abc_$/!p' | \
        sed -e 's/compiling \(.*\)/\1/' \
            -e 's/\(.*\.\)[a-z]\+$/\1abc/' | \
        xargs -L 1 bash -c 'linkTamarinTest $0'
    cd -
}

# Sort classes in $TEST_SUPPORT in dependecy order: put interface
# classes first and sort other classes alphabetically.
function sortSupportClasses() {
    local interfaces=$(echo "$TEST_SUPPORT" | tr " " "\n" | grep Interface)
    local classes=$(echo "$TEST_SUPPORT" | tr " " "\n" | grep -v Interface | sort)
    TEST_SUPPORT=$(echo "$interfaces $classes" | tr "\n" " ")
}

# Create a .swf for a Tamarin test case by figuring out which .abc
# files belong to the test case and merging them.
# Input: the name of test case's main .abc or .abs file
function linkTamarinTest() {
    shouldExcludeTest "$1"
    local exclude_test=$?

    if [[ -f "$1" && "$exclude_test" -eq 0 ]]; then
        echo "Linking $1"

        mkdir -p $CUR/tamarin-SWF/$(dirname $1)
        OUTPUT_SWF="$CUR/tamarin-SWF/${1/%ab[cs]/swf}"

        COMMON="Assert.abc Utils.abc"
        ABS_SUPPORT=$(if [[ -f ${1/%abc/abs} ]]; then echo abcasm/abs_helper.abc; fi)
        TEST_SUPPORT=$(if [[ -d ${1/%.abc/} ]]; then echo $(ls ${1/%.abc/}/*.abc 2> /dev/null); fi)
        sortSupportClasses
        ABC_FILES="$COMMON $TEST_SUPPORT $ABS_SUPPORT $1 $CUR/quit.abc"

        $CUR/../tools/mergeABCtoSWF $ABC_FILES -o $OUTPUT_SWF
    elif [[ "$exclude_test" -ne 0 ]]; then
	echo "Excluding $1"
    fi
}

# Exclude testcases that need imports from shell_toplevel (avmplus,
# avmshell packages) until versioned identifiers are supported.
function shouldExcludeTest() {
    local asfile=${1/ab[cs]/as}
    grep --quiet --no-messages "import avmplus\|import avmshell" "$asfile"
    if [[ $? -eq 0 ]]; then
	return 1
    else
	return 0
    fi
}

export -f linkTamarinTest sortSupportClasses shouldExcludeTest

if [[ "x$1" = "x" ]]; then
    rm -rf tamarin-SWF/*
fi

# Tamarin testing framework changed in commit 7555 in a way that is
# incompatible with previous version of this script. Use Assert.as, a
# file introduced in that commit, to detect the new test setup.
if [[ -f $TAMARIN/test/acceptance/Assert.as ]]; then
    makeTamarin2;
else
    makeTamarin1;
fi
