#!/usr/bin/perl

use List::MoreUtils qw(uniq nsort_by);
use LaTeX::Encode qw(latex_encode);

# four files: 
# - glr: input file, unsorted list of glep numbers, may contain duplicates, 
#        generated by LaTeX
# - gld: database of gleps, format
#        * glep number
#        * glep title
#        * glep status
#        * index keywords separated by ";"
# - glg: output file, LaTeX fragment that can be sourced



# Fetch the title of a bug from Bugzilla
# URL example: https://wiki.gentoo.org/index.php?title=GLEP:31&action=raw
# Result contains
# {{GLEP
# |Number=31
# |Title=Character Sets for Portage Tree Items
# |Type=Standards Track
# |Status=Final
# |Author=Ciaran McCreesh <ciaranm@gentoo.org>
# }}


sub getglepdata {
  my $number=shift;

  open (my $web, '-|:encoding(UTF-8)', 
   'wget -O - https://wiki.gentoo.org/index.php?title=GLEP:'.$number.'\&action=raw');

  my @glep=<$web>;
  close $web;
  
  my $title, $status;
  
  foreach (@glep) {
    $line=$_; chomp $line; print "line is $line\n";
    if ($_=~ /^\|Title=/) {
      $title=$_;
      $title=~s/^\|Title=//;
      chomp $title;
    };
    if ($_=~ /^\|Status=/) {
      $status=$_;
      $status=~s/^\|Status=//;
      chomp $status;
    };
  };
  
  return ($title, $status);
}


# Main code start

# Read the list of gleps referenced in the LaTeX file.
open my $glr, '<', "decisions.glr";
my @glrlist = <$glr>;
close $glr;
chomp @glrlist;

my @glepnumbers=uniq nsort_by { $_ } @glrlist;

# Read the database
open my $gld, '<', "decisions.gld";

my $gleptitle={};
my $glepstatus={};
my $glepkeywords={};

while (1) {
  defined ( my $number=<$gld> ) or last;
  defined ( my $title=<$gld> ) or last;
  defined ( my $status=<$gld> ) or last;
  defined ( my $keywords=<$gld> ) or last;
  chomp $number; 
  chomp $title;
  chomp $status;
  chomp $keywords;
  $gleptitle{$number}=$title;
  $glepstatus{$number}=$status;
  $glepkeywords{$number}=$keywords;  
};

close $gld;


# Loop through the referenced glep numbers, check if we already have the title,
# and if not fetch and add it.
foreach(@glepnumbers) {
  my $cur=$_;
  print "GLEP number $cur\n";
  if ($gleptitle{$cur}) {
    print "  Title is \"$gleptitle{$cur}\"\n";
  } else {
    print "  Title not yet available, fetching all the data\n";
    ($gleptitle{$cur},$glepstatus{$cur})=getglepdata($cur);
    $glepkeywords{$cur}="";
  };
};

# Write out the bug title cache again.
my @gleptitlenew= map { $_ => $gleptitle{$_} } sort keys %gleptitle;

open my $gld, '>', "decisions.gld";
foreach(@glepnumbers) {
  my $cur=$_;
  print $gld "$cur\n";
  print $gld "$gleptitle{$cur}\n";
  print $gld "$glepstatus{$cur}\n";
  print $gld "$glepkeywords{$cur}\n";
};
close $gld;

# Write out the TeX input file
open my $glg, '>', "decisions.glg";
print $glg '\renewcommand{\gleptitle}[1]{%'."\n";

foreach(@glepnumbers) {
  my $cur=$_;
  my $title=latex_encode($gleptitle{$cur});

  my @keywords=split /;/, $glepkeywords{$cur};
  foreach(@keywords) {
    $title=$title.'\index{'.$_.'}';
  }
  print $glg '\ifthenelse{\equal{#1}{'.$cur.'}}{'.$title.'}{}%'."\n";
};

print $glg '}'."\n";
close $glg;
