#!/usr/bin/perl eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; # not running under some shell use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Terse=1; use strict; use utf8::all; use Getopt::Long qw(:config no_auto_abbrev ); use Pod::Usage; my %opt=(id=>""); GetOptions( \%opt, "help|h|?" , "man", "meta" , "src=s@" , "key=s@" , "id=s" , "root=s" , "cata=s" , ## "<>" => sub {push @inputnames, $_[0]}, ) or die "Specify the --help (or -?) option for usage information.\n"; pod2usage(2) if $opt{help}; pod2usage(-exitstatus => 0, -verbose => 2) if $opt{man}; my $root= $opt{root} || "/home/natura/mex/public/users/"; my $dir; my @MANIF ; if(@ARGV){ @MANIF = @ARGV } else { @MANIF = ($root, "/home/jj/svn/gexer/Problemas");} my $catalogue_file = $opt{cata} // "$ENV{HOME}/.mex_catalogue"; #e tambem... /home/jj/svn/main/mex-pedia/UM-epedia my $old_cat= {}; if (-f $catalogue_file) {$old_cat = do( $catalogue_file)} else {$old_cat = {}; } if($opt{meta}){ print Dumper(dump_exer($root,$old_cat,@MANIF)); exit 0; } my %cata=(); my %inv =(); my $next = $old_cat->{__next} || 1; for my $i (keys %$old_cat){ next if($i =~ /^__/); my $j=0; for my $d (@{$old_cat->{$i}}){ my $p1 = $d->{dir}; $inv{"$p1=$i"} = $old_cat->{$i}[$j]{code} // ("__".$next++); $cata{$inv{"$p1=$i"}} = "$p1=$i"; $j++; } } for $dir(@MANIF){ if(-d $dir){ my @lista = `find $dir`; for(@lista){ chomp; if(m!(.*)/(.+\.(txt|mex)$)!) { my ($path,$file)=($1,$2); my $code = $inv{"$path=$file"} || ("__".$next++) ; push(@{$cata{$file}},{ dir => $path, code => $code, (get_meta($path,$file)) }); $cata{$code} = "$path=$file"; } } } else { warn("cant find $dir\n")} } $cata{__next}=$next; open(F,">",$catalogue_file) or die("Cant create catalogue '$catalogue_file'\n"); print F Dumper(\%cata); close F; sub get_meta{ my($dir,$f)=@_; my $txt = `cat "$dir/$f"`; my $base = $f; my %meta=(); if($dir =~ m!$root([^/]+)!){$meta{user}=$1} for($txt){ s/[ \t\r]*\n/\n/g; if(/(?:^|\n)#title:?\s*(.+)/i ){$meta{title}=$1} else {$meta{title}=$f} if(/(?:^|\n)#author:?\s*(.+)/i){$meta{author}=$1} if(/(?:^|\n)#keys?:?\s*(.+)/i ){$meta{key}=[split(/\s*[,;]\s*/,$1)]} if(-f "$dir/$f.pdf") {$meta{pdf}="$dir/$f.pdf";} } return %meta ; } sub dump_exer{ my ($root,$cata,@ex)=@_; my @r=(); my ($dir,$file); for (@ex){ s/^\~/$root/; my $m={}; if(-f $_){ if( m!(.*)/(.*)!){ $dir=$1; $file=$2 } else { $dir="."; $file=$_ } $m = { dir=>$dir,file=>$file,local=>1,get_meta($dir,$file)}; } elsif(defined $cata->{$_} and m{^__}){ ($dir,$file)= split("=",$cata->{$_}); for(@{$cata->{$file}}){ if($_->{dir} eq $dir) { $m = {%$_,file=>$file}; } } } elsif(defined $cata->{$_}){ $m = { %{$cata->{$_}[0]},file=>$_}; } elsif( m!(.*):(\d)! and defined $cata->{$1}){ $m = { %{$cata->{$1}[$2] || {} },file=>$1}; } elsif( m!(.*)/(.*)! and defined $cata->{$2}){ my ($x1,$x2)=($1,$2); for( @{ $cata->{$x2}} ){ if($_->{dir} =~ m{$x1}){ $m = {%$_,file=>$x2}; } } } else { } push(@r,$m); } return \@r; } __END__ =encoding utf8 =head1 NAME =encoding utf8 mex_catalogue - creates a catalogue of MeX exercises =head1 SYNOPSIS mex-catalogue [options] dir* mex-catalogue -meta exercise_id* options: -cata catalogue-file (def: ~/.mex_catalogue) -root directory (def: /home/natura/mex/public/users) =head1 DESCRIPTION exercise_id can be: . path (to an existing mex-file) . filename . filename:int . uniq_id (__int eg: __23) all mex-file in the catalogue have a uniq_id . pattern/filename (pattern =~ m/path/) . ~username/filename ($root/username/filename ) =head2 Options =head1 AUTHOR J.Joao Almeida, jj@di.uminho.pt =head1 SEE ALSO perl(1). =cut