1#!/usr/bin/awk -f
2#
3#
4# Merge two boot loader help files for FreeBSD 3.0
5# Joe Abley <jabley@patho.gen.nz>
6
7BEGIN \
8{
9  state = 0;
10  first = -1;
11  ind = 0;
12}
13
14# beginning of first command
15/^###/ && (state == 0) \
16{
17  state = 1;
18  next;
19}
20
21# entry header
22/^# T[[:graph:]]+ (S[[:graph:]]+ )*D[[:graph:]][[:print:]]*$/ && (state == 1) \
23{
24  match($0, " T[[:graph:]]+");
25  T = substr($0, RSTART + 2, RLENGTH - 2);
26  match($0, " S[[:graph:]]+");
27  SSTART = RSTART
28  S = (RLENGTH == -1) ? "" : substr($0, RSTART + 2, RLENGTH - 2);
29  match($0, " D[[:graph:]][[:print:]]*$");
30  D = substr($0, RSTART + 2);
31  if (SSTART > RSTART)
32    S = "";
33
34  # find a suitable place to store this one...
35  ind++;
36  if (ind == 1)
37  {
38    first = ind;
39    help[ind, "T"] = T;
40    help[ind, "S"] = S;
41    help[ind, "link"] = -1;
42  } else {
43    i = first; j = -1;
44    while (help[i, "T"] help[i, "S"] < T S)
45    {
46      j = i;
47      i = help[i, "link"];
48      if (i == -1) break;
49    }
50
51    if (i == -1)
52    {
53      help[j, "link"] = ind;
54      help[ind, "link"] = -1;
55    } else {
56      help[ind, "link"] = i;
57      if (j == -1)
58        first = ind;
59      else
60        help[j, "link"] = ind;
61    }
62  }
63  help[ind, "T"] = T;
64  help[ind, "S"] = S;
65  help[ind, "D"] = D;
66
67  # set our state
68  state = 2;
69  help[ind, "text"] = 0;
70  next;
71}
72
73# end of last command, beginning of next one
74/^###/ && (state == 2) \
75{
76  state = 1;
77}
78
79(state == 2) \
80{
81  sub("[[:blank:]]+$", "");
82  if (help[ind, "text"] == 0 && $0 ~ /^[[:blank:]]*$/) next;
83  help[ind, "text", help[ind, "text"]] = $0;
84  help[ind, "text"]++;
85  next;
86}
87
88# show them what we have (it's already sorted in help[])
89END \
90{
91  node = first;
92  while (node != -1)
93  {
94    printf "################################################################################\n";
95    printf "# T%s ", help[node, "T"];
96    if (help[node, "S"] != "") printf "S%s ", help[node, "S"];
97    printf "D%s\n\n", help[node, "D"];
98    for (i = 0; i < help[node, "text"]; i++)
99      printf "%s\n", help[node, "text", i];
100    node = help[node, "link"];
101  }
102  printf "################################################################################\n";
103}
104