#!/bin/bash
#
# 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[72G"
    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 "$WHITE$COMMAND$NORMAL:\\033[14G $COLOR$MSG$NORMAL"
}

wwprintf() {
    FMT="$1"
    shift
    
    COLOR=$GRAY
    COMMAND=`basename -- $0 | sed -e 's/^[0-9]*-\?//' | sed -e 's/\.init$//'`

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

    echo -ne "$WHITE$COMMAND$NORMAL:\\033[14G $COLOR"
    printf "$FMT" "$@"
    echo -ne "$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
            wwprintf "%s\n" YELLOW "$i"
        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
    else
        reply_error
        cat $TMPFILE | while read i; do
            wwprintf "%s\n" YELLOW "$i"
        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
}

wwpackage_check() {
    for PACKAGE in "$@"; do
        if [ -x /bin/rpm ]; then
            wwprint "Checking to see if RPM or capability '$PACKAGE' is installed "
            if wwtest rpm -q --whatprovides "$PACKAGE"; then
                return 0
            fi
        elif [ -x /bin/dpkg ] || [ -x /usr/bin/dpkg ]; then
            wwprint "Checking to see if DEB '$PACKAGE' is installed "
            if dpkg -s $PACKAGE 2>&1 | grep Status | grep -v deinstall > /dev/null; then
                wwtest true
                return 0
            else
                wwtest false
            fi
        fi
    done
    return 1
}

# this function will take an argument list of service names, and return
# on the first match. This is useful for multi-distribution compatibility
# that have different service names for the same service (e.g. ntp and
# ntpd).
wwservice_activate() {
    for SERVICE in "$@"; do
        if [ -x "/bin/systemctl" ]; then
            if ! echo $SERVICE | egrep -q '\.service$|\.socket$'; then
                SERVICE="${SERVICE}.service"
            fi
            if systemctl list-unit-files --type=service,socket | egrep -q "^$SERVICE"; then
                wwprint "Activating Systemd unit: $1\n"
                if systemctl is-enabled --quiet $SERVICE ||
			wwrun /bin/systemctl -q enable $SERVICE; then
                    if wwrun /bin/systemctl -q restart $SERVICE; then
                        return 0
                    fi
                fi
            fi
        elif [ -x "/etc/rc.d/init.d/$SERVICE" ]; then
            wwprint "Activating system service: $1\n"
            if [ -x "/sbin/chkconfig" ]; then
                wwrun chkconfig $SERVICE on
            fi
            if wwrun /etc/rc.d/init.d/$SERVICE restart; then
                return 0
            fi
        elif [ -x "/etc/init.d/$SERVICE" ]; then
            wwprint "Activating system service: $1\n"
            if wwrun /etc/init.d/$SERVICE restart; then
                return 0
            fi
        elif [ -e "/etc/xinetd.d/$SERVICE" ]; then
            wwprint "Activating xinetd service: $1\n"
            wwrun sed -ie 's@\(disable\s*=\s*\)\S*@\1no@' /etc/xinetd.d/$SERVICE
            wwservice_activate xinetd
            return 0
        fi
    done

    wwprint "Could not enable service ($1)\n" error
    return 1
}


service_findname() {
    wwprint "service_findname() has been deprecated" warn
    return 0
}

service_enable() {
    wwprint "service_enable() has been deprecated" warn
    return 0
}

service_restart() {
    wwprint "service_restart() has been deprecated" warn
    return 0
}

