#!/bin/sh -Cue
#.help
# % [START..] [COND..]
#
# Find non-hidden files; similar to "find STARTs CONDs" except ignores hidden files and files in hidden directories.
#
# Similar to "find ... | grep -v /\\.", except should be more efficient by pruning hidden directories.
#
# All STARTs must not start with dash ("-") and all CONDs must start with dash.  If no START given, must use "--" before CONDs and the current directory is searched.

fatal() { rc="$1"; shift; printf %s\\n "${0##*/} error: $*" >&2 || true; exit "$rc"; }
nonfatal() { printf %s\\n "${0##*/}: $*" >&2 || true; }
while [ $# -gt 0 ]; do case "$1" in --) shift; break ;; -) break ;; -*) fatal 64 'unexpected option, possible missing START directory' ;; *) break ;; esac; done

case "$#" in
    0) set -- . ;;
    *)
        case "$1" in
            -*) set -- . "$@" ;;
            esac
        ;;
    esac

need_default() {
    while [ $# != 0 ]; do
        case "$1" in
            -delete|-exec|-execdir|-fls) return 1 ;;
            -fprint|-fprint0|-fprintf) return 1 ;;
            -ls|-ok|-okdir) return 1 ;;
            -print|-print0|-printf) return 1 ;;
            -prune|-quit) return 1 ;;
            esac
        shift
        done
    return 0
    }
default=
if need_default "$@"; then
    default=-print
    fi

star_newline="$(printf '*\n*')"
do_findr() {  # START [DISCARDED_START..] [COND..]
    start="$1"
    shift
    while case "${1:--}" in -*) false ;; esac; do
        shift
        done
    [ $# != 0 ] || set -- -true
    find "$start" -maxdepth 0 '(' "$@" ')'
    find "$start" -mindepth 1 \
        '(' -name "$star_newline" -o -name '.?*' ')' -prune -o \
        '(' "$@" ')' $default
    }

while case "${1:--}" in -*) false ;; esac; do
    do_findr "$@"
    shift
    done
