#!/usr/bin/gawk -F '  ' -f

BEGIN {
	split(urls, url_array, " ");
	for(i in url_array){
		split(url_array[i], url_components, ":");
		url_type[i] = url_components[1];
	}
	IFS="  ";
}

# { print $0 }

/^# \S+ HASH$/ {
	hash=tolower(gensub(/^# (\S+) HASH$/, "\\1", "", $0))
	# print "hash='" hash "'"
}

/^[a-fA-F0-9]+  \S+/ {
	files[$2] = 1;
	# print "file: " $2
	verification[$2] = verification[$2] "<hash type='" hash "'>" $1 "</hash>"
}

END {
	print "<?xml version='1.0' encoding='utf-8'?>";
	print "<metalink version='3.0' generator='digest_to_metalink' xmlns='http://www.metalinker.org/'>";
	print "<files>";
	for(file in files){
		print "<file name='" file "'>";
		print "  <verification>";
		print "    " verification[file];
		print "  </verification>";
		print "  <resources>";
		for (i in url_array) {
			url = url_array[i];
			type = url_type[i];
			print "    <url type='" type "' preference='100'>" url "/" file "</url>"
		}
		print "  </resources>";
		print "</file>";
	}
	print "</files>";
	print "</metalink>";
}