#[ku]
# patches to have init.d scripts logging to /var/log also!
#
# ::copy::
# ::maintainer::
#
ku_log_echo()
{
	_KUSA_LOG_PHASE=${_KUSA_LOG_PHASE:-"RC"}
	DEBUG=${DEBUG:-"false"}

	local nparm=
	[ "X${1:-}" = "X-n" ] && {
		nparm="-n"
		shift
	}

	ku_log_write_to_console "$@" || true
	echo $nparm "$@" || true
 
	# 1. try to use syslog
	#
	[ -f /var/run/rsyslogd.pid -a -x /usr/bin/logger ] && {
		# should flush RAM and disk buffers first?
		[ X"${_ku_log_buffer:-}" != "X" ] && ku_log_flush_buffer
		[ -s /lib/init/rw/_ku_log_buffer ] && ku_log_flush_disk_buffer
		/usr/bin/logger -t $_KUSA_LOG_PHASE "${_ku_log_line_buffer:-}$@"
		_ku_log_line_buffer=
		return 0
	}

	# 2. syslogd not running, try to write to a disk buffer using
	#    the /lib/init/rw tmpfs filesystem
	#
	(echo -n "" >>/lib/init/rw/_ku_log_buffer) 2>/dev/null && {
		# should flush ram buffer first?
		[ X"${_ku_log_buffer:-}" != "X" ] && ku_log_flush_buffer
		echo "(`date +%H:%M:%S`) ${_ku_log_line_buffer:-}$@" >>/lib/init/rw/_ku_log_buffer
		_ku_log_line_buffer=
		return 0
	}

	# 3. can't write to disk, use a ram temp buffer
	#    (this doesn't work, I think, because init scripts are not
	#    included but runs on separate shell process)
	#
	_ku_log_buffer="${_ku_log_buffer:-}$@\n"
}

ku_log_flush_disk_buffer()
{
	local msg="[ku] saving log buffer to /var/log/boot.log"
	_KUSA_LOG_PHASE=${_KUSA_LOG_PHASE:-"RC"}
	/usr/bin/logger -t $_KUSA_LOG_PHASE "$msg"
	echo -n " * $msg ... "
	echo -e "\nLOG SAVED ON `date`"	>>/var/log/boot.log
	cat /lib/init/rw/_ku_log_buffer	>>/var/log/boot.log
	rm -f /lib/init/rw/_ku_log_buffer
	echo "ok"
	return 0
}

ku_log_flush_buffer()
{
	local line=
	$DEBUG && echo "[ku-debug] flushing RAM buffer"	>>/lib/init/rw/_ku_log_buffer
	echo -e "${_ku_log_buffer:-}"			>>/lib/init/rw/_ku_log_buffer
	$DEBUG && echo "[ku-debug] RAM buffer flushed"	>>/lib/init/rw/_ku_log_buffer
	_ku_log_buffer=
	return 0
}

ku_log_write_to_console () {
    [ "${QUIET:-no}" != yes ] || return 0

    # Only output to the console when we're given /dev/null
    stdin=`readlink /proc/self/fd/0`
    [ "${stdin#/dev/null}" != "$stdin" ] || return 0

    echo "$@" </dev/console >/dev/console || true
}
#[ku] end


# LSB OVERRIDES

log_begin_msg () {
   log_begin_msg_pre "$@"
   if [ -z "${1:-}" ]; then
	return 1
   fi
   ku_log_echo -n "$@"
   log_begin_msg_post "$@"
}

log_daemon_msg () {
    if [ -z "${1:-}" ]; then
    	return 1
    fi
    log_daemon_msg_pre "$@"

    if [ -z "${2:-}" ]; then
	ku_log_echo -n "$1:" || true
 	return
    fi

    ku_log_echo -n "$1: $2" || true
    log_daemon_msg_post "$@"
}



# same of debian func, but fixes the unquoted args to log_begin_msg
log_success_msg () {
    if [ -n "${1:-}" ]; then
	log_begin_msg "$@"
    fi
    log_end_msg 0
}

# same of debian func, but fixes the unquoted args to log_begin_msg
log_failure_msg () {
    if [ -n "${1:-}" ]; then
	log_begin_msg "$@" "..."
    fi
    log_end_msg 1 || true
}

# same of debian func, but fixes the unquoted args to log_begin_msg
log_warning_msg () {
    if [ -n "${1:-}" ]; then
	log_begin_msg "$@" "..."
    fi
    log_end_msg 255 || true
}

# same of debian func, but removes dot echo
# int log_end_message (int exitstatus)
log_end_msg () {
    # If no arguments were passed, return
    if [ -z "${1:-}" ]; then
        return 1
    fi

    local retval
    retval=$1

    log_end_msg_pre "$@"

    # Only do the fancy stuff if we have an appropriate terminal
    # and if /usr is already mounted
    if log_use_fancy_output; then
    	TPUT=${TPUT:-/usr/bin/tput}	# 2023-09-30 lc - missing on daedalus
	RED=$( $TPUT setaf 1)
	YELLOW=$( $TPUT setaf 3)
	NORMAL=$( $TPUT op)
    else
	RED=''
	YELLOW=''
	NORMAL=''
    fi

    if [ $1 -eq 0 ]; then
	# echo "." || true
	echo || :
    elif [ $1 -eq 255 ]; then
	#/bin/echo -e " ${YELLOW}(warning).${NORMAL}" || true
	/bin/echo -e " ${YELLOW}(warning)${NORMAL}" || :
    else
	/bin/echo -e " ${RED}failed!${NORMAL}" || true
    fi
    log_end_msg_post "$@"
    return $retval
}
