#!/bin/sh

make_files()
{
    tmp=$AUTOPKGTEST_TMP

    cat <<EOF > $tmp/wrong_format.sh
#!/bin/sh

if [ -d someDir ];then
    echo "directory exists"
fi
EOF
}

check_format()
{
    test="check_format"
    file="$AUTOPKGTEST_TMP/wrong_format.sh"

    if [ "`shfmt -i 4 -l $file`" != "$file" ]; then
        echo "ERROR: [$test] shfmt not found format error on $file."
        exit 1
    fi
}

check_bashism()
{
    test="check_bashism"

    if [ "`echo 'foo=(1 2)' | shfmt -ln bash`" != "foo=(1 2)" ]; then
        echo "ERROR: [$test] failed on parse Bash."
        exit 1
    fi

    expect_part="arrays are a bash/mksh feature"
    echo 'foo=(1 2)' | shfmt -ln posix 2>&1 | grep "$expect_part" > /dev/null
    if [ $? != 0 ]; then
        echo "ERROR: [$test] failed on parse POSIX."
        exit 1
    fi
}

check_syntax()
{
    test="check_syntax"

    echo "if 0; then" | shfmt 2> /dev/null
    if [ $? != 1 ]; then
        echo "ERROR: [$test] must found a syntax error."
        exit 1
    fi

    echo "if 0; then echo; fi" | shfmt > /dev/null 2>&1
    if [ $? != 0 ]; then
        echo "ERROR: [$test] must NOT found a syntax error."
        exit 1
    fi
}

## main
make_files
check_format
check_bashism
check_syntax
