
if [ -z "$__TINY_BITS__" ]; then
	__TINY_BITS__=0
fi

if [ "$__TINY_BITS__" -eq 0 ]; then
	__TINY_BITS__=1


#+
# NAME:
#	tiny_note
# PURPOSE:
#	Prints a string to stdout.
# INPUTS:
#	$1		user message
#	$2		positive integer indicating the "verbosity" threshold.
#			If get_options is used then messages with threshold $2=1
#			become visible when --verbose, or --debug=1 is set.
#			(see also tiny_say below).
#			If the threshold is set to 'n' then the message becomes
#			visible if $2 is set to n or larger ($2 >= n)
#	$3		single char: S,I,E,W. The default is 'I'
# OUTPUTS:
#	Modified string $1 to stdout
# NOTE:
#	A collection of functions implement special cases of tiny_note:
#
#	For echoing to stdin
#	--------------------
#	Always echo (i.e. ignore VERBOSE or DEBUG setting):
#		tiny_echo
#
#	For echoing to stderr
#	---------------------	
#		tiny_message		behaves exactly as tiny_note
#
#	Always echo (i.e. ignore VERBOSE or DEBUG setting):
#		tiny_yell			used to print informational messages
#							that must always be visible
#		tiny_warn			used to print warning messages that
#							must always be visible
#		tiny_die			echos and exits with code 1.
#							used to signal fatal condition
#		tiny_done			echos and exit with code 0.
#							used to signal normal completion. 
#
#	Echo for every non-zero threshold:
#		tiny_say			used for non-critical messages
#							they become visible by setting --verbose
#							(VERBOSE=1) or --debug=1 (or higher;
#							DEBUG>=1) when get_options is used.
#
#	For some of the functions above there is a 'bare'
#	version that prints the message under the same conditions
#	as the 'dressed-up' version, but echos the message
#	unmodified except for an optional, fixed, prefix (see PROCEDURE).
#	These are usually used following a call to the non-bare
#	version.
#
#	For echoing to stdin
#	--------------------
#		tiny_note_bare
#		tiny_echo_bare
#
#	For echoing to stderr
#	---------------------	
#		tiny_message_bare
#		tiny_yell_bare
#		tiny_warn_bare
#		tiny_say_bare
#
# PROCEDURE:
#	The main intent is to provide a mechanism for
#	echoing a message after adding the name of the
#	calling routine identified in the bash variable
#	__CALLER__.
#
#	Action is controlled by several bash variables:
#
#	DEBUG, VERBOSE
#
#	These are most easily set by using get_options,
#	which introduces DEBUG as a positive integer,
#	and VERBOSE as a boolean variable. Setting the
#	bool VERBOSE is equivalent to DEBUG=1.
#
#	The message is only echoed if
#		max(DEBUG,VERBOSE) >= $2
#	i.e. if the user-requested "verbosity" level in
#	DEBUG and/or VERBOSE is equal to or larger then the
#	threshold $2 set by the call to tiny_note.
#
#	__CALLER__
#
#	If this is not set, or is the null string, then
#	the string $1 is printed with the content of
#	__NOTE_PREFIX__ at the start. By default this is
#	a string of four spaces. This is used for the
#	'bare' functions listed above.
#
#	Usually __CALLER__ is set to a string identifying
#	the calling routine. For a top-level script this
#	can be `basename $0 .sh`. Bash functions need
#	to hardcode a string as a local variable:
#		local __CALLER__="function name"	
#
#	The string echoed to stdout has the form
#		[DRYRUN ]%$__CALLER__[-SEIW][-$TIME]-$1
#	where the components between square brackets
#	may not be present.
#
#	The components are:
#
#	'DRYRUN'
#	Is added if the bash variable DRYRUN is set to a
#	non-zero integer (DRYRUN is a boolean option defined
#	by get_options).
#
#	__CALLER__
#	identifies the calling function (see above)
#
#	SEIW (from $3; default: 'I')
#	Single character:
#	'S'	success, used by tiny_done
#	'E'	error, used by tiny_die
#	'I'	information, used by tiny_say
#	'W'	warning:, used by tiny_warn
#
#	TIME
#	The system time. This is added only if variable
#	__TIME_TAG__ is defined. This should be a valid
#	format accepted by the 'date' utility (without
#	the leading plus sign, e.g. "%m/%d %H:%M")
# MODICATION HISTORY:
#	JAN-2013, Paul Hick (UCSD/CAIDA; pphick@caida.org)
#-

