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