summaryrefslogtreecommitdiffstats
path: root/preps/perl
blob: 112980f06fd48d1c55b56575d430442db4e13f66 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env perl

use warnings 'FATAL' => 'all';
use strict;

use File::Fetch;
use IO::Handle; # for autoflush
use Cwd;

my $PROG = 'preps/perl';
my %BADNAMES = ('perl-libwww' => 'libwww-perl');

sub err
{
	print STDERR "$PROG: ", @_, "\n";
	exit 1;
}

sub matchdist
{
	my($dist) = @_;

	# Refresh our local list of distributions if needed.
	my $var = $ENV{'PKGVAR'}
		or err('PKGVAR env variable is unset');

	if(!-f "$var/cpan.dists" || -M "$var/cpan.dists" > 1) {
		print STDERR "$PROG: Refreshing local CPAN data.\n";
		my $cwd = getcwd();
		chdir $var or die "chdir: $!";
		system 'fetchcpan';
		exit 1 unless($? == 0);
		chdir $cwd or die "chdir: $!";
	}

	open(DISTS, '<', "$var/cpan.dists") or err("open: $!");
	while(<DISTS>) {
		my @f = split;
		my $d = lc $f[0]; $d =~ tr/-_/--/s;
		next unless($d eq lc($dist));
		close(DISTS);
		return ($f[0], $f[2]);
	}
	close(DISTS);
	return ();
}

sub fetchdist
{
	my($cpath) = @_;
	my $file = $cpath; $file =~ s{^.*/}{};
	if(-f $file) {
		print STDERR "$file already downloaded.\n";
		return;
	}

	my $mirror = $ENV{'CPANMIRROR'} || 'https://mirror.leaseweb.net/CPAN/';
	my $url = "${mirror}/authors/id/${cpath}";

	print STDERR "Downloading $file... ";
	my $ff = File::Fetch->new('uri' => $url);
	die "FAILED\n" unless($ff->fetch());
	print STDERR "OK\n";
}

sub main
{
	my $dist = shift or die "usage: $PROG [package name]\n";

	my $guess;
	if($BADNAMES{$dist}){
		$dist = $BADNAMES{$dist};
	}elsif($dist =~ s/^perl-// == 0){
		$guess = 1;
		$dist = "app-$dist";
	}

	STDERR->autoflush(1);
	my ($realname, $cpath) = matchdist($dist);
	unless($realname){
		if($guess){
			return 100
		}else{
			## If its a perl- package tell prepkg to cleanup.
			print STDERR qq{$PROG: failed to find perl dist similar to '$dist'\n};
			return 101
		}
	}
	fetchdist($cpath);

	print <<"END_META";
url
https://metacpan.org/release/$realname

source
https://cpan.metacpan.org/authors/id/$cpath

END_META

	my $file = $cpath; $file =~ s{.*/}{};
	system 'perl-dist' => $file;
	return $? >> 8;
}

exit main(@ARGV);