#!/usr/bin/env perl

use warnings;

my $db = grep /^-d$/, @ARGV;
my %jewellery_prefixes;

sub jewellery_name($$)
{
    my ($enum, $name) = @_;
    my $prefix = $jewellery_prefixes{$enum} || "";
    my $type = $enum =~ /^AMU_/ ? "amulet" : "ring";
    return "$type of $prefix$name";
}

open IN, "util/cpp_version item-name.cc|" or die "Can't read item-name.cc\n";
{ undef local $/; $_ = <IN>; }
close IN;

# Remove this from the input so the main jewellery pattern doesn't match.
s/^static [^\n]*_jewellery_effect_prefix\([^\n]*\).(.*?)^}//ms;
my $prefixes = $1;
$jewellery_prefixes{$1} = $2
    while $prefixes =~ /((?:RING|AMU)_[A-Z_]+): *return "([^"]+)";/g;

$items{"wand of $_"} = 1 for /WAND_[A-Z_]+: *return "([^"]+)";/g;
$items{"potion of $_"} = 1 for /POT_[A-Z_]+: *return "([^"]+)";/g;
$items{"scroll of $_"} = 1 for /SCR_[A-Z_]+: *return "([^"]+)";/g;
my %seen_enums;
while (/((?:RING|AMU)_[A-Z_]+): *return "([^"]+)";/g)
{
    next if $seen_enums{$1}++;
    $items{jewellery_name($1, $2)} = 1
}

unless ($db)
{
    $items{"$_ rune of Zot"} = 1 for /RUNE_[A-Z_]+: *return "([^"]+)";/g;
    $items{"$_ deck of cards"} = 1 for /DECK_RARITY_[A-Z_]+: *return "([^"]+)";/g;
}
$items{$_} = 1 for /MISC_[A-Z_]+: *return "([^"]+)";/g;
$items{"book of $_"} = 1 for /BOOK_[A-Z_]+: *return "([^"]+)";/g;
$items{"staff of $_"} = 1 for /STAFF_[A-Z_]+: *return "([^"]+)";/g;

open IN, "util/cpp_version item-prop.cc|" or die "Can't read item-prop.cc\n";
{ undef local $/; $_ = <IN>; }
close IN;

s/" "//g;
$items{$_} = 1 for /\{ *ARM_[A-Z_]+, *"([^"]+)"/mg;
$items{"$_ dragon scales"} = 1 for /^ *DRAGON_ARMOUR\([A-Z_]+, *"([^"]+)"/mg;
$items{$_} = 1 for /^ *\{ *WPN_[A-Z_]+, *"([^"]+)"/mg;
$items{$_} = 1 for /^ *\{ *MI_[A-Z_]+, *"([^"]+)"/mg;

open IN, "util/cpp_version decks.cc|" or die "Can't read decks.cc\n";
{ undef local $/; $_ = <IN>; }
close IN;
my $data = $_;
$data =~ s/.*all_decks =(.*?)};.*/$1/s or die "can't find all_decks\n";
$items{"deck of $_"} = 1 for $data =~ /^\s+"([^"]+)"/mg;

$items{$_} = 1 for (split /\n/, <<END);
corpse
eggplant
gold piece
manual
orb of Zot
pair of boots
pair of gloves
rune of Zot
Young Poisoner's Handbook
Grand Grimoire
Necronomicon
Fen Folio
Everburning Encyclopedia
There-And-Back Book
Great Wizards, Vol. II
Great Wizards, Vol. VII
Trismegistus Codex
the Unrestrained Analects
END

$items{"decaying skeleton"} = 1 if $db;

delete $items{$_} for (split /\n/, <<END);
boots
gloves
END

for (sort keys %items)
{
    next if /bugginess/i;
    # yay consistency, all other descs use proper capitalization
    tr/A-Z/a-z/ if $db && !/Geryon/;
    print "$_\n";
}
