# A stripped down version of /etc/rc.d/init.d/functions containing only the handful
# of functions needed by MMS.

# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH

COLORS=yes
MOVE_TO_COL="echo -en \\033[60G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"

if [[ $( tput colors ) -lt 16 ]]; then
    COLORS=no
fi

daemon() {
    local cmd user=
    case $1 in
        --user=?*)
            user=${1#--user=}
            shift
            ;;
    esac
    cmd="$*"
    [[ ! -z "$user" ]] && cmd="sudo -u $user $cmd"
    $cmd
}

echo_message() {
    local message=$1
    local color_cmd=$2
    [ "$COLORS" = "yes" ] && $MOVE_TO_COL
    echo -n "["
    [ "$COLORS" = "yes" ] && $color_cmd
    echo -n "$message"
    [ "$COLORS" = "yes" ] && $SETCOLOR_NORMAL
    echo -n "]"
    echo -ne "\r"
}

# Log that something succeeded
success() {
    echo_message "  OK  " "$SETCOLOR_SUCCESS"
}

# Log that something failed
failure() {
    echo_message "FAILED" "$SETCOLOR_FAILURE"
}

# Log a warning
warning() {
    echo_message "WARNING" "$SETCOLOR_WARNING"
}

