#!/bin/sh

# ClonePanel - Manages duplicate accounts on two or more webservers,
# including snapshot backups, monitoring and failover dns.
# Copyright (C)2006 Chris Cheers, Internet Lynx.
# Contact chris[at]clonepanel[dot]com.
# Internet Lynx, PO Box 7117, Mannering Park, NSW 2259, Australia

# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Version=0.33
# Colour handling for all applications

ECHO=echo
# override standard (/bin/echo) if builtin is available to support colours
# Comment out the above to disable colours


# Colour handling function:
function cecho {
	#Defaults:
	option=''
	colour=$normal
	message=''

	#Start with options (currently supporting only -c colour and -n)
	while getopts ":nc:" opt
	do
        	case $opt in
                	n)      option='-n'
                        ;;
	                c)      colour=$OPTARG
                        ;;
	                *)      $ECHO "Error - cecho syntax"; exit 1
                        ;;
	        esac
	done
	shift $(($OPTIND - 1))
	OPTIND=1
	# reset getopts for next time

	if [ $# -gt 0 ]; then
		for arg in $@
		do
			message="$message$arg "
		done
	fi
	$ECHO $option -e "$colour$message$normal"
}

blackf=30
redf=31
greenf=32
yellowf=33
bluef=34
magentaf=35
cyanf=36
whitef=37
blackb=40
redb=41
greenb=42
yellowb=43
blueb=44
magentab=45
cyanb=46
whiteb=47

bg=$blackb
fg=$whitef

normal="\\E[${fg};${bg}m"
green="\\E[${greenf};${bg}m"
yellow="\E[${yellowf};${bg}m"
blue="\\E[${bluef};${bg}m"
magenta="\\E[${magentaf};${bg}m"
cyan="\\E[${cyanf};${bg}m"
red="\\E[${redf};${bg}m"

redcode="\[${redf};${bg}m"
# Modified version of red colour code to be used in grep for detecting errors
# (If another colour is to be used for errors, add its code here similarly)

# Content-related colour definitions:
info=$cyan
warning=$yellow
error=$red
errorcode=$redcode

ok=$green
prompt=$blue

# Usage examples:
# cecho -c $blue "hello"
# cecho -c $warning "world"
# cecho -c $ok "happy"
# cecho -c $info "blah"
# cecho -c $error "Oops!"
# grep "$errorcode" messages.log

