1#!/usr/bin/awk -f
2#
3# Copyright (c) 2004 Oliver Eikemeier. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# MOVEDlint - check MOVED for consistency
29#
30# Usage:
31#  [env PORTSDIR=/usr/ports] /usr/ports/Tools/scripts/MOVEDlint.awk
32#
33
34BEGIN {
35    FS = "|"
36    portsdir = ENVIRON["PORTSDIR"] ? ENVIRON["PORTSDIR"] : "/usr/ports"
37    if (ARGC == 1) {
38        ARGV[ARGC++] = portsdir "/MOVED"
39    }
40    if (ENVIRON["BLAME"]) {
41        blame=1
42    }
43    if (blame) {
44	if (!system("test -r " portsdir "/.git")) {
45            blame = "cd " portsdir "; git blame MOVED 2>/dev/null"
46        }
47    }
48    sort = "/usr/bin/sort -n"
49    if (!lastdate) {
50        lastdate="1999-12-31"
51    } else if (lastdate !~ /^20[0-3][0-9]-[01][0-9]-[0-3][0-9]$/) {
52	    printf "Invalid date format '%s' expecting YYYY-MM-DD\n", lastdate
53	    exit(1)
54    }
55}
56
57/^(#|$)/ {
58    next
59}
60
61!started && $3 < lastdate {
62	next
63}
64
65!started && $3 >= lastdate {
66	started = 1
67}
68
69NF != 4 {
70    printf "%5d: format is from|to|date|reason, detected %d field(s) \n", NR, NF | sort
71    error[NR] = 1
72    next
73}
74
75$1 !~ /^[^\/]+\/[^\/]+$/ || $2 !~ /^([^\/]+\/[^\/]+)?$/ {
76    printf "%5d: source and destination must be category/port\n", NR | sort
77    error[NR] = 1
78    next
79}
80
81$3 !~ /^20[0-3][0-9]-[01][0-9]-[0-3][0-9]$/ {
82    printf "%5d: missing YYYY-MM-DD date\n", NR | sort
83    error[NR] = 1
84    next
85}
86
87$1 ~ /[ \t]/ {
88	printf "%5d: '%s' contains spaces\n", NR, $1 | sort
89	error[NR] = 1
90	next
91}
92
93$2 ~ /[ \t]/ {
94	printf "%5d: '%s' contains spaces\n", NR, $2 | sort
95	error[NR] = 1
96	next
97}
98
99{
100    if ($1 in srcs) {
101        printf "%5d: %s has duplicate entries\n", NR, $1 | sort
102        error[NR] = 1
103        next
104    }
105    srcs[$1] = 1
106
107    if (lastdate > $3) {
108        printf "%5d: date going backwards from %s to %s from this line\n", NR-1, lastdate, $3 | sort
109        error[NR-1] = 1
110        printf "%5d: date going backwards from %s to %s to this line\n", NR, lastdate, $3 | sort
111        error[NR] = 1
112    }
113    lastdate = $3
114
115    from_flavor=""
116    if ($1 ~ "@") {
117        from_flavor=$1
118        sub("@.*", "", $1)
119        sub(".*@", "", from_flavor)
120    }
121
122    if (system("test -f " portsdir "/" $1 "/Makefile")) {
123        delete missing[$1]
124    } else {
125        if (from_flavor != "") {
126            if (!system("test \"" from_flavor "\" = \"`make -C " portsdir "/" $1 " -VFLAVORS:M" from_flavor " __MAKE_CONF=/dev/null`\"")) {
127                printf "%5d: %s still has the %s flavor\n", NR, $1, from_flavor | sort
128            }
129            # No else because the port is there but does not have the flavor,
130            # so it should be ok.
131        } else {
132	    if ($2 ~ $1 "@") {
133                printf "%5d: %s is a flavor of %s, the line should be removed\n", NR, $2, $1 | sort
134	    } else {
135                printf "%5d: %s must be marked as resurrected\n", NR, $1 | sort
136            }
137        }
138    }
139
140    if ($2) {
141        to_flavor=""
142        if ($2 ~ "@") {
143            to_flavor=$2
144            sub("@.*", "", $2)
145            sub(".*@", "", to_flavor)
146        }
147
148        if (system("test -f " portsdir "/" $2 "/Makefile"))
149            missing[$2] = NR
150        else
151            if (to_flavor != "") {
152                if (system("test \"" to_flavor "\" = \"`make -C " portsdir "/" $2 " -VFLAVORS:M" to_flavor " __MAKE_CONF=/dev/null`\"")) {
153                    printf "%5d: %s does not have the %s flavor\n", NR, $2, to_flavor | sort
154                    error[NR] = 1
155                }
156            }
157    }
158
159#    Produces too many false positives
160#    if ($4 ~ /^[a-z].*/) {
161#       printf "Initial value of 'reason' is lowercase: %5d (%s)\n", NR, $4 | sort
162#       error[NR] = 1
163#    }
164
165    if ($4 ~ /\.$/) {
166        printf "%5d: Final character is a dot: (%s)\n", NR, $4 | sort
167        error[NR] = 1
168    }
169}
170
171END {
172    for (port in missing) {
173        printf "%5d: %s not found\n", missing[port], port | sort
174        error[missing[port]] = 1
175    }
176
177    if (blame) {
178        line = 1
179        while (blame | getline) {
180            if (error[line])
181                printf "%5d!\n%5d! %s\n", line, line, $0 | sort
182            line++
183        }
184        close(blame)
185    }
186
187    close(sort)
188    if (length(error) > 0)
189	    exit(1)
190}
191