#!/bin/bash
USER_NAME=`whoami`	
GRAPH_HOME="/home/$USER_NAME/graphs"
GRAPH_DATE="`date +%Y%m%d-%H%M`"
HYBODUS_HOST="monitor.contegix.com"
HYBODUS="https://$HYBODUS_HOST"
FROM_EMAIL='support@contegix.com'
PWD=`pwd`
SEND="true"
GRAPH_FILE="$PWD/graphfile"
EMAIL_FILE="$PWD/graphemails"
SIGNATURE_FILE="$PWD/graphs-signature"
DATE=`date +%Y%m%d`
SUMMARY_EMAIL="DO NOT REPLY\r\rGraph Email summary for today, $DATE\r\r"

function failandexit {
	echo "$1"
	exit 0
}

function intval {
# $1: String or numeric value
# Remove all non numeric characters
 n="$(echo "$1" |  sed 's/[^0-9]//g')"
 echo "$n"
}

function getcookie {
	cookies=`locate cookies.txt|grep $(whoami)`
	counter="0"
	for cookie in $cookies
	do
		cookiecontents=`cat $cookie`
		for cookieline in $cookiecontents
		do
			if [ "$counter" == "0" ]; then
				if [[ "$cookieline" =~ "${HYBODUS_HOST}" ]]; then
					counter="1"
					REPLY=$(echo "$cookie")
				fi
			fi
		done	
	done		

	if [ "$counter" == "0" ]; then
		failandexit "Unable to find any cookies for monitor.contegix.com"
	fi
}

function getfile {
#$1 = input
#$2 = companyname 
#$3 = servername
#$4 = graphtype
#getfile inputfilename companyname servername graphtype

#input /graphs/216-cpu-1200x4501181589967.png
#server gsi01
#graphtype cpu

	local input=$1
	local COMPANY=$2
	local server=$3
	local graphtype=$4
	local file=${input#\/graphs\/}

	wget "$HYBODUS$input"
	mkdir -p "$GRAPH_HOME/$GRAPH_DATE/$COMPANY-$DATE"
	mv "$PWD/$file" "$GRAPH_HOME/$GRAPH_DATE/$COMPANY-$DATE/$graphtype-$server.png"		
}

getcookie
newcookie=$REPLY

mkdir -p "$GRAPH_HOME/$GRAPH_DATE"
i=0
n=0
n=`wc -l $GRAPH_FILE`  # Counts the number of lines in GRAPH_FILE
n=$(intval $n)
i=1 # Start on the first line of file



while [ $i -le $n ]
do 
	line=`head -$i $GRAPH_FILE | tail -1` # The next line


	if [[ ! "$line" =~ "#" ]]; then #If there is no # in the line, use it, as it is not commented out
		day=`echo $line | cut -d' ' -f1` # Should this be sent out only on Sunday (example:  0 for sunday, anything else for daily)
		company=`echo $line | cut -d' ' -f2` # Company name where files are downloaded to
	        servername=`echo $line | cut -d' ' -f3` # the server name (example: gsi01)
		SERVERPAGE=`echo $line | cut -d' ' -f4` # URL for where to grab graphs from
		CPUGRAPH=`echo $line | cut -d' ' -f5` #Should we get CPU Graph
		LOADGRAPH=`echo $line | cut -d' ' -f6` #Should we get Load Graph
		MEMORYGRAPH=`echo $line | cut -d' ' -f7` #Should we get Memory Graph

		day=$(intval $day)
		curday="$(date +%w)"
		#curday=0  #Test for sunday
		curday=$(intval $curday)

		process=1

		if [[ $day == 0 ]]; then
			if [[ $curday == 0 ]]; then
				process=1
			else
				process=0
			fi
		fi

		#Don't get the graphs
		#process=0

		if [[ $process == 1 ]]; then
			wget --load-cookies $newcookie "$HYBODUS/$SERVERPAGE"
			STROUT=`cat $PWD/$SERVERPAGE`

			for IND in $STROUT
			do
			        match=".png"
			        a="$IND"; 
				if [[ "$a" =~ "${match}" ]]; then
					cpu="cpu"
			                b="$a"

			                if [[ "$b" =~ "${cpu}" ]]; then
						if [[ "$CPUGRAPH" =~ "${cpu}" ]]; then
							b1=${b%\"}
				                        b2=${b1#src\=\"}
							getfile "$b2" "$company" "$servername" "cpu" 
						fi
					fi
					load="load"
					c="$a"
					if [[ "$c" =~ "${load}" ]]; then
						if [[ "$LOADGRAPH" =~ "${load}" ]]; then
							c1=${c%\"}
			                        	c2=${c1#src\=\"}
							getfile "$c2" "$company" "$servername" "load"
						fi
			                fi
					
					memory="physical_memory-1"
					d="$a"
					if [[ "$d" =~ "${memory}" ]]; then
						if [[ "${memory}" =~ "$MEMORYGRAPH" ]]; then
							d1=${d%\"}
			                        	d2=${d1#src\=\"}
							getfile "$d2" "$company" "$servername" "memory"							
						fi
					fi
				fi
			done
			rm "$PWD/$SERVERPAGE"
		fi
	fi
	i=`expr $i + 1`  #increment counter
done #end while loop

GRAPH_DIR="$GRAPH_HOME/$GRAPH_DATE"
cd $GRAPH_DIR

directories=`find * -maxdepth 0`
for object in $directories
do
        zipfile="${object}.zip"
        zip -r $zipfile $object

#==================================================================================================================

	if [[ "$SEND" == "true" ]]; then
		n=`wc -l $EMAIL_FILE`  # Counts the number of lines in GRAPH_FILE
		n=$(intval $n)
		i=1 # Start on the second line of file
		
		while [ $i -le $n ]
		do
		        line=`head -$i $EMAIL_FILE | tail -1` # The next line

			if [[ ! "$line" =~ "#" ]]; then  #If there is no # in the line, use it, as it is not commented out
			        COMPANY_NAME=`echo $line | cut -d' ' -f1` # Company Name
				RECURRENCE_NUM=`echo $line | cut -d' ' -f2` # how often it should be sent out
			        TO_EMAIL=`echo $line | cut -d' ' -f3` # Company name where files are downloaded to
				

				if [[ "$zipfile" =~ "$COMPANY_NAME" ]]; then
		
					if [[ "$RECURRENCE_NUM" == "1" ]]; then
						RECURRENCE="24 hours"
					else
						RECURRENCE="week"
					fi

					SUMMARY_EMAIL="${SUMMARY_EMAIL}The graphs for the last ${RECURRENCE} for ${COMPANY_NAME} where sent to ${TO_EMAIL}\r"

					#@@// variables for debugging
					#TO_EMAIL='brian.metzler@contegix.com'

					EMAIL_TEST="I have zipped and attached the graphs for the last ${RECURRENCE}."
					EMAIL_SIG=`cat $SIGNATURE_FILE`
					EMAIL_TEST="$EMAIL_TEST$EMAIL_SIG"
				
					(echo "$EMAIL_TEST"; uuencode $zipfile $zipfile)  | mail -s "[$COMPANY_NAME] CPU and Load Graphs" -a 'From: support@contegix.com' $TO_EMAIL

				fi
			fi
		        i=`expr $i + 1`  #increment counter
		done #end while loop

		
	else
		echo "Graphs Not Emailed"

	fi
done

if [[ "$SEND" == "true" ]]; then
	echo -e "$SUMMARY_EMAIL" | mail -s "Daily Graph Summary for $DATE" -a 'From: graphs@contegix.com' engineers@contegix.com
fi


echo "DONE"

