#!/usr/bin/perl
use strict;
use warnings;
my $copyright = <<'EOF';
/* Copyright (C) 2007,2012,2013,2015 Olly Betts
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
EOF

use Tokeniseise;

my $srcdir = shift @ARGV;

my $hdr = Tokeniseise->new('languages/sbl-dispatch.h', 'Map string to language code', $copyright, 'XAPIAN_INCLUDED_SBL_DISPATCH_H', 'sbl_code');
$hdr->add('none', 'NONE');
my @langs;
foreach (sort @ARGV) {
    m{.*/(.*)\.sbl$} or die "Failed to parse snowball source: $_\n";
    my $lang = $1;
    push @langs, $lang;
    # Include the headers for all the snowball stemmers.
    $hdr->append("#include \"$lang.h\"");
    my $enum = uc $lang;
    $hdr->add($lang, $enum);
    open SBL, '<', "$srcdir/$_" or die "Open $srcdir/$_ failed: $!\n";
    my $l = <SBL>;
    close SBL;
    $l =~ s/^\W*Alias:\s*//i or die "No Alias: header in $srcdir/$_\n";
    $l =~ s/\s*$//;
    for (split /\s+/, $l) {
	$hdr->add($_, $enum);
    }
}

$hdr->append('');
$hdr->append("#define LANGSTRING \"" . join(" ", @langs) . "\"");

$hdr->write();
