#!/usr/bin/env bash
set -u

gitgrep()
{
    local pathspecs=('bash_completion' 'completions-fallback/*.bash'
        'completions-core/*.bash' 'test/' 'bash_completion.d/' ':!:test/runLint'
        ':!:test/docker/' ':!:test/test-cmd-list.txt')
    local filter_list
    read -ra filter_list <<<"$filter_out"
    pathspecs+=("${filter_list[@]/#/":!:"}")
    local out=$(
        git grep -I -P -n "$1" -- "${pathspecs[@]}" |
            grep -Pv ':[[:space:]]*(#|""")' # Ignore bash and python comments
    )
    if [[ $out ]]; then
        printf '***** %s\n' "$2"
        printf '%s\n\n' "$out"
    fi
}

unset -v CDPATH
if ! cd "$(dirname "$0")/.."; then
    echo 'test/runLint: failed to cd into the working tree of bash-completion' >&2
    exit 1
fi

cmdstart='(?:(?:^|[;&|()])[[:blank:]]*|\b(?:if|then|elif|else|for|while|until|do|coproc|command|builtin)[[:blank:]]+|{[[:blank:]]+)'
filter_out=

# Note: Since we started to use _comp_{awk,tail}, we do not have to care about
# the small feature set of Solaris awk/tail anymore.

gitgrep "${cmdstart}(_comp_)?awk\b.*\[:[a-z]*:\]" \
    'awk with POSIX character class not supported in mawk-1.3.3-20090705 (Debian/Ubuntu)'

gitgrep "$cmdstart"'sed\b.*\\[?+]' \
    'sed with ? or +, use POSIX BRE instead (\{m,n\})'

gitgrep "$cmdstart"'sed\b.*\\\|' \
    "sed with \|, use POSIX BRE (possibly multiple sed invocations) or another tool instead"

gitgrep '(?:'"$cmdstart"'(?:sed|awk|grep)\b| =~ ).*\\[<>]' \
    "\< or \> in regular expressions. Anchors \< and \> are not defined by POSIX.  No POSIX alternatives."

# Note: awk defines \b to be an escape sequence for <backspace>
gitgrep '(?:'"$cmdstart"'(?:sed|grep)\b| =~ ).*\\b' \
    "\b in regular expressions. Anchor \b is not defined by POSIX.  No POSIX alternatives."

# TODO: really nonportable? appears to work fine in Linux, FreeBSD, Solaris
#gitgrep $cmdstart'sed\b.*;' \
#    'sed with ;, use multiple -e options instead (POSIX?) (false positives?)'

gitgrep "$cmdstart"'sed\b.*[[:space:]]-[^[:space:]]*[rE]' \
    'sed with -r or -E, drop and use POSIX BRE instead'

gitgrep "$cmdstart"'[ef]grep\b' \
    '[ef]grep, use grep -[EF] instead (historical/deprecated)'

# TODO: $ in sed subexpression used as an anchor (POSIX BRE optional, not in
#       Solaris/FreeBSD)

filter_out="test/fixtures/_longopt/grep--help.txt" gitgrep "${cmdstart/|command|/|}"'(grep|ls|sed|cd)(\s|$)' \
    'invoke grep, ls, sed, and cd through "command", e.g. "command grep"'

gitgrep "${cmdstart/|command|/|}"'(?:awk|tail)(\s|$)' \
    'invoke awk/tail through "_comp_{awk,tail}"'

gitgrep '@\([^()|$]+\)' \
    '@(...) may not be needed when ... does not contain |.'

#------------------------------------------------------------------------------
# Bash pitfalls/styles/compatibilities (which are not detected by shellcheck)

gitgrep "$cmdstart"'unset [^-]' 'Explicitly specify "unset -v/-f"'

filter_out="test/config/bashrc" gitgrep "$cmdstart"'((set|shopt)\s+[+-][a-z]+\s+posix\b|(local\s+)?POSIXLY_CORRECT\b)' \
    'fiddling with posix mode breaks keybindings with some bash versions'

gitgrep '\$\{([^{}\n]|\{.*\})+/([^{}\n]|\{.*\})+/([^{}"\n]|\{.*\})*\$.*\}' \
    '$rep of ${var/pat/$rep} needs to be double-quoted for shopt -s patsub_replacement (bash >= 5.2) [see Sec. of patsub_replacement in doc/styleguide.md]'

gitgrep '"([^"\n]|\\.)*\$\{([^{}\n]|\{.*\})+/([^{}\n]|\{.*\})+/([^{}"\n]|\{.*\})*"([^{}"\n]|\{.*\})*\$.*\}' \
    '$rep of "${var/pat/"$rep"}" should not be quoted for bash-4.2 or shopt -s compat42 (bash >= 4.3) [see Sec. of patsub_replacement in doc/styleguide.md]'
