1#! @AWK@ -f
2#
3# -*-awk-*-
4#
5#     Copyright (C) 1998-2000  Thomas Roessler <roessler@guug.de>
6#                   2000-2016  Roland Rosenfeld <roland@spinnaker.de>
7#                   2007       Yaroslav Halchenko <debian@onerussian.com>
8#
9#     This program is free software; you can redistribute it and/or modify
10#     it under the terms of the GNU General Public License as published by
11#     the Free Software Foundation; either version 2 of the License, or
12#     (at your option) any later version.
13#
14#     This program is distributed in the hope that it will be useful,
15#     but WITHOUT ANY WARRANTY; without even the implied warranty of
16#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#     GNU General Public License for more details.
18#
19#     You should have received a copy of the GNU General Public License
20#     along with this program; if not, write to the Free Software Foundation,
21#     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,, USA.
22
23function print_line (line, lastdate, count)
24{
25    if (line == "") return;
26    if (limitdate != "" && lastdate < limitdate && count < limitcount) return;
27    if (limitcount == 0)
28	print line;
29    else
30	print line, FS, count;
31}
32
33BEGIN {
34    # initialization
35    FS = "\t";
36    i = 0;
37
38    # default settings
39    keeporder = 0;                  # do not keep order by default
40    limitdate = "0000-00-00_00:00"; # keep all dates by default
41    limitcount = 0;                 # and do not limit by count by default
42}
43
44{
45    line[$1] = ($1 FS $2 FS $3);
46
47    if (lastdate[$1] == "" || lastdate[$1] < $3)
48        lastdate[$1] = $3;
49
50    line[$1] = ($1 FS $2 FS lastdate[$1]);
51
52    if (line[$4] != "")
53	cnt[$1] += line[$4];
54    else
55        cnt[$1]++;
56
57    if (keeporder == 1)
58    {
59        pos[$1] = i;
60        idx[i++] = $1;
61    }
62}
63
64END {
65    if (keeporder == 1)
66       for (j = 0; j < i; j++) {
67            if (pos[idx[j]] == j) {
68                print_line(line[idx[j]], lastdate[idx[j]], cnt[idx[j]]);
69            }
70        }
71    else
72        for (a in line) {
73            print_line(line[a], lastdate[a], cnt[a]);
74        }
75}
76