#!/bin/sh
#
# walk - walk recursively list FILE (default: ./)
#
# Usage: walk [-adfx | -n COUNT] FILE ... |  CMD
#
# ---
# source: https://github.com/phillbush/home

# shellcheck disable=SC2250

usage() {
  printf "usage: %s [-dfx] [-n COUNT] FILE ...\n" "${0##*/}" >&2
  exit 2
}

if command -v gfind >/dev/null 2>&1; then
  find() (
    gfind "$@"
  )
fi

hide=1
filter=
min=
max=
while getopts adfn:x opt; do
  case "${opt}" in
    a)
      hide=
      ;;
    [dfx])
      filter="${opt}"
      ;;
    n)
      if test -z "${max}"; then
        max="${OPTARG}"
      else
        min="${max}"
        max="${OPTARG}"
      fi
      ;;
    *)
      usage
      ;;
  esac
done
shift "$((OPTIND - 1))"

test "$#" -eq 0 && set -- .
for arg; do
  set -- "$@" -f "$arg"
  shift
done
set -- "$@" --

case "${filter}" in
  "") ;;
  d)
    set -- "$@" -type d
    ;;
  f)
    set -- "$@" -type f
    ;;
  x)
    set -- "$@" -type f -perm -111
    ;;
  *)
    exit 1
    ;;
esac
test -n "${min}" && set -- "$@" -mindepth "$min"
test -n "${max}" && set -- "$@" -maxdepth "$max"
test -n "${hide}" && set -- "$@" -not -path "*/.*"

exec find "$@"
