#!/bin/bash -eu
# describe_interval - describe an interval in seconds

# See also python lib: humanize
# e.g. desc = humanize.precisedelta(seconds)
# e.g. desc = humanize.precisedelta(datetime.timedelta(seconds=seconds))
# I'm keeping this because it works as a bash function and should be lighter than running python.

describe_interval() {
	short=
	full=
	OPTIND=1
	while getopts "s" opt; do
		case "$opt" in
		s)	short=1
			;;
		f)	full=1
			;;
		esac
	done
	shift $((OPTIND-1))

	local d="$1"

	local n unit

	if [ "$d" -ge 86400 ]; then
		n=$[$d / 86400] unit=day
		d=$[$d % 86400]
	elif [ "$d" -ge 3600 ]; then
		n=$[$d / 3600] unit=hour
		d=$[$d % 3600]
	elif [ "$d" -ge 60 ]; then
		n=$[$d / 60] unit=minute
		d=$[$d % 60]
	else
		n="$d" unit=second
	fi
	if [ "$n" -gt 1 ]; then
		unit="${unit}s"
	fi
	if [ -n "$short" ]; then
		echo "$n${unit:0:1}"
	else
		echo "$n $unit"
	fi
}

if [ "$0" = "$BASH_SOURCE" ]; then
	describe_interval "$@"
fi
