reindex.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/perl -w
  2. # Copyright 2007 Nick Mathewson. See LICENSE for licensing information.
  3. use strict;
  4. my $propdir = ".";
  5. local *DIR;
  6. local *F;
  7. opendir(DIR, $propdir) || die "Can't open $propdir";
  8. my @files = sort grep { /^\d\d\d-.*[^\~]$/ } readdir(DIR);
  9. closedir DIR;
  10. my %title = ();
  11. my %status = ();
  12. my @KNOWN_STATUSES = qw{
  13. DRAFT OPEN NEEDS-REVISION NEEDS-RESEARCH ACCEPTED META FINISHED CLOSED
  14. SUPERSEDED DEAD};
  15. for my $f (@files) {
  16. my $num = substr($f, 0, 3);
  17. my $status = undef;
  18. my $title = undef;
  19. my $alleged_fname = undef;
  20. if ($f !~ /\.txt/) { print "$f doesn't end with .txt\n"; }
  21. open(F, "$f");
  22. while (<F>) {
  23. last if (/^\s*$/);
  24. if (/^Status: (.*)/) {
  25. $status = uc $1;
  26. chomp $status;
  27. }
  28. if (/^Filename: (.*)/) {
  29. $alleged_fname = $1;
  30. chomp $alleged_fname;
  31. }
  32. if (/^Title: (.*)/) {
  33. $title = $1;
  34. $title =~ s/\.$//;
  35. chomp $title;
  36. }
  37. }
  38. close F;
  39. die "I've never heard of status $status in proposal $num"
  40. unless (grep(/$status/, @KNOWN_STATUSES) == 1);
  41. die "Proposal $num has a bad status line" if (!defined $status);
  42. die "Proposal $num has a bad title line" if (!defined $title);
  43. die "Proposal $num has no Filename line" unless (defined $alleged_fname);
  44. die "Proposal $num says its fname is $alleged_fname, but it's really $f"
  45. if ($alleged_fname ne $f);
  46. $title{$num} = $title;
  47. $status{$num} = $status;
  48. }
  49. local *OUT;
  50. open(OUT, ">000-index.txt.tmp");
  51. open(F, "000-index.txt") or die "Can't open index file.";
  52. while (<F>) {
  53. print OUT;
  54. last if (/^={3,}/);
  55. }
  56. close(F);
  57. print OUT "Proposals by number:\n\n";
  58. for my $num (sort keys %title) {
  59. print OUT "$num $title{$num} [$status{$num}]\n";
  60. }
  61. print OUT "\n\nProposals by status:\n\n";
  62. for my $status (@KNOWN_STATUSES) {
  63. print OUT " $status:\n";
  64. for my $num (sort keys %status) {
  65. next unless ($status{$num} eq $status);
  66. print OUT " $num $title{$num}\n";
  67. }
  68. }
  69. rename('000-index.txt.tmp', '000-index.txt');