summaryrefslogtreecommitdiffstats
path: root/preps/perl.d/fetchcpan
blob: 8065f2893b644922006871689c5234bef06ac9ba (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh

mirror=${CPANMIRROR:-https://mirror.leaseweb.net/CPAN/}
path=/modules/02packages.details.txt.gz

curl --silent "$mirror$path" | zcat 2>/dev/null | awk '
NR < 10 { next }
{
    file = a[split($3, a, "/")]
    ver = a[split(file, a, "[-_]")]
    dist = substr(file, 1, length(file) - length(ver) - 1)
    if(!dist) next # ignore dists with no versions

    sub(/[.]tar[.].*$|[.]zip$/, "", ver)
    sub(/^[vV]/, "", ver)
    sub(/[.]pm$/, "", dist) # srsly?

    if(dist == "perl") next

    mods[dist,ver] = mods[dist,ver] $1 " " $2 "\n"
    if (lessthan(dists[dist], ver)) {
        dists[dist] = ver
        paths[dist] = $3
    }
}

END {
    if(NR == 0) exit(1)

    for (dist in dists) {
        ver = dists[dist]
        print dist, ver, paths[dist] | "sort > cpan.dists"
    }
    close("sort > cpan.dists")

    # Prints modules out in sorted order, too!
    while((getline < "cpan.dists") > 0) {
        print $1 "\n" mods[$1,$2] > "cpan.mods"
    }
}

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
}

'

if [ $? -ne 0 ]; then
    echo "fetchcpan: failed to download package list from CPAN" 1>&2
    exit 1
fi

exit 0