#!/bin/sh -e
#
# latest - list latest modified files in given directory/pwd - up to depth of 4
# dependencies: hi

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

count=5
# shellcheck disable=SC2249
case "$1" in
  -a | --all)
    all=true
    shift
    ;;
  --)
    shift
    ;;
  # -*)
  #   args="$1 $2"
  #   shift 2
  #   ;;
  [0-9])
    count=$(($1 * 10))
    shift
    ;;
esac

for f in "${@:-${PWD}}"; do
  # test "$#" -gt 1 && printf '\e[4m%b\e[0m\n' "${f}"
  test "$#" -gt 1 && { hi "${f}" || echo "${f}"; }
  # shellcheck disable=SC2312,SC2091
  find "${f}" -maxdepth 4 -printf '%.16T+ %P\n' \
    | sort -r | $(test "${all}" && echo "less" || echo "head -${count}")
done
