tllocalmgr 25.8 KB
Newer Older
1
#!/usr/bin/perl
2
use 5.010;
3
use warnings;
firmicus's avatar
firmicus committed
4
use strict;
5
6
use File::Path;
use LWP::Simple;
7
require LWP::UserAgent;
8
9
10
use Term::ANSIColor qw(:constants);
use Term::ShellUI;
use Getopt::Long;
11
use lib ('tlpkg', "/var/lib/texmf/arch/tlpkg");
12
use TeXLive::Arch;
firmicus's avatar
firmicus committed
13

14
15
16
17
18
19
die "You can't run this program as root\n" if $ENV{'USER'} eq "root";

# Paths
my $TEXMFVAR = "/var/lib/texmf";
my $INSTALLEDPKGS = "$TEXMFVAR/arch/installedpkgs";

20
21
22
our $VERSION = "0.4.6";
our $YEAR = "2014";
my $CTANBASEURL="http://mirror.ctan.org/systems/texlive/tlnet";
23
24
25
26
27
28
29

my $ROOT = "$ENV{HOME}/.texlive/texmf-var/arch";
my $LOCALDB = "$ROOT/tlpkg/texlive.tlpdb";
my $LOCALDBXZ = "$LOCALDB.xz";
my $logfile = "$ROOT/tllocalmgr.log";

############# Option parsing ##########
firmicus's avatar
firmicus committed
30
31
my $opts = {};
GetOptions( $opts,
32
33
34
35
36
			'skipupdate',
			'forceupdate',
			'localsearch', #TODO
			'location', #TODO ?
			'mirror',
Robson R S Peixoto's avatar
Robson R S Peixoto committed
37
			'nocolor',
firmicus's avatar
firmicus committed
38
39
			'help|h',
			'version|v'
firmicus's avatar
firmicus committed
40
		);
41
die usage(), "\n" if $opts->{'help'};
firmicus's avatar
firmicus committed
42
die version(), "\n" if $opts->{'version'};
Francois Charette's avatar
Francois Charette committed
43
44
45
46
my $skipupdate = $opts->{'skipupdate'} || 0;
my $forceupdate = $opts->{'forceupdate'} || 0;
my $location = $opts->{'location'} || $ROOT;
my $mirror = $opts->{'mirror'} || $CTANBASEURL;
47
$ENV{ANSI_COLORS_DISABLED} = 1 if $opts->{'nocolor'};
Francois Charette's avatar
Francois Charette committed
48
my $localsearch = $opts->{'localsearch'} || 0;
firmicus's avatar
firmicus committed
49

50
# Main URLs
51
my $TLARCHIVE="$mirror/archive";
52
my $TLPDB="$mirror/tlpkg/texlive.tlpdb.xz";
firmicus's avatar
firmicus committed
53

54
55
56
57
$Term::ANSIColor::AUTORESET = 1; 
my $term = new Term::ShellUI( commands => get_commands());
$term->prompt( "tllocalmgr> " );

58
59
########### INITIALIZATION ###########

60
unless ( -d $ROOT ) {
Francois Charette's avatar
Francois Charette committed
61
	system("mkdir -p $ROOT/tlpkg")
62
63
}

firmicus's avatar
firmicus committed
64
65
66
67
68
69
70
71
open LOG, ">> $logfile";
my $initlog = 0;
sub initlog {
	my @lt = localtime(time);
	print LOG "\n\n";
	print LOG "*** ", $lt[5]+1900, " ", $lt[4], " ", $lt[3], " ", $lt[2], ":", $lt[1], ":", $lt[0], " ***\n";
	$initlog = 1;
}
72

73
74
75
76
77
78
79
my @dbstats;
my $ageofdb;
if (-f $LOCALDB) {
    @dbstats = stat($LOCALDB);# to know how old the local db is
} else {
    $forceupdate = 1
}
80
81
82

# only retrieve if local DB is older than 12 hours
unless ($skipupdate) {
83
84
85
    unless ($forceupdate) {
    	$ageofdb = time - $dbstats[9] ;
    }
86
87
	if ($forceupdate || $ageofdb > 3600*12) {
		print "Retrieving new TeXLive database from CTAN...\n";
firmicus's avatar
firmicus committed
88
		initlog() unless $initlog;
89
		print LOG "* Retrieving new TeXLive database from CTAN...\n\t$TLPDB";
90
91
92
93
		my $agent = LWP::UserAgent->new;
		my $rc = $agent->get($TLPDB, ":content_file" => $LOCALDBXZ);
		unless ($rc->is_success) {
			die "could not fetch $TLPDB :\n" . $rc->status_line();
94
95
        };
        
96
97
		system("test -f $LOCALDB && mv $LOCALDB $LOCALDB.old");
		system("unxz $LOCALDBXZ");
98
99
100
101
	}
};

print GREEN, BOLD, "Initializing ...", RESET, "\n" ;
firmicus's avatar
firmicus committed
102

103
### read DB
firmicus's avatar
firmicus committed
104

