##
#
#  Logging/Progressbar handling for OTA update scripts
#
#  $Id: libotautils 18977 2022-10-02 00:37:38Z NiLuJe $
#
#  kate: syntax bash;
#  shellcheck shell=sh
#
##

## Logging
# Pull some helper functions for logging
_FUNCTIONS=/etc/rc.d/functions
[ -f ${_FUNCTIONS} ] && source ${_FUNCTIONS}

# Make sure HACKNAME is set (NOTE: This should be overriden in the update script)
[ -z "${HACKNAME}" ] && HACKNAME="ota_script"


# Slightly tweaked version of msg() (from ${_FUNCTIONS}, where the constants are defined)
logmsg()
{
	local _NVPAIRS
	local _FREETEXT
	local _MSG_SLLVL
	local _MSG_SLNUM

	_MSG_LEVEL="${1}"
	_MSG_COMP="${2}"

	{ [ $# -ge 4 ] && _NVPAIRS="${3}" && shift ; }

	_FREETEXT="${3}"

	eval _MSG_SLLVL=\${MSG_SLLVL_$_MSG_LEVEL}
	eval _MSG_SLNUM=\${MSG_SLNUM_$_MSG_LEVEL}

	local _CURLVL

	{ [ -f ${MSG_CUR_LVL} ] && _CURLVL=$(cat ${MSG_CUR_LVL}) ; } || _CURLVL=1

	if [ ${_MSG_SLNUM} -ge ${_CURLVL} ] ; then
		/usr/bin/logger -p local4.${_MSG_SLLVL} -t "${HACKNAME}" "${_MSG_LEVEL} def:${_MSG_COMP}:${_NVPAIRS}:${_FREETEXT}"
	fi

	[ "${_MSG_LEVEL}" != "D" ] && echo "${HACKNAME}: ${_MSG_LEVEL} def:${_MSG_COMP}:${_NVPAIRS}:${_FREETEXT}"
}

# Do the maths from the constants in ${_FUNCTIONS} ;)
EIPS_MAXCHARS="$((${SCREEN_X_RES} / ${EIPS_X_RES}))"
EIPS_MAXLINES="$((${SCREEN_Y_RES} / ${EIPS_Y_RES}))"


# Adapted from libkh[5]
## Check if we have an FBInk binary available somewhere...
# Default to something that won't horribly blow up...
FBINK_BIN="true"
for my_hackdir in linkss linkfonts libkh usbnet ; do
	my_fbink="/mnt/us/${my_hackdir}/bin/fbink"
	if [ -x "${my_fbink}" ] ; then
		FBINK_BIN="${my_fbink}"
		# Got it!
		break
	fi
done
has_fbink()
{
	# Because the fallback is the "true" binary/shell built-in ;).
	if [ "${FBINK_BIN}" != "true" ] ; then
		# Got it!
		return 0
	fi

	# If we got this far, we don't have fbink installed
	return 1
}

do_fbink_print()
{
	# We need at least two args
	if [ $# -lt 2 ] ; then
		echo "not enough arguments passed to do_fbink_print ($# while we need at least 2)"
		return
	fi

	kh_eips_string="${1}"
	kh_eips_y_shift_up="${2}"

	# Unlike eips, we need at least a single space to even try to print something ;).
	if [ "${kh_eips_string}" == "" ] ; then
		kh_eips_string=" "
	fi

	# Check if we asked for a highlighted message...
	if [ "${3}" == "h" ] ; then
		fbink_extra_args="h"
	else
		fbink_extra_args=""
	fi

	# NOTE: FBInk will handle the padding. FBInk's default font is square, not tall like eips,
	#       so we compensate by tweaking the baseline ;).
	${FBINK_BIN} -qpm${fbink_extra_args} -y $(( -4 - ${kh_eips_y_shift_up} )) "${kh_eips_string}"
}

do_fbink_bar()
{
	# We need at least two args
	if [ $# -lt 2 ] ; then
		echo "not enough arguments passed to do_fbink_bar ($# while we need at least 2)"
		return
	fi

	fbink_progress="${1}"
	kh_eips_y_shift_up="${2}"

	${FBINK_BIN} -qP ${fbink_progress} -y $(( -4 - ${kh_eips_y_shift_up} ))
}

do_eips_print()
{
	# We need at least two args
	if [ $# -lt 2 ] ; then
		echo "not enough arguments passed to do_eips_print ($# while we need at least 2)"
		return
	fi

	kh_eips_string="${1}"
	kh_eips_y_shift_up="${2}"

	# Get the real string length now
	kh_eips_strlen="${#kh_eips_string}"

	# Add the right amount of left & right padding, since we're centered, and eips doesn't trigger a full refresh,
	# so we'll have to padd our string with blank spaces to make sure two consecutive messages don't run into each other
	kh_padlen="$(((${EIPS_MAXCHARS} - ${kh_eips_strlen}) / 2))"

	# Left padding...
	while [ ${#kh_eips_string} -lt $((${kh_eips_strlen} + ${kh_padlen})) ] ; do
		kh_eips_string=" ${kh_eips_string}"
	done

	# Right padding (crop to the edge of the screen)
	while [ ${#kh_eips_string} -lt ${EIPS_MAXCHARS} ] ; do
		kh_eips_string="${kh_eips_string} "
	done

	# And finally, show our formatted message centered on the bottom of the screen (NOTE: Redirect to /dev/null to kill unavailable character & pixel not in range warning messages)
	eips 0 $((${EIPS_MAXLINES} - 2 - ${kh_eips_y_shift_up})) "${kh_eips_string}" >/dev/null
}

eips_print_bottom_centered()
{
	# We need at least two args
	if [ $# -lt 2 ] ; then
		echo "not enough arguments passed to eips_print_bottom_centered ($# while we need at least 2)"
		return
	fi

	kh_eips_string="${1}"
	kh_eips_y_shift_up="${2}"

	# Sleep a tiny bit to workaround the logic in the 'new' (K4+) eInk controllers that tries to bundle updates
	if [ "${EIPS_SLEEP}" == "true" ] ; then
		usleep 150000	# 150ms
	fi

	# Can we use FBInk?
	if has_fbink ; then
		do_fbink_print "${kh_eips_string}" ${kh_eips_y_shift_up}
	else
		do_eips_print "${kh_eips_string}" ${kh_eips_y_shift_up}
	fi
}


## Progressbar
# Check if arg is an int
is_integer()
{
	# Cheap trick ;)
	[ "${1}" -eq "${1}" ] 2>/dev/null
	return $?
}

# The amount of steps needed to fill the progress bar
# I'm lazy, so just count the amount of calls in the script itself ;)
# NOTE: Yup, $0 still points to the original script that sourced us :).
[ -z ${STEPCOUNT} ] && STEPCOUNT="$(grep -c '^[[:blank:]]*otautils_update_progressbar$' ${0} 2>/dev/null)"
# Make sure it's sane...
is_integer "${STEPCOUNT}" || STEPCOUNT=1
# NOTE: If you need to for some strange reason, this can be overriden in the update script

# In case we need to catch failure early...
otautils_die()
{
	# FIXME: Invest in a custom eips message? We've got one in libotautils5...

	# And it is called die, after all ;)
	sleep 5
	exit 1
}

# Fill up our progress bar, one step at a time
# Keep track of what we're doing...
_CUR_STEP=0
_CUR_PERCENTAGE=0
otautils_update_progressbar()
{
	# One more step...
	_CUR_STEP=$((_CUR_STEP + 1))
	# Bounds checking...
	if [ ${_CUR_STEP} -lt 0 ] ; then
		_CUR_STEP=0
	elif [ ${_CUR_STEP} -gt ${STEPCOUNT} ] ; then
		_CUR_STEP=${STEPCOUNT}
	fi

	# Make that a percentage
	local bar_percentage=$(( (${_CUR_STEP} * 100) / ${STEPCOUNT} ))
	# We can only *fill* the bar...
	if [ ${_CUR_PERCENTAGE} -lt ${bar_percentage} ] ; then
		_CUR_PERCENTAGE=${bar_percentage}
	fi

	# Make sure that percentage is sane...
	is_integer "${_CUR_PERCENTAGE}" || _CUR_PERCENTAGE=0
	# Bounds checking...
	if [ ${_CUR_PERCENTAGE} -gt 100 ] ; then
		_CUR_PERCENTAGE=100
	elif [ ${_CUR_PERCENTAGE} -lt 0 ] ; then
		_CUR_PERCENTAGE=0
	fi

	# If we're not in the updater runlevel (3), do it ourselves ;)
	if [ "$(runlevel | sed -n -r 's/^([[:alnum:]])([[:blank:]]*)([[:alnum:]])$/\3/p')" -ne 3 ] ; then
		# NOTE: We can actually draw a progress bar with FBInk!
		if has_fbink ; then
			do_fbink_bar ${_CUR_PERCENTAGE} 2
		else
			do_eips_print "Progress: ${_CUR_PERCENTAGE}/100" 2
		fi
	else
		# We're in the updater, refresh the bar (update_progressbar is from ${_FUNCTIONS)
		update_progressbar ${_CUR_PERCENTAGE}
	fi
	if [ $? -eq 0 ] ; then
		logmsg "D" "guiprogress" "progress=${_CUR_PERCENTAGE}" "update progress indicator"
	else
		logmsg "W" "guiprogress" "progress=${_CUR_PERCENTAGE},status=fail" "update progress indicator"
	fi
}

# That's all, folks ;)
