#! /bin/bash

epelgeneral()
{
	echo "== Stats =="
	echo
	echo "=== General ==="
	echo
	wget -q -O - 'http://cvs.fedora.redhat.com/viewcvs/*checkout*/owners/owners.epel.list?root=extras' | grep '^Fedora EPEL|' | cut -d '|' -f 4 | sed 's|,|\n|g' |  sort | uniq > ~/.epel-owners.new

	local oldpackagers=$(wc -l < ~/.epel-owners)
	local newpackagers=$(wc -l < ~/.epel-owners.new)

	echo "Number of EPEL Contributors ${newpackagers}"$'\n'

	if [[ ${newpackagers} -gt ${oldpackagers} ]]
	then
		echo We welcome $((${newpackagers}-${oldpackagers})) new contributors: $(echo $(grep -vf ~/.epel-owners ~/.epel-owners.new | sed 's|@|_AT_|') | sed 's| |, |g' )$'\n'
	fi

	mv ~/.epel-owners ~/.epel-owners.old
	mv ~/.epel-owners.new ~/.epel-owners
}


proceedrepo()
{
	echo "=== EPEL ${1} ==="
	echo
	repoquery --repoid=epel${1}-source -a --archlist="src"  --queryformat " * %{NAME} | %{SUMMARY}"  | grep -v 'primary.xml.gz' | grep " | " | sort > ~/.epel${1}-source-pgklist.new
	echo "Number of source packages: $(wc -l < ~/.epel${1}-source-pgklist.new)"$'\n'
	repoquery --repoid=epel${1} -a  --queryformat " * %{NAME} | %{SUMMARY}" | grep -v 'primary.xml.gz' | grep " | " | sort > ~/.epel${1}-arch-pgklist.new
	echo "Number of binary packages: " $(wc -l < ~/.epel${1}-arch-pgklist.new) $'\n'

	local oldpackages=$(wc -l < ~/.epel${1}-source-pgklist)
	local newpackages=$(wc -l < ~/.epel${1}-source-pgklist.new)
	if [[ ${newpackages} -gt ${oldpackages} ]]
	then
		echo "There are $((${newpackages}-${oldpackages})) new Packages:"
		grep -vwf - .epel${1}-source-pgklist.new <<< "$(awk '{print $2}' < .epel${1}-source-pgklist)"
		echo
	fi

	mv ~/.epel${1}-arch-pgklist ~/.epel${1}-arch-pgklist.old
	mv ~/.epel${1}-arch-pgklist.new ~/.epel${1}-arch-pgklist
	
	mv ~/.epel${1}-source-pgklist ~/.epel${1}-source-pgklist.old
	mv ~/.epel${1}-source-pgklist.new ~/.epel${1}-source-pgklist
}

epelgeneral
proceedrepo 5 
proceedrepo 4

echo $'\n''----'$'\n''["CategoryEPELReports"]'