105
my $tlpdb = new TeXLive::Arch (root => $ROOT) || die "cannot read $ROOT/tlpkg/texlive.tlpdb";
firmicus's avatar
firmicus committed
106

107
my %tlpackages = $tlpdb->archpackages;
firmicus's avatar
firmicus committed
108

109
my %versions = $tlpdb->archversions;
firmicus's avatar
firmicus committed
110

111
112
113
114
# list of all packages in the database that interest us
# i.e. less than $tlpdb->list_packages !
my @allpackages; 

firmicus's avatar
firmicus committed
115
my %seen = ();
firmicus's avatar
firmicus committed
116
117
118
foreach my $coll (keys %tlpackages) {
	my @tmp;
	foreach my $pkg (@{$tlpackages{$coll}}) {
119
120
        my $tlpkg = $tlpdb->get_package($pkg);
		push @tmp, $tlpkg->revision;
firmicus's avatar
firmicus committed
121
122
123
124
		unless (exists $seen{$pkg}) {
			push @allpackages, $pkg
		};
		$seen{$pkg}++;
firmicus's avatar
firmicus committed
125
126
127
128
129
	};
	@tmp = sort {$a <=> $b} @tmp;
	$versions{$coll} = pop @tmp
}

130
131
132
my %installedcol_pkgs; # list of packages in each installed collection
my %installedcol_versions; # version of each installed coll
my %installedpkg_versions; # version of each installed pkg
firmicus's avatar
firmicus committed
133
134
135
136
my %locallyinstalledpkg_versions; # version of each locally installed pkg 
my %locallyinstalleddoc_versions; # version of each locally installed doc pkg
#my @installed_lists = <$INSTALLEDPKGS/*.pkgs> ;
#my @installed_cols; # collections installed
137

firmicus's avatar
firmicus committed
138
139
chomp(my @pacman = `pacman -Qs ^texlive- | egrep ^local | cut -d'/' -f2`);
my @localpacman = grep { /^texlive-local-/ } @pacman;
firmicus's avatar
firmicus committed
140
141
142
# To have the standard collections we get rid of 
# texlive-local-<pkg> and texlive-localmanager:
my @collpacman = grep { !/^texlive-local/ } @pacman; 
143
144
145
146
147
148
149
150

if (@localpacman) {
	foreach (@localpacman) {
		my @tmp = split;
		my $n = $tmp[0];
		$n =~ s/^texlive-local-//;
		my $v = $tmp[1] ;
		$v =~ s/-[0-9]+$//;
firmicus's avatar
firmicus committed
151
152
153
154
155
156
157
		if ($n =~ /-doc$/ ) {
			$n =~ s/-doc$//;
			$locallyinstalleddoc_versions{$n} = $v;
		}
		else {
			$locallyinstalledpkg_versions{$n} = $v;
		}
158
159
	}
}
firmicus's avatar
firmicus committed
160

firmicus's avatar
firmicus committed
161
162
163
164
165
166
167
if (@collpacman) {
	foreach (@collpacman) {
		my @tmp = split;
		my $col = $tmp[0];
		next if $col eq 'texlive-bin';
		$col =~ s/^texlive-//;
		my $ver = $tmp[1];
168
		$ver =~ s/[0-9]{4}\.([0-9]+)-[0-9]+$/$1/;
firmicus's avatar
firmicus committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
		#print "debug: $col - $ver\n";
		$installedcol_versions{$col} = $ver;
		my @list = ();
		unless ( -f "$INSTALLEDPKGS/texlive-$col\_$ver.pkgs" ) {
			die "Cannot open $INSTALLEDPKGS/texlive-$col\_$ver.pkgs\n"
		};
		open INST, "<$INSTALLEDPKGS/texlive-$col\_$ver.pkgs" ;
		while (<INST>) {
			next if /^\s*$/;
			chomp( (my $name, my $ver) = split / /, $_ );
			$installedpkg_versions{$name} = $ver unless (exists $installedpkg_versions{$name}) ; 
			push @list, $name;
		};
		close INST;
		$installedcol_pkgs{$col} = [ @list ];
	}
}
else {
	die "Huh? You don't seem to have installed texlive on this system!\n"
188
189
}

firmicus's avatar
firmicus committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#foreach (@installed_lists) {
#	my ($col, $ver) = $_ =~ m/^.+\/texlive-(.+)\_(.+)\.pkgs$/;
#	$installedcol_versions{$col} = $ver;
#	my @tmp = ();
#	open INST, "<$_";
#	while (<INST>) {
#		next if /^\s*$/;
#	    chomp( (my $name, my $ver) = split / /, $_ );
#		$installedpkg_versions{$name} = $ver unless (exists $installedpkg_versions{$name}) ; 
#		push @tmp, $name;
#	};
#	close INST;
#	$installedcol_pkgs{$col} = [ @tmp ];
#}

205
206
207
my %collectionsbypkg; # collection to which belongs each pkg

