#!/bin/sh
# autopkgtest check: Run Groovy code from a script
# (C) 2021 Pierre Gruet.
# Author: Pierre Gruet <pgt@debian.org>

set -e

WORKDIR=$(mktemp -d)
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
cd $WORKDIR

# This should print "2 3 4" thanks to the loop, and then a linefeed.
cat <<EOF >test.groovy
2.upto(4) {print "\$it "}
print "\n"
EOF

cat <<EOF >Test.java
import ch.grengine.Grengine;
import java.io.File;

public class Test {
    public static void main(String[] args) {
        Grengine gren = new Grengine();
        gren.run(new File("test.groovy"));
    }
}
EOF

javac -cp /usr/share/java/grengine.jar:. Test.java
java -cp /usr/share/java/grengine.jar:. Test | grep -q "^2 3 4 $"

if [ $? -ne 0 ]; then
  exit 1
fi

