#!/bin/sh
# Taken action* from /etc/rc.d/init.d/halt (startup-0.9.8.11-alt1)
#
# Taken echo*, SETCOLOR_*, MOVE_TO_COL and first initialization
# from /etc/rc.d/init.d/functions (service-0.5.14-alt1)

BOOTUP="${BOOTUP:-color}"

if [ -x /sbin/consoletype ] && [ "$(consoletype)" = serial ]; then
	BOOTUP=serial
fi

MOVE_TO_COL()     { :; }
SETCOLOR_BANNER() { :; }
SETCOLOR_FAILURE(){ :; }
SETCOLOR_INFO()   { :; }
SETCOLOR_NORMAL() { :; }
SETCOLOR_SUCCESS(){ :; }
SETCOLOR_WARNING(){ :; }
if [ "$BOOTUP" = color ]; then
	MOVE_TO_BOL() { printf '\r'; }
	. /etc/init.d/outformat
else
	MOVE_TO_BOL() { printf %s " $(date +%T)"; }
fi

echo_success() {
	MOVE_TO_COL
	printf %s '[ '
	SETCOLOR_SUCCESS
	printf %s 'DONE'
	SETCOLOR_NORMAL
	printf %s ' ]'
	MOVE_TO_BOL
	return 0
}

echo_failure() {
	MOVE_TO_COL
	printf %s '['
	SETCOLOR_FAILURE
	printf %s 'FAILED'
	SETCOLOR_NORMAL
	printf %s ']'
	MOVE_TO_BOL
	return 1
}

echo_passed() {
	MOVE_TO_COL
	printf %s '['
	SETCOLOR_WARNING
	printf %s 'PASSED'
	SETCOLOR_NORMAL
	printf %s ']'
	MOVE_TO_BOL
	return 1
}

action_begin_msg() {
	[ -z "$*" ] || printf '%s ' "$*"
}

action_end_msg() {
	local rc=$1
	[ $rc = 0 ] && echo_success || echo_failure ||:
	echo
	return $rc
}

action_passed_msg() {
	echo_passed ||:
	echo
	return 1
}

action() {
	action_begin_msg "$1"
	shift
	$* >&2
	local rc=$?
	action_end_msg "$rc"
	return $rc
}