foreach my $col (keys %tlpackages) {
firmicus's avatar
firmicus committed
208
	next if $col =~ /-doc$/;
209
210
211
	foreach my $pkg (@{$tlpackages{$col}}) {
		$collectionsbypkg{$pkg} = $col
	}
firmicus's avatar
firmicus committed
212
213
}

firmicus's avatar
firmicus committed
214
215
216
217
218
219
220
221
222
# TODO 
#my %doccollectionsbypkg; # doc collection to which belongs each pkg
#foreach my $col (keys %tlpackages) {
#	next unless $col =~ /-doc$/;
#	foreach my $pkg (@{$tlpackages{$col}}) {
#		$doccollectionsbypkg{$pkg} = $col
#	}
#}

223
224
my %updatedcol_pkgs; # pkgs for which updates are available, by collection
my @updatedpkgs; # pkgs for which updates are available
firmicus's avatar
firmicus committed
225

226
foreach my $col (sort keys %installedcol_versions) {
firmicus's avatar
firmicus committed
227
	#my $installedver = `pacman -Q texlive-$col`;
228
	#$installedver =~ s/texlive-$col $YEAR\.([0-9]+)-.+/$1/;
229
	next unless ( $installedcol_versions{$col} < $versions{$col} ) ;
firmicus's avatar
firmicus committed
230
	foreach my $pkg (sort @{$tlpackages{$col}}) {
231
232
        my $tlpkg = $tlpdb->get_package($pkg);
		my $newver = $tlpkg->revision;
233
234
		if ( exists $installedpkg_versions{$pkg} ) {
			my $oldver = $installedpkg_versions{$pkg} ;
firmicus's avatar
firmicus committed
235
			if ($newver > $oldver) {
236
				push @{$updatedcol_pkgs{$col}}, $pkg;
firmicus's avatar
firmicus committed
237
238
				push @updatedpkgs, $pkg;
			}
239
240
		} else { # new pkg
			push @{$updatedcol_pkgs{$col}}, $pkg;
firmicus's avatar
firmicus committed
241
242
243
244
245
			push @updatedpkgs, $pkg;
		};
	}
}

246
247
248
my @pkglist;
if ( $localsearch ) {
   @pkglist = keys %installedpkg_versions
firmicus's avatar
firmicus committed
249
} else {
250
   @pkglist = @allpackages
firmicus's avatar
firmicus committed
251
252
};

253
##############################################
firmicus's avatar
firmicus committed
254

255
256
257
258
no strict 'refs';
if (@ARGV) {
	my $cmd = shift @ARGV;
	my $runcmd = "run_$cmd";
firmicus's avatar
firmicus committed
259
260
261
262
263
264
265
266
	if ($cmd =~ /^(help|usage)/ ) {
		print usage(); 
	} 
	else {
		eval { &$runcmd(@ARGV) };
		die "Error: command $cmd is not defined\n" if $@;
		exit 0 unless $cmd eq 'status';
	}
267
}
firmicus's avatar
firmicus committed
268

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
print "Welcome to the TeXLive Local Manager shell. Type 'help' for assistance.\n";

########### END OF INITIALIZATION ###########

$term->run();

