#! /bin/sh
# This file is in the public domain.
case "$1" in
	"-o")	# generate list of .o files
		exec awk '/^#/ { next; } /^[ 	]*$/ { next; } /^[ 	]/ { printf("alg-%s-%s.o\n",t,$NF); next; } { t = tolower($1); }' | sort | uniq
		;;
	"-c")	# generate C code
		echo '/* This file is in the public domain. */'
		exec awk '
/^#/	{ next; }
/^[ 	]*$/ { next; }
/^[ 	]/ {
		list[type "-" listn[type]] = $1;
		listn[type] ++;
		next;
	}
	{
		type = $1;
		listn[type] = 0;
		next;
	}
END	{
		print "#include \"algs.h\"";
		for (type in listn) {
			for (i=0;i<listn[type];i++) {
				printf("extern %sALG %salg_%s;\n",
					type,tolower(type),list[type "-" i]);
			}
			printf("%sALG *alglist_%s[] = {\n",type,tolower(type));
			for (i=0;i<listn[type];i++) {
				printf("&%salg_%s,\n",
					tolower(type),list[type "-" i]);
			}
			printf("0};\n");
		}
	}'
		;;
esac
