#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename "$0")
CMDVER="1.0"
CMDSTR="$CMD v$CMDVER (2026-01-01)"

export LC_ALL=C

usage()
{
	echo "
== $CMDSTR = compute file usage by match ==

usage:	$CMD dir filematch(s)...

- filematches are shell glob (use quotes to avoid unintended expansion)
- filematches are case insensitives
- WARNING: read errors (like unaccessible files or directories) are silently
  ignored, run it with right privileges to have correct counting

examples:

	$CMD . '*.mov' '*.mp4'

	$CMD Documents '*.jpg' '2025-01*.pdf'
" >&2
	exit 1
}

findfiles()
{
	for match
	do
		echo -en "$match " >&2
		findmatch "$match"
	done
}

findmatch()
{
	find $dir -iname "$1" -type f -print0 2>/dev/null
}


compute_apparent()
{
	du --human-readable --apparent-size \
		--total --summarize --files0-from=- | tail -1 \
		| sed -e 's/[[:space:]].*//'
}

compute_real()
{
	du --human-readable \
		--total --summarize --files0-from=- | tail -1 \
		| sed -e 's/[[:space:]].*//'
}

# (MAIN)

[ $# -lt 2 ] && usage

dir=$1; shift

# args len for proper display
#
printf_size=0
for match
do
	match=${#match}	# variable len
	[ $match -gt $printf_size ] && printf_size=$match
done


echo -en "  computing apparent usage ... " >&2
apparent=$(findfiles "$@" | compute_apparent)
echo "ok" >&2

echo -en "  computing real usage ... " >&2
realsize=$(findfiles "$@" | compute_real)
echo "ok" >&2

echo
printf "%-${printf_size}s  %6s %8s %8s\n" "DETAILS" "COUNT" "USAGE" "ON_DISK"
for match
do
	printf " %-${printf_size}s  " "$match"
	count=$(find $dir -iname "$match" -type f 2>/dev/null | wc -l)
	printf " %6d" $count

	usage=$(findmatch "$match" | compute_apparent)
	printf " %8s" "$usage"

	usage=$(findmatch "$match" | compute_real)
	printf " %8s" "$usage"
	echo
done

echo
echo "TOTAL USAGE"
printf " %-10s %s\n" "apparent" "$apparent"
printf " %-10s %s\n" "real" "$realsize"
echo

exit 0