tiny_note () {
	local __VERBOSE__
	if [ -z "$VERBOSE" ]; then
		__VERBOSE__=0
	else
		__VERBOSE__=$VERBOSE
	fi
	if [ -n "$DEBUG" ]; then
		if [ $DEBUG -gt $VERBOSE ]; then
			__VERBOSE__=$DEBUG
		fi
	fi

	local __threshold__
	if [ -z "$2" ]; then
		__threshold__=0
	else
		__threshold__=$2
	fi

	# Only if the VERBOSE level is higher than the
	# threshold, something is actually printed

	if [ $__VERBOSE__ -lt $__threshold__ ]; then
		return
	fi

	# If __CALLER__ is not defined, then just echo

	if [ -z "$__CALLER__" ]; then

		if [ -z "$__NOTE_PREFIX__" ]; then
			local __NOTE_PREFIX__="    "
		fi
			
		echo "$__NOTE_PREFIX__$1"

	else

		local __seiw__
		if [ -n "$3" ]; then
			__seiw__="-$3"
		else
			__seiw__=""
		fi

		local __note_date__
		if [ -n "$__TIME_TAG__" ]; then
			__note_date__="`date +\"$__TIME_TAG__\"` "
		else
			__note_date=""
		fi

		local __dryrun__
		if [ "$DRYRUN" = "1" ]; then
			__dry_run__="DRYRUN "
		else
			__dry_run__=""	
		fi

		echo "$__dry_run__$__note_date__%$__CALLER__$__seiw__--  $1"

	fi
}

#+
# NAME:
#	tiny_note_bare
# PURPOSE:
#	Print message, obeying verbosity rules from
#	tiny_note, prefixed by content of __NOTE_PREFIX__
#-

tiny_note_bare () {
	local __CALLER__=""
	tiny_note "$1" "$2"
}

tiny_echo () {		# Always echo to stdin
	tiny_note "$1" "$VERBOSE" "$2"
}
tiny_echo_bare () {	# Always echo to stdin
	tiny_note_bare "$1" "$VERBOSE"
}

#+
# NAME:
#	tiny_message
# PURPOSE:
#	Prints message to stderr
# PROCEDURE:
#	Same as tiny_note, but prints to stderr
#	instead of stdout.
#-

tiny_message () {
	tiny_note "$1" "$2" "$3" >&2
}

tiny_message_bare () {
	tiny_note_bare "$1" "$2" >&2
}

tiny_warn () {		# Always echo to stderr
	local __seiw__
	if [ -n "$2" ]; then
		__seiw__="$2"
	else
		__seiw__="W"
	fi
	tiny_message "$1" "$VERBOSE" "$__seiw__"
}

tiny_warn_bare () {	# Always echo to stderr
	tiny_message_bare "$1" "$VERBOSE"
}

tiny_die () {		# Always echo to stderr; then quit with error code 1
	if [ -n "$1" ]; then
		local __seiw__
		if [ -n "$2" ]; then
			__seiw__="$2"
		else
			__seiw__="E"
		fi
		tiny_message "$1" "$VERBOSE" "$__seiw__"
	fi

	local status
	if [ -n "$3" ]; then
		status=$3
	else
		status=1
	fi
	exit $status
}

tiny_done () {		# Always echo to STDERR; then quit with error code 0
	if [ -n "$1" ]; then
		local __seiw__
		if [ -n "$2" ]; then
			__seiw__="$2"
		else
			__seiw__="S"
		fi
		tiny_message "$1" "$VERBOSE" "$__seiw__"
	fi
	exit 0
}

tiny_yell () {		# Always echo to stderr
	local __seiw__
	if [ -n "$2" ]; then
		__seiw__="$2"
	else
		__seiw__="I"
	fi
	tiny_message "$1" "$VERBOSE" "$__seiw__"
}

tiny_yell_bare () {	# Always echo to stderr
	tiny_message_bare "$1" "$VERBOSE"
}

tiny_say () {		# Echo for every non-zero VERBOSE to stderr
	local __seiw__
	if [ -n "$2" ]; then
		__seiw__="$2"
	else
		__seiw__="I"
	fi
	tiny_message "$1" "1" "$__seiw__"
}
tiny_say_bare () {	# Echo for every non-zero VERBOSE to stderr
	tiny_message_bare "$1" "1"
}

tiny_debug () {		# Echo for every non-zero VERBOSE to stderr
	local __seiw__
	if [ -n "$2" ]; then
		__seiw__="$2"
	else
		__seiw__="I"
	fi
	tiny_message "$1" "2" "$__seiw__"
}
tiny_debug_bare () {	# Echo for every non-zero VERBOSE to stderr
	tiny_message_bare "$1" "2"
}

nice_size () {
	local size=0
	local digits=2
	if [ -n "$1" ]; then
		size=$1
	fi
	if [ -n "$2" ]; then
		digits=$2
	fi
	python -c"import tiny_bits; print tiny_bits.nice_size($size,$digits)"
}

nice_wsize () {
	local size=0
	local digits=2
	if [ -n "$1" ]; then
		size=$1
	fi
	if [ -n "$2" ]; then
		digits=$2
	fi
	local digits=$2
	if [ -z "$digits" ]; then
		digits=2
	fi
	python -c"import tiny_bits; print tiny_bits.nice_wsize($size,$digits)"
}

fi
