#!/usr/bin/env bash
# recent -- show recently modified files in the current directory

. lib.bash || exit

dirs=()
opts=()
today=1
days=1

for arg; do
	if [[ $arg =~ ^-([0-9]+)$ ]]; then
		days=${BASH_REMATCH[1]}
	elif [[ $arg == -* ]]; then
		die "unknown option '$arg'"
	elif [[ $arg =~ ^\+([0-9]+)[Mm]$ ]]; then
		opts+=(-size +${BASH_REMATCH[1]}M)
	else
		dirs+=("$arg")
	fi
done

if (( ! ${#dirs[@]} )); then
	dirs=(.)
fi

if (( today )); then
	opts+=(-daystart)
	opts+=(-mtime -$days)
	if (( days == 1 )); then
		echo "Files modified today:"
	else
		echo "Files modified in the last $days days:"
	fi
fi

opts+=(-printf '%h/%f  \033[2m[%kk]\033[m\n')

# -mtime x
#   match if mtime is between {x*24 hours ago} and {(x+1)*24 hours ago}
# e.g. -mtime 0
#   match if mtime is between {0 hours ago} and {24 hours ago}
# -mtime -x
#   match if mtime is newer than {(x+1)*24 hours ago}
# e.g. -mtime -1
#   match if mtime is newer than {24 hours ago}

for dir in "${dirs[@]}"; do
	xdir=$(realpath "$dir")
	xdir=${xdir/#"$HOME/"/"~/"}
	find "$dir" -xtype f "${opts[@]}" | sed 's!^\./!!' | treeify -ffr "$xdir"
done
