# -*- Shell-script -*-
# Basic functions (particularly formatting) used in ALT distribute.

# Copyright (C) 2002 Ivan Zakharyaschev <imz@altlinux.ru>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Function names similar to those in on /etc/init.d/functions.
# For changelog see the main script.
# Last change: 2002 Oct 21.


# ncurses progs require a set TERM
[ -n "$TERM" ] || TERM=dumb
export TERM

# Obsolete: we'll use tput to get it everytime we need.
# Get a sane screen width
#[ -n "$COLUMNS" ] || COLUMNS=80

# Taken from /etc/sysconfig/init:

# offset from right margin of the screen to start status labels at.
RES_OFFSET=8

# The cmd names and color codes used as arguments for tput(1)
# were taken from terminfo(5).

# terminal sequence to move to that position.
MOVE_TO_COL(){ 
    # `tput cols` reports 80 on dumb terms, the real width when appropriate;
    # `tput hpa N` moves to col N; on dumb terms does nothing.
    tput hpa $(( $(tput cols) - RES_OFFSET )) # Horizontal Position Absolute
}

# Enumerate colors
let BLACK=0 RED=1 GREEN=2 YELLOW=3 BLUE=4 MAGENTA=5 CYAN=6 WHITE=7

# set 'success' color (currently: green)
SETCOLOR_SUCCESS(){ tput bold; tput setaf $GREEN; }

# set 'failure' color (currently: red)
SETCOLOR_FAILURE(){ tput bold; tput setaf $RED; }

# set 'warning' color (currently: yellow)
SETCOLOR_WARNING(){ tput bold; tput setaf $YELLOW; }

# set 'info' color (currently: cyan)
SETCOLOR_INFO(){ tput bold; tput setaf $CYAN; }

# set 'banner' color (currently: blue on yellow)
SETCOLOR_BANNER(){ tput setaf $BLUE; tput setab $YELLOW; }

# terminal sequence to reset to the default color.
SETCOLOR_NORMAL(){ 
    tput op # set Original color Pair
    tput sgr0 # turn off all special graphics mode (bold in our case)
}

echo_success()
{
	MOVE_TO_COL
	echo -n '[  '
	SETCOLOR_SUCCESS
	echo -n 'OK'
	SETCOLOR_NORMAL
	echo '  ]'
	return 0
}

echo_failure()
{
	MOVE_TO_COL
	echo -n '['
	SETCOLOR_FAILURE
	echo -n 'FAILED'
	SETCOLOR_NORMAL
	echo ']'
	return 0
}

echo_passed()
{
	MOVE_TO_COL
	echo -n '['
	SETCOLOR_WARNING
	echo -n 'PASSED'
	SETCOLOR_NORMAL
	echo ']'
	return 0
}

# Log that something succeeded
success()
{
	echo_success
	return 0
}

# Log that something failed
failure()
{
	rc=$?
	echo_failure
	return $rc
}

# I change the sematics, now it is not:
# Log that something passed, but may have had errors. Useful for fsck
# anymore; return 'no failure' always (as success() does).
passed()
{
	#local rc=$?
	echo_passed
	return 0
}

# Run some action. Log its output.

start_action()
{
	local action="$1"
	shift
	SETCOLOR_INFO
	printf "$action" "$@"
	SETCOLOR_NORMAL
	echo -n ": "
	ACTION="$STAGE $action"
}

action()
{
	start_action "$1"
	shift
	bash -c "$*" && success "$STRING" || failure "$STRING"
	local rc=$?
	#echo -e '\r'
	return $rc
}

enter_stage()
{
    if [[ "$STAGE" == "$1" ]]; then
	return
    fi
	STAGE="$1"
	shift
	SETCOLOR_BANNER
	printf "$STAGE" "$@"
	SETCOLOR_NORMAL
	echo
}

