#!/usr/bin/perl -w

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 itemname.cc|" or die "Can't read itemname.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;
$items{jewellery_name($1, $2)} = 1
    while /((?:RING|AMU)_[A-Z_]+): *return "([^"]+)";/g;

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;
$items{"rod of $_"} = 1 for /ROD_[A-Z_]+: *return "([^"]+)";/g;
$items{$_} = 1 for /FOOD_[A-Z_]+: buff << "([^"]+)";/g;

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

s/" "//g;
$items{$_} = 1 for /\{ *ARM_[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 =~ /"([^"]+)"/mg;

$items{$_} = 1 for (split /\n/, <<END);
chunk of flesh
mutagenic chunk of flesh
inedible chunk of flesh
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
Akashic Record
lightning rod
iron rod
END

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

delete $items{$_} for (split /\n/, <<END);
boots
gloves
rod of lightning
rod of iron
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";
}
