#!/bin/bash
#  $HeadURL: http://dione.no-ip.org/svn/ade/tags/1.7.6/lib/tests/bin/ADESTDTEST302_shell_scripts_dont_return_dollarquestionmark_if_negated.sh $ $LastChangedRevision: 6247 $

MESSAGE="It looks like you are inverting the return code from a ADE-style call.\nPlease check that you are not then using \$? in the same stanza. If not,\nthen append a comment with the text 'ADESTDTEST302' to the line."

#  Try to detect code like the following:
#
#      if ! some_function_take_a_stack $ERRSTACK_REF; then
#          return $?
#      fi
#
#  This is invalid, because '!' means that $? isn't the return
#  value from some_function_take_a_stack(), but is the inverse
#  of it (making it useless as a return value).
#
#  Unfortunately, this is just too complicated to detect (it
#  requires parsing possibly-nested 'if' statements). So instead
#  we look for inversions that don't mention 'ADESTDTEST302'
#  on the same line.

#  Detect inversion of return values. 
#  which will not affect the stack. (The backslash is 
#  a way to prevent the pattern itself matching the pattern.)
egrep -n 'if \! .*ERRSTACK_REF' $(find $ADETEST_MODROOT -type f -name '*.sh') | grep -v ADESTDTEST302 | sed "s/\$/\n$MESSAGE\n/"

#  This test gets a bit complicated if we try to make it return a
#  sensible return code, and given that the reference file is 
#  empty, it would be superfluous. Instead, just make the return
#  code consistent.
true
