#!/bin/bash
#
# This counts the prevalence of autotools, in software included
# in a Linux or Unixish distribution.
#
# Run this is in a full checkout of your favorite project (openSUSE:Factory),
# including all tarballs extracted.
#
pkgcount=0;
b_android=0;
b_ant=0;
b_autoconf=0;
b_automake=0;
b_cmake=0;
b_haskell=0;
b_mavenpom=0;
b_perl=0;
b_php=0;
b_python=0;
b_qmake=0;
b_scons=0;
b_waf=0;
b_xmkmf=0;
b_plain=0;
b_none=0;
if [ "$#" -eq 0 ]; then
	set -- *;
fi;

findfile()
{
	local name;
	name="$1";
	shift;
	find "$name"/* -mindepth 1 "$@" | grep -Pqe.;
}

for name in "$@"; do
	if [ ! -d "$name" ]; then
		continue;
	fi;
	if ! find "$name" -mindepth 1 -maxdepth 1 -type f -name "*.spec" | \
	    grep -Pqe .; then
#		echo "$name seems to have no specfile, skipping";
		continue;
	fi;
	pkgcount=$((pkgcount+1));
	found=0;
	if findfile "$name" -iname android.mk; then
		b_android=$((b_android+1));
		found=1;
	fi;
	if findfile "$name" -name build.xml; then
		b_ant=$((b_ant+1));
		found=1;
	fi;
	if findfile "$name" "(" -name configure.ac -o -name configure.in -o \
	    -name config.guess -o -name aclocal.m4 ")"; then
		b_autoconf=$((b_autoconf+1));
		found=1;
	fi;
	if findfile "$name" -iname Makefile.am; then
		b_automake=$((b_automake+1));
		found=1;
	fi;
	if findfile "$name" -iname CMakeLists.txt; then
		b_cmake=$((b_cmake+1));
		found=1;
	fi;
	if findfile "$name" "(" -name Setup.hs -o -name Setup.lhs ")"; then
		b_haskell=$((b_haskell+1));
		found=1;
	fi;
	if findfile "$name" -name pom.xml; then
		b_mavenpom=$((b_mavenpom+1));
		found=1;
	fi;
	if findfile "$name" "(" -iname Makefile.PL -o -name Build.PL ")"; then
		b_perl=$((b_perl+1));
		found=1;
	fi;
	if findfile "$name" -name package.xml; then
		b_php=$((b_php+1));
		found=1;
	fi;
	if findfile "$name" -name setup.py; then
		b_python=$((b_python+1));
		found=1;
	fi;
	if findfile "$name" -iname SConstruct; then
		b_scons=$((b_scons+1));
		found=1;
	fi;
	if findfile "$name" -name "*.pro"; then
		b_qmake=$((b_qmake+1));
		found=1;
	fi;
	if findfile "$name" -name wscript; then
		b_waf=$((b_waf+1));
		found=1;
	fi;
	if findfile "$name" -name Imakefile; then
		b_xmkmf=$((b_xmkmf+1));
		found=1;
	fi;
	if [ "$found" -eq 0 ] && findfile "$name" \
	    "(" -iname Makefile -o -name Makefile.in -o \
	    -name Makefile.unix -o \
	    -iname GNUmakefile ")"; then
		b_plain=$((b_plain+1));
		found=1;
	fi;
	if [ "$found" -eq 0 ]; then
		b_none=$((b_none+1));
		echo "WARNING: Nothing identified for $name";
	fi;
	echo "$name";
done;
cat <<-EOF
	Specfiles:    $pkgcount
	android:      $b_android
	ant:          $b_ant
	autoconf:     $b_autoconf
	automake:     $b_automake
	cmake:        $b_cmake
	haskell:      $b_haskell
	maven/pom:    $b_mavenpom
	perl:         $b_perl
	php:          $b_php
	python:       $b_python
	qmake:        $b_qmake
	scons:        $b_scons
	waf:          $b_waf
	xmkmf:        $b_xmkmf
	plain:        $b_plain
	none/unknown: $b_none
EOF
