1BEGIN {
2	orig_rs = RS;
3	orig_fs = FS;
4	RS=" ";
5	mod_count = 0;
6	SUBSEP=":";
7}
8
9function get_deps(module_name, module_dir,       depline, cmd)
10{
11	# this could probably be made *much* better
12	RS=orig_rs;
13	FS="[(,) \t]+"
14	cmd = "grep PHP_ADD_EXTENSION_DEP " module_dir "/config*.m4"
15	while (cmd | getline) {
16#		printf("GOT: %s,%s,%s,%s,%s\n", $1, $2, $3, $4, $5);
17		if (!length($5)) {
18			$5 = 0;
19		}
20		mod_deps[module_name, $4] = $5;
21	}
22	close(cmd)
23	RS=" ";
24	FS=orig_fs;
25}
26
27function get_module_index(name,  i)
28{
29	for (i in mods) {
30		if (mods[i] == name) {
31			return i;
32		}
33	}
34	return -1;
35}
36
37function do_deps(mod_idx,        module_name, mod_name_len, dep, ext, val, depidx)
38{
39	module_name = mods[mod_idx];
40	mod_name_len = length(module_name);
41
42	for (ext in mod_deps) {
43		if (substr(ext, 0, mod_name_len+1) != module_name SUBSEP) {
44			continue;
45		}
46		val = mod_deps[ext];
47		ext = substr(ext, mod_name_len+2, length(ext)-mod_name_len);
48
49		depidx = get_module_index(ext);
50		if (depidx >= 0) {
51			do_deps(depidx);
52		}
53	}
54	printf("	phpext_%s_ptr,@NEWLINE@", module_name);
55	delete mods[mod_idx];
56}
57
58function count(arr,       n, i)
59{
60	n = 0;
61	for (i in arr)
62		n++;
63	return n;
64}
65
66/^[a-zA-Z0-9_;-]+/ {
67	split($1, mod, ";");
68
69	# mini hack for pedantic awk
70	gsub("[^a-zA-Z0-9_-]", "", mod[1])
71	# add each item to array
72	mods[mod_count++] = mod[1]
73
74	# see if it has any module deps
75	get_deps(mod[1], mod[2]);
76}
77END {
78	# order it correctly
79	out_count = 0;
80
81	while (count(mods)) {
82		for (i = 0; i <= mod_count - 1; i++) {
83			if (i in mods) {
84				do_deps(i);
85			}
86		}
87	}
88}
89