#!/usr/bin/perl -w # PASSarola cataLOGUE v1.0 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; # not running under some shell use utf8::all; use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Terse=1; 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/"; my $dir; my @MANIF ; ## list of p-problems or directories of p-problems if(@ARGV){ @MANIF = @ARGV } else { @MANIF = ($root, "/home/jj/svn/gexer/Problemas");} my $catalogue = $opt{cata} // "$ENV{HOME}/catalogue"; ##FIXME #e tambem... /home/jj/svn/main/mex-pedia/UM-epedia ##FIXME my $old_cat= {}; if (-f $catalogue) {$old_cat = do( $catalogue)} else {$old_cat = {}; } if($opt{meta}){ ## query p-problems' path and properties 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("Warning: Can't find '$dir'.\n"); next; } } $cata{__next}=$next; open(F,">",$catalogue) or die("Cant create catalogue '$catalogue'\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.tex") {$meta{tex}="$dir/$f.tex";} if(-f "$dir/OUT/$f.tex") {$meta{tex}="$dir/OUT/$f.tex";} if(-f "$dir/$f.pdf") {$meta{pdf}="$dir/$f.pdf";} if(-f "$dir/OUT/$f.pdf") {$meta{pdf}="$dir/OUT/$f.pdf";} } return %meta ; } sub dump_exer{ my ($root,$cata,@ex)=@_; ## information about a set of p-id 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__ =pod =encoding utf8 =head1 NAME pass-logue - Passarola Exercise Catalogue Manager =head1 SYNOPSIS pass-logue [I