#!/bin/bash

set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit

if ! test -d "${1:-}" && ! test -f "${1:-}" ; then
   true "ERROR: not a folder or file: ${1:-}"
   exit 1
fi

opts=(
   --exclude SC2086
   --exclude SC2016
   --exclude SC2034
   --exclude SC2129
   --exclude SC2004
   --exclude SC2088
   --exclude SC2015
   --exclude SC2068
   --exclude SC2001
)

while IFS= read -r -d '' file_name; do
   true "file_name: $file_name"
   if ! test -f "$file_name" ; then
      continue
   fi
   ## Match the shebang only on the first line; a file that just
   ## mentions `#!/bin/bash` in a comment shouldn't qualify.
   if ! head -n 1 -- "$file_name" | grep -q -- '^#!/bin/bash' ; then
      continue
   fi
   if shellcheck "${opts[@]}" "$file_name" ; then
      continue
   fi
   file_name_short="${file_name##*/}"
   true "press y to open kate. enter to continue"
   read -r temp
   if test "$temp" = "y" ; then
      kate "$file_name" >/dev/null 2>&1
   fi
done < <(find "${1:-}" -type f -not -iwholename '*.git*' -not -iwholename '*changelog.upstream*' -print0)

true "INFO: End."
