#!/bin/bash
# This is a small script which, run into a base portage dir, counts the number
# of files present in the $FILESDIR for the packages. It has two parameters:
# The first one is the name of the category to look at.
# The other one is the minimum number of files to report (default: 1)

count_files() {
	filescount=$(ls $1/$2/files | egrep -v '^CVS$|^digest-' | wc -l)
	
	mincount=$3
	
	(( $filescount >= ${mincount:=1} )) && echo "$1/$2 $filescount"
}

count_cat() {
	packages=$(ls $1 | egrep -v '^CVS$|^metadata.xml$')
	
	for package in $packages; do
		count_files $1 $package $2
	done
}

if [[ -n "$1" ]]; then
	count_cat $1 $2
else
	cats="$(ls | egrep -v \
		'^CVS$|^profiles$|^distfiles$|^packages$|^header.txt$|^skel.')"
	
	for category in $cats; do
		count_cat $category $2
	done
fi
