summaryrefslogtreecommitdiffstats
path: root/urpmi.addmedia
blob: 468e675d9d2fad1f17761c8c44aba8eed51392a6 (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
106
107
108
109
110
111
112
#!/usr/bin/perl

my $DIR = "/var/lib/urpmi";
my $DIR2 = "/etc/urpmi";
my $DEPSLIST = "$DIR/depslist";
my $CFG = "$DIR2/urpmi.cfg";

$| = 1;


sub substInFile(&@) {
    my $f = shift;
    local @ARGV = @_ or return;
    local ($^I, $_) = '';
    while (<>) { &$f($_); print }
}


my @entries = map { /list.(.*)/ } glob("$DIR/list.*");

if ($0 =~ /removemedia/) {
    local ($_) = @ARGV or @entries ? 
	die "missing the entry to remove\n(one of " . join(", ", @entries) . ")\n" :
	die "nothing to remove (use urpmi.addmedia to add a media)\n";
    foreach $e (/^--?a/ ? @entries : @ARGV) {
	my $f;
	$f = "$DIR/hdlist.$e"; unlink("$f.cz2") or warn "failed to remove $f";
	$f = "$DIR/list.$e";   unlink($f) or warn "failed to remove $f";
	substInFile { s/^/\#/ if /^$e\s/ } $CFG;
    }
    system("urpmi.update");
    exit 0;
} elsif ($0 =~ /update/) {
    if (local ($_) = @ARGV) {
	my $regexp = /^--?a/ ? '[^#]\S*' : join('|', @ARGV);
	system("urpmi.addmedia --noupdate $_") foreach grep { /^$regexp\s/ } `cat $CFG`;
    }
    if (glob("$DIR/hdlist.*")) {
	print "generating dependencies list\n";
	system("gendepslist2 $DIR/hdlist.* > $DEPSLIST");
	system("autoirpm.update");
    }
    exit 0;
}

my $noupdate = shift @ARGV if $ARGV[0] eq "--noupdate";
my ($name, $url, $with, $ftp_hdlist) = @ARGV;
my $suffix;

# basic checking of arguments
{
    my $usage = 
"usage: urpmi.addmedia <name> <url>
where <url> is one of
	file://<path>
	ftp://<login>:<password>@<host>/<path> with <relative filename of hdlist>
	ftp://<host>/<path> with <relative filename of hdlist>
	http://<host>/<path> with <relative filename of hdlist>
        removable_<device>_<number>://<path>
";
    $name or die $usage;
    my ($type, $dev, $num) = $url =~ m,^(file|ftp|http|removable_(\w+)_(\d+))://, or die $usage;
    $name =~ s/\s/_/g;

    if ($type eq "removable") {
	$num =~ /^\d+$/ or die "$usage  bad number (is `$num')\n";
	$dev && -e "/dev/$dev" or die "$usage  device `$dev' do not exist\n";
    } elsif ($type eq "ftp") {
	$with eq "with" or die "$usage  `with' missing for ftp media\n";
	$ftp_hdlist or die "$usage  <relative path of hdlist> missing\n";
    }
}

-e $CFG or `echo > $CFG`; # create it if not there
$? == 0 or die "can't write to config file $CFG";
substInFile {
    s/^/\#/ if /^$name\s/;
    $_ .= "$name $url $with $ftp_hdlist\n"  if eof;
} $CFG;


my $HDLIST = "$DIR/hdlist.$name.cz2";
my $RPMLIST = "$DIR/list.$name";

-e $DIR || mkdir $DIR, 0755 or die "can't create $DIR";
!-e $HDLIST || unlink $HDLIST or die "can't remove $HDLIST";

if (my ($prefix, $dir) = $url =~ m,^(removable_.*?|file):/(.*),) {
    if (-e (my $f = "$dir/Mandrake/base/hdlist.cz2")) {
	system("cp -f $f $HDLIST");
    } else {
	system("genhdlist_cz2 -o $HDLIST $dir");
    }
} else {
    system("wget -O $HDLIST $url/$ftp_hdlist");
    $? == 0 or die "wget of $url/$ftp_hdlist failed (maybe wget is missing?)";
}

my $mask = umask 077;
open LIST, ">$RPMLIST" or die "can't output $RPMLIST";
umask $mask;

open F, "bzip2 -dc $HDLIST 2>/dev/null | hdlist2names - |";
foreach (<F>) {
    chomp;
    print LIST "$url/$_\n";
}
close F or die "hdlist2names failed";
close LIST;

system("urpmi.update") unless $noupdate;
exit 0;