sub get_commands
{
	return {
		"status" => { desc => "Current status of TeXLive installation",
                      proc => \&run_status }, 
		"install" => { desc => "Locally install new CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_install(@_) } }, 
		"installdoc" => { desc => "Locally install documentation of CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_installdoc(@_) } }, 
		"installsrc" => { desc => "Locally install sources of CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_installsrc(@_) } }, 
		"update" => { desc => "Locally update CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_update(@_) } }, 
		"?" =>		{ alias => "help" },
		"h" =>		{ alias => "help", exclude_from_completion=>1 },
		"help" => 	{ desc => "Print helpful information",
					  args => sub { shift->help_args(undef, @_); },
					  method => sub { shift->help_call(undef, @_); } }, 
firmicus's avatar
firmicus committed
301
302
303
		"usage" => 	{ desc => "Print more detailed help",
					  maxargs => 0,
					  proc => sub { print usage() } },
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
		"quit" =>	{ desc => "Quit tllocalmgr",
					  maxargs => 0,
                      method => sub { shift->exit_requested(1); } }, 
		"q" =>		{ alias => "quit" },
		"exit" =>	{ alias => "quit" },
		"clean" =>	{ desc => "Clean local build tree",
					  proc => \&run_clean },
		"info"	=>  { desc => "Print info on CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_info(@_) } },
		"shortinfo"	=>  { desc => "Print a one-liner description of CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_shortinfo(@_) } },
		"listfiles"	=>  { desc => "List all files in CTAN packages",
					  args => \&complete_pkgname ,
					  minargs => 1,
                      proc => sub { run_listfiles(@_) } },
		"search"	=>  { desc => "Search info on CTAN packages",
					  minargs => 1,
                      proc => sub { run_search(@_) } },
		"s" =>		{ alias => "search" },
		"searchfiles" =>  { desc => "Search for files in CTAN packages",
					  minargs => 1,
                      proc => sub { run_searchfiles(@_) } },
		"sf" =>		{ alias => "searchfiles" },
		"texhash" => { desc => "Refresh the TeX file database",
					   maxargs => 0,
					   proc => sub { system('sudo texhash') } }
firmicus's avatar
firmicus committed
334
	};
335
336
337
}

sub echo {
firmicus's avatar
firmicus committed
338
339
340
341
342
	print @_, "\n"
}

sub warning {
	print RED @_, "\n"
343
344
345
346
347
348
349
350
351
}

sub run_clean {
	print "Are you sure? (y/N) > ";
	chomp(my $choice = <STDIN>) ;
	rmtree("$ROOT/builds") if $choice =~ m/^y/i ;
}

sub run_status {
firmicus's avatar
firmicus committed
352
	initlog() unless $initlog;
353
354
355
356
357
	print LOG "* List of updated or new packages for each collection:\n";

	foreach my $col (sort keys %updatedcol_pkgs) {
		print LOG " - texlive-$col :\n";
		print BOLD RED "texlive-$col :\n";
358
    my $colver = $installedcol_versions{$col};
359
		foreach my $pkg (sort @{$updatedcol_pkgs{$col}}) {
firmicus's avatar
firmicus committed
360
			next if $locallyinstalledpkg_versions{$pkg};
361
362
363
			my $newver = $tlpdb->package_revision($pkg);
			if ( $installedpkg_versions{$pkg} ) {
				my $oldver = $installedpkg_versions{$pkg} ;
364
				if ($newver > $oldver && $newver > $colver) {
365
366
367
					print GREEN, "\t$pkg", RESET, " has been updated ($oldver => $newver)\n";
					print LOG "\t$pkg has been updated ($oldver => $newver)\n";
				}
firmicus's avatar
firmicus committed
368
369
370
371
372
			} 
			else {
					print GREEN, "\t$pkg", RESET, " is new!\n";
					print LOG "\t$pkg is new (as of rev $newver)\n";
			}
373
		}
firmicus's avatar
firmicus committed
374
375
376
377
378
379
380
381
	};
	my @locpkgs = keys %locallyinstalledpkg_versions; 
	if (@locpkgs) {
		print BOLD RED "Locally installed packages:", RESET, "\n";
		print LOG " - locally installed packages :\n";
		my $count = 0;
		foreach my $pkg (sort @locpkgs) {
			my $localver = $locallyinstalledpkg_versions{$pkg};
382
383
            my $tlpkg = $tlpdb->get_package($pkg);
            my $newver = $tlpkg->revision || "0";
firmicus's avatar
firmicus committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
			print LOG "Warning: revision of $pkg is problematic\n" if $newver eq '0';
			if ( $newver > $localver ) {
				$count++;
				print GREEN, "\t$pkg", RESET, " has been updated ($localver => $newver)\n";
				print LOG "\t$pkg has been updated ($localver => $newver)\n";
			}
		};
		print "\t(No updates)\n" unless $count;
	};
	my @locdocs = keys %locallyinstalleddoc_versions; 
	if (@locdocs) {
		print BOLD RED "Locally installed doc packages:", RESET, "\n";
		print LOG " - locally installed doc packages :\n";
		my $count = 0;
		foreach my $pkg (sort @locdocs) {
			my $localver = $locallyinstalleddoc_versions{$pkg};
400
401
            my $tlpkg = $tlpdb->get_package($pkg);
            my $newver = $tlpkg->revision || "0";
firmicus's avatar
firmicus committed
402
403
404
405
406
407
408
409
			print LOG "Warning: revision of $pkg is problematic\n" if $newver eq '0';
			if ( $newver > $localver ) {
				$count++;
				print GREEN, "\t$pkg", RESET, " has been updated ($localver => $newver)\n";
				print LOG "\t$pkg has been updated ($localver => $newver)\n";
			}
		};
		print "\t(No updates)\n" unless $count;
410
411
412
413
414
	}
}

sub run_install {
	foreach (@_) {
415
416
        my $tlpkg  = $tlpdb->get_package($_);
        my $pkgrev = $tlpkg->revision;
417
		if ( $locallyinstalledpkg_versions{$_} ) {
firmicus's avatar
firmicus committed
418
			echo "$_ is already installed with texlive-local-$_";
419
			if ($locallyinstalledpkg_versions{$_} == $pkgrev) {
420
421
422
423
424
425
426
427
428
				echo "and it is uptodate"
			} else {
				print "Do you want to update it locally? (Y/n) > " ;
				chomp(my $choice = <STDIN>) ;
				next if $choice =~ m/^n/i;
				makepkg($_);
			}
		}
		elsif ( $installedpkg_versions{$_} ) {
firmicus's avatar
firmicus committed
429
			echo "$_ is already installed with texlive-$collectionsbypkg{$_}";
430
			if ($installedpkg_versions{$_} == $pkgrev) {
431
432
433
434
435
436
437
438
				echo "and it is uptodate"
			} else { 
				#TODO check if it was already locally installed
				print "Do you want to update it locally? (Y/n) > " ;
				chomp(my $choice = <STDIN>) ;
				next if $choice =~ m/^n/i;
				makepkg($_);
			}
439
		} elsif (! $pkgrev) {
firmicus's avatar
firmicus committed
440
			warning "$_ is unknown" ;
441
442
443
444
445
446
447
448
449
450
			next ;
		} else {
			#TODO check if texlive-local-$_ was already locally installed
			makepkg($_)
		}
   }
}

sub run_installdoc {
	foreach (@_) {
451
452
        my $tlpkg  = $tlpdb->get_package($_);
        my $pkgrev = $tlpkg->revision;
firmicus's avatar
firmicus committed
453
		if ( $locallyinstalleddoc_versions{$_} ) {
firmicus's avatar
firmicus committed
454
			echo "$_ is already installed with texlive-local-$_-doc";
455
			if ($locallyinstalleddoc_versions{$_} == $pkgrev) {
456
457
458
459
460
461
462
463
				echo "and it is uptodate"
			} else {
				print "Do you want to update it locally? (Y/n) > " ;
				chomp(my $choice = <STDIN>) ;
				next if $choice =~ m/^n/i;
				makepkgdoc($_);
			}
		}
464
		elsif (! $pkgrev) {
firmicus's avatar
firmicus committed
465
			warning "$_ is unknown" ;
466
467
468
469
470
471
472
473
474
			next ;
		} 
		else {
			makepkgdoc($_)
		}
   }
}

sub run_installsrc {
firmicus's avatar
firmicus committed
475
	echo 'Sorry, not implemented yet'
476
477
478
479
}

sub run_update {
	foreach (@_) {
480
481
        my $tlpkg  = $tlpdb->get_package($_);
        my $pkgrev = $tlpkg->revision;
482
		if ( $locallyinstalledpkg_versions{$_} ) {
firmicus's avatar
firmicus committed
483
			echo "$_ is already installed with texlive-local-$_";
484
			if ($locallyinstalledpkg_versions{$_} == $pkgrev) {
485
486
487
488
489
490
491
492
493
				echo "and it is uptodate"
			} else {
				print "Do you want to update it locally? (Y/n) > " ;
				chomp(my $choice = <STDIN>) ;
				next if $choice =~ m/^n/i;
				makepkg($_);
			}
		}
		elsif ( $installedpkg_versions{$_} ) {
firmicus's avatar
firmicus committed
494
			echo "$_ is already installed with texlive-$collectionsbypkg{$_}";
495
			if ($installedpkg_versions{$_} == $pkgrev) {
496
497
498
499
500
501
502
				echo "and it is uptodate"
			} else { 
				print "Do you want to update it locally? (Y/n) > " ;
				chomp(my $choice = <STDIN>) ;
				next if $choice =~ m/^n/i;
				makepkg($_);
			}
503
		} elsif (! $pkgrev) {
firmicus's avatar
firmicus committed
504
			warning "$_ is unknown" ;
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
			next ;
		} else {
			echo "$_ is not installed" ;
			print "Do you want to update it locally? (Y/n) > " ;
			chomp(my $choice = <STDIN>) ;
			next if $choice =~ m/^n/i;
			makepkg($_);
		}
   }
}

sub run_shortinfo {
	foreach (@_) {
		my $pkg = $tlpdb->get_package($_);
		if ($pkg) {
			print BOLD, GREEN, $_, RESET, ": ", $pkg->shortdesc, "\n";
		} else {
firmicus's avatar
firmicus committed
522
			warning "$_ is unknown";
523
524
525
526
527
528
529
530
		}
	}
}

sub run_info {
	foreach (@_) {
		my $pkg = $tlpdb->get_package($_);
		if ($pkg) {
firmicus's avatar
firmicus committed
531
			print BOLD, BLUE, $_, RESET, ":\n";
532
533
534
535
536
        	print CYAN, "  Category: ", RESET, $pkg->category, "\n";
	        print CYAN, "  ShortDesc: ", RESET, $pkg->shortdesc, "\n" if ($pkg->shortdesc);
    	    print CYAN, "  LongDesc: ", RESET, $pkg->longdesc, "\n" if ($pkg->longdesc);
    	    print CYAN, "  Size: ", RESET, $pkg->containersize, "\n" if ($pkg->containersize);
			print CYAN, "  Collection: ", RESET, "texlive-", $collectionsbypkg{$_}, "\n" if $collectionsbypkg{$_};
firmicus's avatar
firmicus committed
537
538
539
540
541
542
543
544
545
        	print CYAN, "  Revision: ", RESET, $pkg->revision , "\n" if ($pkg->revision);
			if ($locallyinstalledpkg_versions{$_}) { 
				my $ver = $locallyinstalledpkg_versions{$_};
        		print CYAN, "  Installed: ", RESET, "Yes (as texlive-local-$_ version $ver)\n"
			}
			else {
        		print CYAN, "  Installed: ", RESET, 
				(exists $installedpkg_versions{$_} ? "Yes\n" : "No\n")
			};
546
547
	        print "\n";
		} else {
firmicus's avatar
firmicus committed
548
			warning "$_ is unknown";
549
550
551
552
553
554
555
556
557
		}
	}
}

sub run_listfiles {
	foreach (@_) {
		my $pkg = $tlpdb->get_package($_);
		if ($pkg) {
			my @allfiles = $pkg->all_files;
firmicus's avatar
firmicus committed
558
			print BOLD, BLUE, $_, RESET, ":\n";
559
560
561
562
563
			foreach (@allfiles) {
				print "\t", $_, "\n";
			};
	        print "\n";
		} else {
firmicus's avatar
firmicus committed
564
			warning "$_ is unknown";
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
		}
	}
}

sub run_search {
  my $r = shift;
  my $ret = "";
  my @pkglist;
  if ( $localsearch ) {
	  @pkglist = keys %installedpkg_versions
  } else {
	  @pkglist = $tlpdb->list_packages
  };
  foreach my $pkg (@pkglist) {
      next if ($pkg =~ m/\./);
      my $t = $tlpdb->get_package($pkg)->shortdesc;
      $t |= "";
      my $lt = $tlpdb->get_package($pkg)->longdesc;
      $lt |= "";
      if (($pkg =~ m/$r/) || ($t =~ m/$r/) || ($lt =~ m/$r/)) {
        $ret .= " $pkg - $t\n";
      }
    }
  print $ret;
}

sub run_searchfiles {
  my $r = shift ;
  foreach my $pkg (@pkglist) {
      my @ret = grep(m;$r;, $tlpdb->get_package($pkg)->all_files);
      if (@ret) {
firmicus's avatar
firmicus committed
596
        print BOLD BLUE "$pkg:\n";
597
598
599
600
601
602
603
604
605
606
        foreach (@ret) {
          print "\t$_\n";
        }
      }
    }
}

sub makepkg {
	my @stack = @_;
	
firmicus's avatar
firmicus committed
607
	initlog() unless $initlog;
608
609
610
611
	print LOG "* Preparing to make the following packages:\n";
	print LOG "\t", join(", ", @stack), "\n\n";

	foreach my $pkg (@stack) {
612
613
        my $tlpkg  = $tlpdb->get_package($pkg);
		if ( ! defined $tlpkg ) {
614
615
616
617
618
619
			print RED "Warning: $pkg does not exist\n";
			print LOG "Warning: $pkg does not exist\n";
			next;
		};
		mkpath( "$ROOT/builds/$pkg", { mode => 0755 });
		open PKGBUILD, "> $ROOT/builds/$pkg/PKGBUILD";
620
        my $newver = $tlpkg->revision;
621
		my $col = $collectionsbypkg{$pkg};
622
		my $pkgmd5 = $tlpkg->containermd5;
firmicus's avatar
firmicus committed
623
624
625
		#TODO download it first and compare the md5sum
		#     if it differs ask the user if the PKGBUILD 
		#     should be adjusted
626
		print PKGBUILD <<"EOF"
firmicus's avatar
firmicus committed
627
628
629
630
631
632
# Generated by tllocalmgr
pkgname=texlive-local-$pkg
pkgver=$newver
pkgrel=1
pkgdesc="TeX Live - local installation of CTAN package $pkg"
license=('GPL')
633
arch=('any')
firmicus's avatar
firmicus committed
634
depends=('texlive-core')
635
conflicts=('texlive-$col>=$YEAR.$newver')
firmicus's avatar
firmicus committed
636
url='http://tug.org/texlive/'
637
source=('$TLARCHIVE/$pkg.tar.xz')
firmicus's avatar
firmicus committed
638
639
md5sums=($pkgmd5)

640
package() {
641
642
   cd \$srcdir
   rm -rf tlpkg
Rémy Oudompheng's avatar
Rémy Oudompheng committed
643
   install -m775 -d \$pkgdir/usr/local/share/texmf
644
   wanteddirs=\$(for d in *; do test -d \$d && [[ \$d != texmf* ]] && echo \$d; done) || true
645
   for dir in \$wanteddirs; do
Rémy Oudompheng's avatar
Rémy Oudompheng committed
646
647
     find \$dir -type d -exec install -d -m775 \$pkgdir/usr/local/share/texmf/'{}' \\;
     find \$dir -type f -exec install -m664 '{}' \$pkgdir/usr/local/share/texmf/'{}' \\;
648
649
650
651
   done
   for dir in texmf-dist texmf-doc; do
      if [[ -d \$dir ]]; then
        cd \$dir
Rémy Oudompheng's avatar
Rémy Oudompheng committed
652
653
        find . -type d -exec install -d -m775 \$pkgdir/usr/local/share/texmf/'{}' \\;
        find . -type f -exec install -m664 '{}' \$pkgdir/usr/local/share/texmf/'{}' \\;
654
655
656
657
658
659
        cd ..
      fi
   done
   if [[ -d \$pkgdir/usr/local/share/texmf/scripts ]]; then
     find \$pkgdir/usr/local/share/texmf/scripts -type f -exec chmod a+x '{}' \\;
   fi
firmicus's avatar
firmicus committed
660
661
}
EOF
662
663
664
665
666
667
668
669
	;
		close PKGBUILD;
		print LOG "* Created $ROOT/builds/$pkg/PKGBUILD\n";
	#	open INSTALL, "> $ROOT/builds/$pkg/texlive.install";
	#	print INSTALL $TLINSTALL;
	#	close INSTALL;
		print LOG "* Running makepkg -ci on $ROOT/builds/$pkg/PKGBUILD\n";
		chdir "$ROOT/builds/$pkg";
670
		system( "test -f $pkg.tar.xz && rm $pkg.tar.xz" );
firmicus's avatar
firmicus committed
671
		# TODO : catch error if makepkg fails
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
		system( "makepkg -ci" );
	}

	print "\n>>> Finished. Don't forget to run 'texhash' to update your file database.\n";
	#system ( "sed -i '/^$pkg /d' $INSTALLEDPKGS/texlive-local.pkgs" );
	#system ( "echo $pkg $newver >> $INSTALLEDPKGS/texlive-local.pkgs" ); 

	my @execlines;
	foreach my $pkg (@stack) {
		push @execlines, $tlpdb->get_package($pkg)->executes if $tlpdb->get_package($pkg)->executes;
	}

	if (@execlines) {
		print   ">>> Also make sure to update /usr/share/texmf-config/web2c/updmap.cfg in\n";
		print   "    case you have installed new fonts or fonts with a different map file.\n";
		print   "    Look at the log file for details.\n";
		print LOG "* Lines that can be added to /usr/share/texmf-config/web2c/updmap.cfg if needed:\n";
		foreach my $line (@execlines) {
			print LOG "\t", $line, "\n"
		};
		print LOG "(Make sure to run updmap-sys to activate them).\n";
	close LOG;
	}
firmicus's avatar
firmicus committed
695
696
}

697
698
699
sub makepkgdoc {
	my @stack = @_;
	
firmicus's avatar
firmicus committed
700
	initlog() unless $initlog;
701
702
703
704
705
706
707
708
709
710
711
712
	print LOG "* Preparing to make the following packages:\n";
	print LOG "\t", join(", ", @stack), "\n\n";

	foreach my $pkg (@stack) {
		if ( ! defined $tlpdb->package_revision($pkg) ) {
			print RED "Warning: $pkg does not exist\n";
			print LOG "Warning: $pkg does not exist\n";
			next;
		};
		mkpath( "$ROOT/builds/$pkg-doc", { mode => 0755 });
		open PKGBUILD, "> $ROOT/builds/$pkg-doc/PKGBUILD";
		my $newver = $tlpdb->package_revision($pkg);
firmicus's avatar
firmicus committed
713
714
715
716
		#TODO check if md5sums are defined in the database
		#TODO download it first and compare the md5sums
		#     if they differ ask the user if the PKGBUILD 
		#     should be adjusted
firmicus's avatar
firmicus committed
717
718
		my $docpkgmd5 = $tlpdb->get_package($pkg)->doccontainermd5;
		my $srcpkgmd5 = $tlpdb->get_package($pkg)->srccontainermd5;
719
720
721
722
723
		print PKGBUILD <<"EOF"
# Generated by tllocalmgr
pkgname=texlive-local-$pkg-doc
pkgver=$newver
pkgrel=1
firmicus's avatar
firmicus committed
724
pkgdesc="TeX Live - local installation of CTAN package $pkg (documentation and sources)"
725
license=('GPL')
726
arch=('any')
727
728
depends=('texlive-core')
url='http://tug.org/texlive/'
firmicus's avatar
firmicus committed
729
730
731
732
EOF
		;
		if ($srcpkgmd5) { 
			print PKGBUILD <<"EOF"
733
source=('$TLARCHIVE/$pkg.doc.tar.xz' '$TLARCHIVE/$pkg.source.tar.xz')
firmicus's avatar
firmicus committed
734
md5sums=('$docpkgmd5' '$srcpkgmd5')
firmicus's avatar
firmicus committed
735
736
737
EOF
		} else {
		print PKGBUILD <<"EOF"
738
source=('$TLARCHIVE/$pkg.doc.tar.xz')
firmicus's avatar
firmicus committed
739
740
md5sums=('$docpkgmd5')
EOF
741
742
        };
		print PKGBUILD <<"EOF"
743
package() {
744
745
   cd \$srcdir
   rm -rf tlpkg
Rémy Oudompheng's avatar
Rémy Oudompheng committed
746
   install -m775 -d \$pkgdir/usr/local/share/texmf
747
   wanteddirs=\$(for d in *; do test -d \$d && [[ \$d != texmf* ]] && echo \$d; done) || true
748
   for dir in \$wanteddirs; do
Rémy Oudompheng's avatar
Rémy Oudompheng committed
749
750
     find \$dir -type d -exec install -d -m775 \$pkgdir/usr/local/share/texmf/'{}' \\;
     find \$dir -type f -exec install -m664 '{}' \$pkgdir/usr/local/share/texmf/'{}' \\;
751
752
753
754
   done
   for dir in texmf-dist texmf-doc; do
      if [[ -d \$dir ]]; then
        cd \$dir
Rémy Oudompheng's avatar
Rémy Oudompheng committed
755
756
        find . -type d -exec install -d -m775 \$pkgdir/usr/local/share/texmf/'{}' \\;
        find . -type f -exec install -m664 '{}' \$pkgdir/usr/local/share/texmf/'{}' \\;
757
758
759
760
761
762
        cd ..
      fi
   done
   if [[ -d \$pkgdir/usr/local/share/texmf/scripts ]]; then
     find \$pkgdir/usr/local/share/texmf/scripts -type f -exec chmod a+x '{}' \\;
   fi
firmicus's avatar
firmicus committed
763
}
764
765
766
767
768
769
770
771
772
EOF
	;
		close PKGBUILD;
		print LOG "* Created $ROOT/builds/$pkg-doc/PKGBUILD\n";
	#	open INSTALL, "> $ROOT/builds/$pkg/texlive.install";
	#	print INSTALL $TLINSTALL;
	#	close INSTALL;
		print LOG "* Running makepkg -ci on $ROOT/builds/$pkg-doc/PKGBUILD\n";
		chdir "$ROOT/builds/$pkg-doc";
773
		system( "test -f $pkg-*.tar.xz && rm $pkg-*.tar.xz" );
firmicus's avatar
firmicus committed
774
		# TODO : catch error if makepkg fails
775
		system( "makepkg -ci" );
firmicus's avatar
firmicus committed
776
		}
777
	
firmicus's avatar
firmicus committed
778

779
780
781
	print "\n>>> Finished. Don't forget to run 'texhash' to update your file database.\n";
	#system ( "sed -i '/^$pkg /d' $INSTALLEDPKGS/texlive-local-doc.pkgs" );
	#system ( "echo $pkg $newver >> $INSTALLEDPKGS/texlive-local-doc.pkgs" ); 
firmicus's avatar
firmicus committed
782
783
}

784
sub complete_pkgname {
firmicus's avatar
firmicus committed
785

786
787
	my $self = shift;
	my $cmpl = shift;
firmicus's avatar
firmicus committed
788

789
790
	my $pkg = $cmpl->{args}->[0];
	return [] unless $pkg;
firmicus's avatar
firmicus committed
791

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
	#$pkg =~ s/^(["'])(.*)\1$/$2/;	# trim surrounding quotes
	my @cset = grep { m/^$pkg.+/i } @pkglist;

	if (@cset) {
		return [ @cset ];
	} else {
		if ($cmpl->{twice}) {
			$self->completemsg("\nNo pkg found for $pkg\n");
		}
	}
	return undef;
}

sub usage {
    qq[ 
Usage: tllocalmgr  
       tllocalmgr [options] [command] [args]

       Running tllocalmgr alone starts the TeXLive local manager shell 
       for Arch Linux. This shell is capable of command-line completion!
       There you can look at the available updates with the command 'status' 
       and you can install individual CTAN packages using 'install' or 'update'
       under \$TEXMFLOCAL. This is done by creating a package texlive-local-<pkg>
       and installing it with pacman. Note that this won’t interfere with your 
       standard texlive installation, but files under \$TEXMFLOCAL will take
       precedence.  
       
       Here are the commands available in the shell:

Commands:       
              status   --   Current status of TeXLive installation
           shortinfo * --   Print a one-liner description of CTAN packages
                info * --   Print info on CTAN packages
              update * --   Locally update CTAN packages
             install * --   Locally install new CTAN packages
          installdoc * --   Locally install documentation of CTAN packages
          installsrc * --   Locally install sources of CTAN packages
           listfiles * --   List all files in CTAN packages
              search * --   Search info on CTAN packages
         searchfiles * --   Search for files in CTAN packages
             texhash   --   Refresh the TeX file database
               clean   --   Clean local build tree
                help   --   Print helpful information
                quit   --   Quit tllocalmgr

        The commands followed by * take one of more package names as arguments.
        Note that these can be completed automatically by pressing TAB.

        You can also run tllocalmgr as a standard command-line program, with
        one of the above commands as argument, then the corresponding task will
        be performed and the program will exit (except when the command is 'status').

        tllocalmgr accepts the following options:

Options:     --help          Shows this help
firmicus's avatar
firmicus committed
847
             --version       Show the version number
848
849
850
851
852
853
854
             --forceupdate   Force updating the TeXLive database
             --skipupdate    Skip updating the TeXLive database
             --localsearch   Search only installed packages
             --location      #TODO?
             --mirror        CTAN mirror to use (default is mirror.ctan.org)
             --nocolor       #TODO
]
firmicus's avatar
firmicus committed
855
}
856

firmicus's avatar
firmicus committed
857
858
859
860
861
sub version {
    qq[ 
tllocalmgr:  version $VERSION
	]
}
firmicus's avatar
firmicus committed
862

Rémy Oudompheng's avatar
Rémy Oudompheng committed
863
# vim: set ts=4 sw=4 noet: