#!/bin/sh -e
#
# dud - "du for humans"
# note: optionally add -L to follow links
#
# ---
# source: https://github.com/mitchweaver/bin

usage() {
  >&2 printf 'Usage: %s [-L links] <directory>\n' "${0##*/}"
  exit 1
}

# shellcheck disable=SC2249
case ${1#-} in
  h | -h | --help)
    usage
    ;;
  l | L | -links)
    LINKS=L
    shift
    ;;
  -*)
    usage
    ;;
esac

if [ -d "${1:-.}" ]; then
  du -d"${LINKS-}" 1 -- "${1:-.}" 2>/dev/null \
    | sort -nr \
    | numfmt --from-unit=512 --to=iec --format="%.1f" \
    | head -n 20
    # | numfmt --from=auto --to=iec-i --suffix=B --format=%9.1f --from-unit=1024
elif [ -f "$1" ]; then
  du -a"${LINKS-}" "$1" \
    | numfmt --from-unit=512 --to=iec --format="%.1f"
fi
