#!/bin/sh
#
# Copyright (c) 2001-2003 Gregory M. Kurtzer
#
# Copyright (c) 2003-2012, The Regents of the University of California,
# through Lawrence Berkeley National Laboratory (subject to receipt of any
# required approvals from the U.S. Dept. of Energy).  All rights reserved.
#


GRAY="\e[0;37m"
RED="\e[0;31m"
GREEN="\e[1;32m"
YELLOW="\e[1;33m"
BLUE="\e[1;34m"
PINK="\e[1;35m"
CYAN="\e[1;36m"
WHITE="\e[1;37m"
NORMAL="\e[0;39m"


msg_blue() {
    echo -ne "$BLUE"
    echo -ne "$1"
    echo -ne "$NORMAL"
}

msg_red() {
    echo -ne "$RED"
    echo -ne "$1"
    echo -ne "$NORMAL"
}

msg_green() {
    echo -ne "$GREEN"
    echo -ne "$1"
    echo -ne "$NORMAL"
}

msg_white() {
    echo -ne "$WHITE"
    echo -ne "$1"
    echo -ne "$NORMAL"
}

msg_gray() {
    echo -ne "$GRAY"
    echo -ne "$1"
    echo -ne "$NORMAL"
}

msg_blue() {
    echo -ne "$BLUE"
    echo -ne "$1"
    echo -ne "$NORMAL"
}
   
msg_yellow() {
    echo -ne "$YELLOW"
    echo -ne "$1"
    echo -ne "$NORMAL"
}
   
reply_ok() {
    echo -en "\\033[77G"
    msg_green " OK\n"
}

reply_yes() {
    echo -en "\\033[76G"
    msg_green " YES\n"
}

reply_no() {
    echo -en "\\033[77G"
    msg_yellow " NO\n"
}

reply_done() {
    echo -en "\\033[75G"
    msg_green " DONE\n"
}

reply_success() {
    echo -en "\\033[71G"
    msg_green " SUCCESS\n"
}

reply_warn() {
    echo -en "\\033[75G"
    msg_yellow " WARN\n"
}

reply_skipped() {
    echo -en "\\033[72G"
    msg_yellow " SKIPPED\n"
}

reply_error() {
    echo -en "\\033[74G"
    msg_red " ERROR\n"
}

wwprint() {
    MSG=$1
    COLOR=$GRAY
    COMMAND=`basename $0 | sed -e 's/^[0-9]*\-//' | sed -e 's/\.init$//'`

    case $2 in
        white|WHITE)            COLOR=$WHITE ;;
        gray|GRAY|grey|GREY)    COLOR=$GRAY ;;
        red|RED|error)          COLOR=$RED ;;
        yellow|YELLOW|warn)     COLOR=$YELLOW ;;
        blue|BLUE)              COLOR=$BLUE ;;
    esac

    echo -ne "$NORMAL$COMMAND:\\033[14G $COLOR$MSG$NORMAL"
}

wwaction() {
    TMPFILE=`mktemp`
    "$@" >$TMPFILE 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        reply_ok
    else
        reply_error
        wwprint "+ $*\n" RED
        cat $TMPFILE | while read i; do
            wwprint "$i\n" YELLOW
        done
    fi
    rm -f $TMPFILE
    return $RETVAL
}

wwtest() {
    if "$@" >/dev/null 2>&1; then
        reply_ok
        return 0
    else
        reply_no
        return 1
    fi
}

wwrun() {
    TMPFILE=`mktemp`
    MSG=`echo "$*" | cut -c 1-60`
    wwprint " + $MSG"
    "$@" >$TMPFILE 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        reply_ok
        return 0
    else
        reply_error
        cat $TMPFILE | while read i; do
            wwprint "$i\n" YELLOW
        done
    fi
    rm -f $TMPFILE
    return $RETVAL
}

wwisuser() {
    USERNAME=$1
    if [ -n $USERNAME ]; then
        REQUESTED_UID=`id -u $USERNAME`
        ACTUAL_UID=`id -u`
        if [ "x$REQUESTED_UID" == "x$ACTUAL_UID" ]; then
            return 0
        fi
    fi

    return 1
}

wwreqroot() {
    MSG=$1
    if ! wwisuser root; then
        if [ -n "$MSG" ]; then
            wwprint $MSG warn
        else
            wwprint "Can only be run by the root user\n" warn
        fi
        exit 255
    fi
}


