#!/bin/sh

usage() {
  printf '%s\n' "\
  $(basename "$0")
    calculates info about the number of lines in files listed on standard in.
    columns are Row Number, File, Percent Total, Cumulative Percent Total.
"
  exit 2
}

# case "$1" in
#   1) : continue ;;
#   *) printf '%s\n' "Usage: $(basename "$0")" '' \
#     'Calculates info about the number of lines in files listed on standard in.' \
#     'Columns are Row Number, File, Percent Total, Cumulative Percent Total.'
#   exit 2;
# esac

main() {

  # expects a list of file names on standard in
  xargs wc -l \
    | awk '
    /total/ { total = $1; }
    { counts[$2] = $1; }
    END { for (k in counts) { print k, counts[k]*100/total; } }' \
    | sort -rn -k2 \
    | grep -v total \
    | {
      echo 'RowNumber File PercentTotal CumulativePercentTotal'
      awk '{ total += $2; print $0, total; }' | nl
    } \
    | column -t \
    | ${PAGER:-less}
}

case "$#" in
  0)
    fd "${@:-.}" --exclude=.git --type=file | main
    ;;
  *)
    if test -d "$1"; then
      fd . "${@:-.}" --exclude=.git --type=file | main
    else
      main
    fi
    ;;
esac
