#!/bin/sh

set -ev

echo $0

# This test ensures that exhaustive will fail if we do not include in the
# facfile an input that is generated by another rule.

rm -rf $0.dir
mkdir $0.dir
cd $0.dir

cat > top.fac <<EOF
| echo foo > foo

| cat foo extra > bar

| cp foo baz
< foo
EOF

echo extra > extra

git init
git add top.fac extra

if ${FAC:-../../fac} --exhaustive &> fac.out; then
    cat fac.out
    echo this should fail for one reason or another
    exit 1
fi
cat fac.out

if ${FAC:-../../fac} --exhaustive &> fac.out; then
    cat fac.out
    echo this should fail due to exhaustive strictness
    exit 1
fi

cat fac.out
grep 'missing dependency' fac.out
grep 'requires' fac.out

cat > top.fac <<EOF
| echo foo > foo

| cat foo extra > bar
< foo

| cp foo baz
< foo
EOF

if ${FAC:-../../fac} --exhaustive &> fac.out; then
    cat fac.out
    echo this should fail due to exhaustive strictness
    exit 1
fi

cat fac.out
grep 'missing dependency' fac.out
grep 'requires extra' fac.out

cat > top.fac <<EOF
| echo foo > foo

| cat foo extra > bar
< extra

| cp foo baz
< foo
EOF

if ${FAC:-../../fac} --exhaustive &> fac.out; then
    cat fac.out
    echo this should fail due to exhaustive strictness
    exit 1
fi

cat fac.out
grep 'missing dependency' fac.out
grep 'requires foo' fac.out

cat > top.fac <<EOF
| echo foo > foo

| cat foo extra > bar
< foo
< extra

| cp foo baz
< foo
EOF

if ${FAC:-../../fac} --exhaustive &> fac.out; then
    cat fac.out
    echo this should fail due to exhaustive strictness
    exit 1
fi

cat fac.out
grep 'missing output' fac.out
grep 'builds foo' fac.out
grep 'failed due to missing outputs' fac.out

cat > top.fac <<EOF
| echo foo > foo
> foo

| cat foo extra > bar
< foo
< extra

| cp foo baz
< foo
EOF

${FAC:-../../fac} --exhaustive

grep foo bar

# now that we have exhaustive knowledge, we should be able to run blind!

rm bar foo baz

${FAC:-../../fac} --blind

grep foo foo
grep foo bar
grep foo baz

${FAC:-../../fac} --clean --blind

${FAC:-../../fac} --blind

grep foo foo
grep foo bar
grep foo baz

exit 0
