#!/bin/bash
########################
# Author: Dawid Orczyk #
########################
NOW=`date +%d%m%y`
# Paths and files 
pathSYMCLI=/usr/symcli/bin/
pathOutput=singlePath/
pathTempOutput=$pathOutput"Temp/"
fileOutput="EMC_$NOW.csv"
filePathOutput=$pathOutput$fileOutput
# Commands
cmdSymmask='symmask list logins'
cmdSymcfg="symcfg list"
symcfgExecute="$pathSYMCLI$cmdSymcfg | awk '{if(NR>5)print}' | awk '{print \$1}'"
# Temp Files
fileOutputTemp='EMCtemp.csv'
fileCounterGlobal='CounterGlobal.txt'
fileCounterYes='CounterYes.txt'
fileExcluded='Excluded.txt'
fileTempOutput='EMC_temp.csv'
# Temp Paths
filePathOutputTemp=$pathTempOutput$fileOutputTemp
filePathCounterGlobal=$pathTempOutput$fileCounterGlobal
filePathCounterYes=$pathTempOutput$fileCounterYes
filePathExcluded=$pathTempOutput$fileExcluded
filePathTempOutput=$pathTempOutput$fileTempOutput
regexp='^[0-9]+$'
regexp2='^[0-9]+'
function Cleanup
{
	if [[ -f $filePathCounterGlobal ]]
	then
		rm $filePathCounterGlobal
	fi
	if [[ -f $filePathCounterYes ]]
	then
		rm $filePathCounterYes
	fi
	if [[ -f $filePathExcluded ]]
	then
		rm $filePathExcluded
	fi
	if [[ -f $filePathOutput ]]
	then
		rm $filePathOutput
	fi
	
	if [[ -f $filePathOutputTemp ]]
	then
		rm $filePathOutputTemp
	fi
	
	echo "---> Old files clean up - done"
	
}
function Counters
{
	cat $filePathOutputTemp | cut -f 3 -d ',' | sort | uniq -c | sort -n >> $filePathCounterGlobal
	cat $filePathOutputTemp | grep -i 'Yes' | cut -f 3 -d ',' | sort | uniq -c | sort -n >> $filePathCounterYes
	
	echo "---> Counters callculations - done"
}
function CreateCatalogStructure
{
	if [[ ! -d $pathOutput ]]
	then
		mkdir -p $pathOutput
	fi
	
	if [[ ! -d $pathTempOutput ]]
	then
		mkdir -p $pathTempOutput
	fi
	
	echo "---> Catalog structure created"
}
function GetSymmaskOutput
{
	echo "---> Collecting Symmask Data it may take a while ..."
	output=`eval $symcfgExecute`
	read -a array <<< $output
	echo "SID,IDENTIFIER,NODE NAME,LOGGED" >> $filePathOutputTemp
	for element in "${array[@]}"
	do
		if [[ $element =~ $regexp ]]
		then
			if [[ $element != "" ]]
			then
				arraySid=${element:8:4}
				symmaskExecute="$pathSYMCLI$cmdSymmask -sid $arraySid | awk '{print  \$1, \$3, \$6}'"
				output=`eval $symmaskExecute`
			
				IFS=$'\n'
				arr=( $(echo -e "$output") ) 
				
				for i in ${arr[@]} 
				do 
					if [[ $i =~ $regexp2 ]]
					then
						toWrite=$(echo $i | sed -e 's/ /,/g')
						echo "$arraySid,$toWrite" >> $filePathOutputTemp
					fi
				done			
			fi
		fi
	done
	
	echo "---> Symmask output was saved to file - $filePathOutputTemp"
}
function CreateExcludeFile
{
	echo "---> Creating Exclude Files it may take a while ..."
	
	for i in `cat $filePathOutputTemp`
	do
		grep_host=`echo $i | cut -f 3 -d','`
		NULL_value=`echo $grep_host | egrep -i 'Null|NODE'` 
	
		if [[ -z ${NULL_value} ]]
		then
			if [[ ! -z $grep_host ]]
			then	
				yes_counter=`cat $filePathCounterYes | grep "$grep_host$" | awk '{print $1}'`
		
				if [[ -z $yes_counter ]]
				then
					yes_counter=0
				fi
		
				global_counter=`cat $filePathCounterGlobal | grep "$grep_host$" | awk '{print $1}'`
			
				division=2
				check=$(expr $global_counter % $division)
				if [[ $check -eq 0 ]]
				then
					valid_counter=$(($global_counter/$division))
				else
					global_counter=$(($global_counter+$(echo 1)))
					valid_counter=$(($global_counter/$division))
				fi	
	
				if [[ "$yes_counter" -ge "$valid_counter" ]]
				then
					echo $grep_host >> $filePathExcluded
				fi
			fi
		fi
	done
	
	echo "NULL" >> $filePathExcluded
	echo "Null" >> $filePathExcluded
	echo "---> Exclude File was created"
}
function GenerateReport
{
	echo "---> Generating Final Report it may take a while ..."
	for i in `cat $filePathOutputTemp`
	do
		grep_host=`echo $i | cut -f 3 -d','`
		parse=`echo $i | cut -f 4 -d',' | grep -i 'No'`
	
		if [[ ! -z $parse ]]
		then
			grep_exclude=`cat $filePathExcluded | grep $grep_host`
			if [[ -z $grep_exclude ]]
			then
				echo $i >> $filePathOutput
			fi
		fi
	done
}
Cleanup
CreateCatalogStructure
GetSymmaskOutput
Counters
CreateExcludeFile
GenerateReport
echo "---> Work done, goodbye"
