summaryrefslogtreecommitdiffstats
path: root/bin/metas/perl.d/cpandists
blob: 9273bd4a399fa67631fa7370e521ce0545826b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/sh

mirror=${CPANMIRROR:-ftp://cpan.pair.com}
path=/modules/02packages.details.txt.gz

curl --silent $mirror$path | gzip -dc | awk '
NR < 10 { next }
{
    file = a[split($3, a, "/")]
    len = split(file, a, ".")

    if (!match(a[1], /[-_][vV]?[0-9]+$/)) {
        print "error: failed to grok " $3 | "cat 1>&2"
        next
    }
    ver  = substr(file, RSTART+1, RLENGTH-1)
    dist = substr(file, 1, RSTART-1)
    for (i=2; i<=len; i++) {
        if (a[i] !~ /^[0-9]/) break
        ver = ver "." a[i]
    }
    sub(/^[vV]/, "", ver)

    if (lessthan(dists[dist], ver)) {
        dists[dist] = ver
        paths[dist] = $3
    }
}

END {
    for (dist in dists) print dist, dists[dist], paths[dist] | "sort"
}

function lessthan (l, r)
{
    return decver(l) < decver(r)
}

function decver (vs)
{
    pcnt = gsub(/[.]/, ".", vs)
    if (pcnt < 2) return vs

    len = split(vs, vc, ".")
    dec = vc[1]
    for (i=2; i<=len; i++) dec += (10 ^ (-i * 3)) * vc[i]
    return dec
}

'