#!/bin/sh

DO_DEBUG=1
debug_out()
{
        if [ "${DO_DEBUG}" = "0" ]; then
                return;
        fi
        echo $1
}


output_size()
{
	V=$1

	B=`find * -name "*${V}*" -ls | awk '{total += $7} END {print total}'`
	M=`echo $B/1024/1024 | bc`

	echo "$M Meg";
}

check_ver()
{
	V=$1

	debug_out '================================================'
	debug_out "checking $V"

	COUNT=`grep maria ../../custombuild/versions.txt | grep -c ":${V}:"`
	if [ "${COUNT}" -gt 0 ]; then
		echo "Version ${V} is in versions.txt";
		return;
	fi

	COUNT=`ls ../../*/*${V}* 2>/dev/null | grep -v php | grep --ignore-case maria | grep -c ${V}`

	if [ "${COUNT}" -gt 0 ]; then
		echo "${V} is used ${COUNT} times";
                if [ "${DO_DEBUG}" -ge 1 ]; then
                        ls ../../*/*${V}* | grep -v php | grep --ignore-case maria | grep ${V}
                fi
		return;
	fi

	echo -n "CAN DELETE ${V} - ";
	output_size ${V}
}

for i in `ls -d 5.5/*; ls -d 10.0/*; ls -d 10.1/*; ls -d 10.2/*; ls -d 10.3/*; ls -d 10.4/*;`; do
{
	if [ -h $i ]; then
		#debug_out "$i is a link;";
		continue;
	fi

	VER=`echo $i | cut -d/ -f2`

	check_ver $VER

};
done;

exit 0;
