#!perl -w

# Copyright 2011 Kevin Ryde

# This file is part of Math-Image.
#
# Math-Image is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-Image is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-Image.  If not, see <http://www.gnu.org/licenses/>.

use 5.004;
use strict;
use warnings;
use Data::Dumper;
use Module::Util;

use vars '$VERSION';
$VERSION = 44;

my @info_arrayref;
my @classes = Module::Util::find_in_namespace('App::MathImage::Values');
@classes = sort @classes;
foreach my $class (@classes) {
  next if $class =~ /^App::MathImage::Values::.*::/; # not sub-parts

  my $filename = Module::Util::find_installed($class) or die;
  open my $in, '<', $filename or die;
  while (<$in>) {
    chomp;
    if (/^# OEIS: /) {
      /^# OEIS: A0*([0-9]+)\s*(.*?)(#.*)?$/
        or die "Oops, bad OEIS line: $_";
      my $num = $1;
      my $parameters = $2;
      my $comment = $3;
      my @parameters = split /[,= \t]+/, $parameters;
      if (@parameters & 1) {
        die "Oops, odd number of  OEIS params: $_";
      }
      defined $class
        or die 'Oops, no "package" seen';
      push @info_arrayref,
        {
         num => $num,
         class => $class,
         parameters_hashref => {@parameters},
        };
    }
  }
  close $in or die;
}

my $dump = Data::Dumper->new([\@info_arrayref])->Sortkeys(1)->Terse(1)->Indent(1)->Dump;
# $dump =~ s/^{\n//;
# $dump =~ s/}.*\n//;

open my $out, '>', 'lib/App/MathImage/Values/OEIS/Catalogue/Plugin/Builtin.pm' or die;
print $out <<"HERE";
# Copyright 2011 Kevin Ryde

# Generated by make-oeis-catalogue.pl -- DO NOT EDIT

# This file is part of Math-Image.
#
# Math-Image is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-Image is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-Image.  If not, see <http://www.gnu.org/licenses/>.

package App::MathImage::Values::OEIS::Catalogue::Plugin::Builtin;
use strict;
use warnings;

use vars '\$VERSION', '\@ISA';
\$VERSION = $VERSION;
\@ISA = ('App::MathImage::Values::OEIS::Catalogue::Base');

use constant info_arrayref =>
HERE

print $out "$dump;\n1;\n__END__\n";
exit 0;
