#!/bin/sh

DO_DEBUG=0

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 "checking $V"

	COUNT=`grep mysql ../../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 mysql | grep -c ${V}`

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

		return;
	fi

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

for i in `ls mysql-*.tar.gz`; do
{
	if [ -h $i ]; then
		debug_out "$i is a link;";
		continue;
	fi

	VER=`echo $i | cut -d- -f2 | cut -d. -f1,2,3`

	check_ver $VER

};
done;

for i in `ls -d 5.5/*; ls -d 5.6/*; ls -d 5.7/*`; 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;
