#!/bin/bash
#!/usr/bin/env -iS bash
#   findscan
#   shellcheck disable=SC2317



set -aCuTE; echo 'set -aCuTE'

scr_lic='Apache 2 license'
scr_max_age_of_tmp_files='1 minutes ago'
scr_nm=./findscan
scr_proper_nm='Find scripts'
scr_protected_git_dir_1="${HOME}/MYPROJECTS"
scr_protected_git_dir_2="${HOME}/OTHERSPROJECTS"
scr_repo_nm=scf
scr_tcode="$(
  builtin printf '%(%F_%H%M%S)T' 2>&1
  )"
scr_version=1.0
export scr_lic scr_max_age_of_tmp_files scr_nm scr_proper_nm \
  scr_protected_git_dir_1 scr_protected_git_dir_2 scr_repo_nm \
  scr_tcode scr_version
scr_dirnm="${scr_repo_nm}.d" export scr_dirnm

SC_sevr=error
SC_shells='(sh|bash|dash|ksh)'
export SC_sevr SC_shells

fn_bndry=' ~~~ ~~~ ~~~ '
fn_lvl=0
export fn_lvl fn_bndry
unset Halt
halt=' '
declare -rx Halt halt # lower case for nounset
declare -nx nBL=BASH_LINENO nBS=BASH_SOURCE nF=FUNCNAME nL=LINENO

# "Setting LC_CTYPE without also setting the other LC_* variables is not always well supported." - Ste'phane Chazelas
unset LC_ALL || : "${Halt:?}"
LC_COLLATE="C.UTF-8" LC_CTYPE="C.UTF-8"     # for predictable sorting
LC_NUMERIC="en_US.UTF-8"                    # for commas in large numbers
export LC_COLLATE LC_CTYPE LC_NUMERIC

GREP_COLORS='mt=01;43'
export GREP_COLORS
unset CDPATH

rand_i="${RANDOM}"
rand_uniq_str="${scr_repo_nm}.${$}.${rand_i}"
rand_lock_nm="${rand_uniq_str}.lock.d"
rand_f_nm="${rand_uniq_str:?}.xtr"
export rand_i rand_uniq_str rand_lock_nm rand_f_nm

# Undocumented CLI option
if [[ "$*" =~ rm_stale ]]
then
  unset x
  rm_stale=y
  export rm_stale

  while read -rd '' pp
  do
    array+=( "$pp" )
  done < <(
    printf '%s\0' "$@" |
      grep -vz rm_stale
    )
fi

_erx() {
  local exit_code="$?"
  : '_erx BEGINS' "${fn_bndry}" "${fn_lvl}>$(( ++fn_lvl ))"
  echo -e Error: "$@" >&2
  exit "${exit_code}"
  : '_erx ENDS  ' "${fn_bndry}" "${fn_lvl}>$(( --fn_lvl ))"
}
declare -fx _erx
declare -t _erx


# Sub-scripts
# Note: Exit traps don't print accurate trace data in PS4; so use a function

# Processes positional parameters as they were passed to source

function _post_src {
  # enable xtrace and "verbose commands"
  if [[ "$6" == y ]]
  then
    declare -ax verb=(-v --)
  # otherwise, neutralize the unused "verbose commands" syntax
  else
    declare -ax verb=(--)
  fi

  # enable xtrace and "verbose commands"
  if [[ "$5" == y ]]
  then
    set -x
  fi

  # after `source` returns, `exit` the calling shell with lineno $1.
  if [[ "$4" == y ]]
  then
    # prints script name and line number of script where function's executed
    : called_at: "$1:$2"

    # prints line number of script where function's executed
    exit -1
  fi

  # reset the color
  printf '%b' "\e[m"
}
declare -fx _post_src
declare -t _post_src

#set -T
#set -x

#
# Description                                 # -source- subscript                    #
#: "${nBS[0]}:${nL}"; : 'Required programs' ;  . './reqdcmds.bash'   "${nL}"   n n n; #exit "$nL"
#: "${nBS[0]}:${nL}"; : 'Verify PATH'       ;  . './path-ver.bash'   "${nL}"   n n n; #exit "$nL"
#: "${nBS[0]}:${nL}"; : 'ID and sudo'       ;  . './id_sudov.bash'   "${nL}"   n n n; #exit "$nL"
# So PS4 is on whenever xtrace is on, and DEBUG trap is always on once defined: 2023-06-18 0840
 : "${nBS[0]}:${nL}"; : 'Enable debugging'  ;  . './gardning.bash'   "${nL}"   n y y;  exit "$nL"
 : "${nBS[0]}:${nL}"; : 'Locks'             ;  . './lock-fds.bash'   "${nL}"   y y y;  exit "$nL"
 : "${nBS[0]}:${nL}"; : 'Verify DACs'       ;  . './dacs-ver.bash'   "${nL}"   y y y;  exit "$nL"
 : "${nBS[0]}:${nL}"; : 'Verify ACLs'       ;  . './acls-ver.bash'   "${nL}"   y y y;  exit "$nL"
 : "${nBS[0]}:${nL}"; : 'Option Parsing'    ;  . './cli-opts.bash'   "${nL}"   y y y;  exit "$nL"
 : "${nBS[0]}:${nL}"; : 'Storing findings'  ;  . './harddisk.bash'   "${nL}"   y y y;  exit "$nL"
#   ^--Purpose                                 ^--dot        nameref--^ xtrace---^     ^--die
#                                                   ^--filename          exit--^   ^--verbose


  # <> Obligatory debugging block
  #_xtrace_
  #: "${nBS[0]}:${nL} ${nBS[1]}:${nBL[0]}"
  exit "${nL}"
  set -x

