#!/usr/bin/perl
my $copyright = <<'END';
// Copyright (C) 2015,2026 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, see
// <https://www.gnu.org/licenses/>.
END

use strict;
use warnings;

@ARGV == 1 or die "Syntax: $0 OUTFILE\n";

my $out = shift @ARGV;

open OUT, '>', "$out.tmp" or die $!;

print OUT "/* Generated by $0 */\n\n";
print OUT $copyright;
print OUT "\n{\n";
for my $ch (0 .. 255) {
    $_ = chr($ch);
    if (/[ -~]/) {
        print OUT "    /* '$_'  */ ";
    } else {
        printf OUT "    /* 0x%02x */ ", $ch;
    }
    my @a;
    if (/[0-9a-fA-F]/) {
        push @a, sprintf "0x%02x", hex($_);
    }
    if (/[A-Za-z]/) {
        push @a, 'Xapian::Internal::IS_ALPHA';
    }
    if (/[A-Z]/) {
        push @a, 'Xapian::Internal::IS_UPPER';
    }
    if (/[0-9]/) {
        push @a, 'Xapian::Internal::IS_DIGIT';
    }
    if (/[\t\n\f\r ]/) {
        push @a, 'Xapian::Internal::IS_SPACE';
    }
    @a > 0 or @a = (0);
    print OUT join ' | ', @a;
    print OUT ",\n";
}
print OUT "},\n";

close OUT or die $!;

rename "$out.tmp", $out or die $!;
