#!/usr/bin/perl
## Copyright (C) 2013 Carnë Draug <carandraug+dev@gmail.com>
##
## This program 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 of the License, or
## (at your option) any later version.
##
## This program 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 this program; if not, see <http://www.gnu.org/licenses/>.

use 5.010;                      # Use Perl 5.10
use strict;                     # Enforce some good programming rules
use warnings;                   # Replacement for the -w flag, but lexically scoped
use LWP::Simple;                # simple procedural interface to WWW library

my %octave_core;
my @octave_fcn = split ("\n", qx/octave --norc -q --eval "list = __list_functions__ (); for i = 1:numel(list), disp (list{i}); endfor" | less/);
@octave_core{@octave_fcn} = "";

my @toolboxes = qw(control comm finance images signal stats optim);
foreach my $box_name (@toolboxes) {
  my $html = LWP::Simple::get("http://www.mathworks.co.uk/help/$box_name/functionlist-alpha.html");

  ## There's also classes from the new type of Matlab packages such as
  ## images.geotrans which can be captured by adding a \. to the [ ] below
  my @matlab_fcn = $html =~ m{>([A-Za-z0-9_]+)</a></td>\n<td class="description">}g;

  ## filter functions that we already have implemented in Octave Core
#  grep { ! ( $_ ~~ @array2 ) } @matlab_fcn
#  @matlab_fcn = grep (grep ($_ eq , @octave_fcn), @matlab_fcn);
  @matlab_fcn = grep (! exists ($octave_core{$_}), @matlab_fcn);

  my $code;
  my $width = 0;
  foreach my $name (@matlab_fcn) {
    my $length = length ($name) +4; # +4 for the quotes, comma and space
    if ($width + $length > 67) {
      $code .= "...\n          ";
      $width = 0;
    }
    $width += $length;
    $code .= qq/"$name", /;
  }
  substr ($code, length ($code) - 2) = ""; # remove last comma and space
  print "    case {$code}\n";
}
