#!/bin/bash
#  vim:sw=8:noet
# SPDX-License-Identifier:  GPL-2.0+
#
# Logging subsystem for alt-rootfs-installer.
#
#   To initialize the subsystem use log_init() function. The subsystem works in
# two major modes "log" and "con".
#   The first mode is "log", it outputs everything to the log file. It is
# still possible to output messages to stdout, but one should use function
# from this module to achieve that.
#   The second mode is "con", it outputs everything to the terminal. In this
# mode it is also possible to redirect the output to a file. The module checks
# whether the file descriptor 1 is a terminal or not. That check was done to
# disable colors and column indentations to not clobber the output in the
# file.

. shell-error 2>/dev/null
. shell-terminfo 2>/dev/null

LOG_FILE=
[ -n "$LOG_MODE" ] || LOG_MODE="log"
[ "$LOG_MODE" = "log" ] && LOG_FILE="$TMP/alt-rootfs-installer.log"
LOG_FD1=1
LOG_FD2=2

# Print a message. This function is intended to be used with print_done() or
# print_fail() functions.
#
# Arguments:
# $1 -- a message to output
print_message() {
	[ "$LOG_MODE" == "con" ] || echo "${1-}"
	if [ -t "$LOG_FD1" ]; then
		1>&$LOG_FD1 color_text "* " magenta
		1>&$LOG_FD1 echo "${1-}"
	else
		1>&$LOG_FD1 echo "* ${1-}"
	fi
	PRINT_TEXT_END=1
}

# Print text without a new line character. It is possible to change color
# and/or a column number to output.
#
# Arguments:
# $1 -- text to output
# $2 (optional) -- color ('-' to skip it)
# $3 (optional) -- a column number
printx() {
	local col=
	local color=
	[ "$LOG_MODE" == "con" ] || echo -n "${1-}"
	if [ -t "$LOG_FD1" ]; then
		[ $# -gt 2 ] && col="$3"
		[ $# -gt 1 ] && color="$2"
		[ -n "$col" ] && 1>&$LOG_FD1 terminfo_col "$col"
		[ "$color" == "-" ] && color=
		1>&$LOG_FD1 color_text "${1-}" "$color"
	else
		1>&$LOG_FD1 echo -n "${1-}"
	fi
}

# Print text with a new line character. It is possible to change color
# and/or a column number to output.
#
# Arguments:
# $1 -- text to output
# $2 (optional) -- color ('-' to skip it)
# $3 (optional) -- a column number
printn() {
	local col=
	local color=
	[ "$LOG_MODE" == "con" ] || echo "${1-}"
	if [ -t "$LOG_FD1" ]; then
		[ $# -gt 2 ] && col="$3"
		[ $# -gt 1 ] && color="$2"
		[ -n "$col" ] && 1>&$LOG_FD1 terminfo_col "$col"
		[ "$color" == "-" ] && color=
		1>&$LOG_FD1 color_message "${1-}" "$color"
	else
		1>&$LOG_FD1 echo "${1-}"
	fi
}

# Print [DONE] with a green color (in the case of terminal).
print_done() {
	[ "$LOG_MODE" == "con" ] || echo "DONE"
	if [ -t "$LOG_FD1" ]; then
		1>&$LOG_FD1 color_message "[DONE]" green
	else
		1>&$LOG_FD1 echo "[DONE]"
	fi
	PRINT_TEXT_END=
}

# Print [FAIL] with a red color (in the case of terminal).
print_fail() {
	[ "$LOG_MODE" == "con" ] || echo "FAIL"
	if [ -t "$LOG_FD1" ]; then
		1>&$LOG_FD1 color_message "[FAIL]" red
	else
		1>&$LOG_FD1 echo "[FAIL]"
	fi
	PRINT_TEXT_END=
}

# Print an error message and exit.
#
# Arguments:
# $1 -- an error message
fatal_error() {
	[ -n "${PRINT_TEXT_END-}" ] && print_fail
	[ "$LOG_MODE" == "con" ] || echo "Error: ${1-}"
	if [ -t "$LOG_FD1" ]; then
		1>&$LOG_FD1 color_text "Error: " red
	else
		1>&$LOG_FD1 echo -n "Error: "
	fi
	1>&$LOG_FD1 2>&$LOG_FD2 fatal "${1-}"
}

# Execute command and redirect stderr to /dev/tty
# $@ -- command and arguments to execute
log_errtty() {
	2>/dev/tty $@
}

# Initialize logging subsystem.
log_init() {
	# Initialize shell-terminfo from libshell
	terminfo_init

	if [ "$LOG_MODE" != "con" ]; then
		# cleanup log file
		if [ ! -d "${LOG_FILE%/*}" ]; then
			fatal_error "can't locate directory ${LOG_FILE%/*} for log file"
		fi
		> "$LOG_FILE"
		exec {LOG_FD1}>&1 {LOG_FD2}>&2 >"$LOG_FILE" 2>&1
	fi
}
