#!/usr/bin/env perl use warnings 'FATAL' => 'all'; use strict; use File::Fetch; use IO::Handle; # for autoflush use Cwd; my $PROG = 'perl'; sub err { print STDERR @_, "\n"; exit 2; } sub matchdist { my ($dist) = @_; # Refresh our local list of distributions if needed. my $var = $ENV{'PKGVAR'} or err("$PROG: PKGVAR env variable is unset\n"); if(!-f "$var/cpandists" || -M "$var/cpandists" > 1) { print STDERR "Refreshing local CPAN distribution list..."; my $cwd = getcwd(); chdir($var) or die "chdir: $!"; system('fetchcpan'); err("FAILED") unless($? == 0); print STDERR "OK\n"; chdir($cwd) or die "chdir: $!"; } open(DISTS, '<', "$var/cpandists") or err("$PROG: open: $!"); while() { my @f = split; next unless(lc($f[0]) 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'} || 'ftp://cpan.pair.com'; my $url = "${mirror}/authors/id/${cpath}"; print STDERR "Downloading $file... "; my $ff = File::Fetch->new('uri' => $url); err("FAILED") unless($ff->fetch()); print STDERR "OK\n"; } sub main { my $pkg = shift or die "Usage: $PROG [package name]\n"; my $dist = $pkg; $dist = "app-$dist" if($dist =~ s/^perl-// == 0); STDERR->autoflush(1); my ($realname, $cpath) = matchdist($dist); unless($realname){ print STDERR "$PROG: failed to find perl dist similar to $dist\n"; exit 1; } fetchdist($cpath); print <<"END_META"; url https://metacpan.org/release/$realname source http://search.cpan.org/CPAN/authors/id/$cpath END_META my $file = $cpath; $file =~ s{^.*/}{}; system('perl-dist', $file); return $?; } exit main(@ARGV);