#!/bin/sh
# Compile a class with a lambda and a string switch, decompile it with cfr,
# and compile and run the decompiled source again.
set -eu
cd "$AUTOPKGTEST_TMP"
mkdir src out dec re
cat > src/Hello.java <<'EOF'
import java.util.function.IntUnaryOperator;
public class Hello {
    static int pick(String s) {
        switch (s) { case "one": return 1; case "two": return 2; default: return 0; }
    }
    public static void main(String[] args) {
        IntUnaryOperator twice = x -> x * 2;
        System.out.println(twice.applyAsInt(pick(args.length > 0 ? args[0] : "two")));
    }
}
EOF
javac -d out src/Hello.java
cfr out/Hello.class --outputdir dec > cfr.log 2>&1
grep -q 'case "one"' dec/Hello.java || { echo "FAIL: no string switch"; cat dec/Hello.java; exit 1; }
javac -d re dec/Hello.java
[ "$(java -cp re Hello one)" = 2 ] || { echo "FAIL: recompiled class"; exit 1; }
cfr --help 2>&1 | grep -q outputdir
echo "ok: decompiled and recompiled"
