#!/usr/bin/perl -w
# public domain

use strict;

my $VERSION = "v2";
my $PREFIX = "/usr/local";

my $one = 1; # Heh heh.

my %subrefs = ( '-h' => [\&help, 'print this help text'],
		'-v' => [\&ver, 'print version information'],
		'-1' => [\&one, 'print one cry and exit (default)'],
		'-m' => [\&mult, 'print multiple cries'],
		'-l' => [\&list, 'list substitutions (long)'] );

my @place = ("hearth",
	     "fortress",
	     "tome",
	     "dwelling",
	     "lands",
	     "mountain",
	     "hills",
	     "mines");

my @adject = ("brave",
	      "stalwart",
	      "honorable",
	      "noble",
	      "fierce",
	      "fearless",
	      "courageous",
	      "valiant",
	      "bold",
	      "heroic");

my @ability = ("wrath",
	       "power",
	       "vengeance",
	       "might",
	       "glory",
	       "ire",
	       "retribution",
	       "fire",
	       "anger",
	       "vengeance",
	       "fury",
	       "rage");

my @insult = ("cowardice",
	      "evil",
	      "fear",
	      "weakness",
	      "treachery",
	      "dishonor",
	      "ignorance",
	      "frailty",
	      "shame");

my @item = ("beard",
	    "forge",
	    "rune",
	    "name",
	    "blood",
	    "anvil",
	    "gold",
	    "gems",
	    "stone",
	    "rock",
	    "fire");

my @people = ("sons",
	      "blood",
	      "clansmen",
	      "beards",
	      "warriors",
	      "brothers",
	      "guardians",
	      "legions",
	      "brethren");

my @event = ("death",
	     "defeat",
	     "destruction",
	     "devastation",
	     "doom",
	     "end",
	     "fall",
	     "downfall",
	     "fate",
	     "last day",
	     "reckoning",
	     "last breath",
	     "demise");

my @deity = ("the All-Father",
	     "the Creator",
	     "the Dwarf Father",
	     "my forefathers",
	     "my elders",
	     "the clan",
	     "Moradin",
	     "the Soul-Forger",
	     "my ancestors",
	     "a 250 pound woman");

my @weapon = ("gauntlet",
	      "greataxe",
	      "hammer",
	      "Urgrosh",
	      "waraxe",
	      "steel",
	      "fist",
	      "warhammer",
	      "shield",
	      "axe",
	      "battleaxe");

my @time = ("is nigh",
	    "is now",
	    "is here",
	    "has come",
	    "is near");

my @word = ("zoom",
	    "gnarr",
	    "schwartz",
	    "mint",
	    "die",
	    "foo",
	    "rice");

my $param;
foreach $param (@ARGV) {
 if ($subrefs{$param}) {
  &{${$subrefs{$param}}[0]}();
 }
}

my @base = ();

if (open( BASE, "$PREFIX/etc/gnarrrc" )) {
  push @base, (<BASE>);
  close BASE;
}
if (open( USER, "$ENV{HOME}/.gnarrrc" )) {
  push @base, (<USER>);
  close USER;
}

chomp @base;

die("No data read from gnarrc files!\n") if ($#base == -1);

do {

 my $phrase = $base[int rand @base];

 $phrase =~ s/PLACE/$place[int rand @place]/g;
 $phrase =~ s/ADJECT/$adject[int rand @adject]/g;
 $phrase =~ s/ABILITY/$ability[int rand @ability]/g;
 $phrase =~ s/INSULT/$insult[int rand @insult]/g;
 $phrase =~ s/ITEM/$item[int rand @item]/g;
 $phrase =~ s/PEOPLE/$people[int rand @people]/g;
 $phrase =~ s/EVENT/$event[int rand @event]/g;
 $phrase =~ s/DEITY/$deity[int rand @deity]/g;
 $phrase =~ s/WEAPON/$weapon[int rand @weapon]/g;
 $phrase =~ s/TIME/$time[int rand @time]/g;
 $phrase =~ s/WORD/$word[int rand @word]/g;

 $phrase = ucfirst($phrase);

 print "$phrase\n";
 if ($one) { exit(); }
 print "Another? [Y/n] ";
 
} while ((my $y = <STDIN>) !~ /n/i);

exit();

sub help {
 print "Gnarr $VERSION. Usage:\n";
 my $key;
 foreach $key (keys %subrefs) {
  my $help = ${$subrefs{$key}}[1];
  print "  $key\t\t$help\n";
 }
 print "\n";
 exit();
}

sub ver {
 print "Gnarr $VERSION by Joe Wreschnig <piman\@sacredchao.net>\n";
 print "Based on the Dwarven Battle Cry generator in Dragon Magazine.\n\n";
 print "This program is in the public domain; feel free to modify and redistribute.\n";
 print "Please send any enhancements to piman\@sacredchao.net.\n\n";
 exit();
}

sub one  { $one = 1; }

sub mult { $one = 0; }

sub list {
  print "PLACE:\n"; print "  $_\n" foreach (@place);
  print "\nADJECT:\n"; print "  $_\n" foreach (@adject);
  print "\nABILITY:\n"; print "  $_\n" foreach (@ability);
  print "\nINSULT:\n"; print "  $_\n" foreach (@insult);
  print "\nITEM:\n"; print "  $_\n" foreach (@item);
  print "\nPEOPLE:\n"; print "  $_\n" foreach (@people);
  print "\nEVENT:\n"; print "  $_\n" foreach (@event);
  print "\nDEITY:\n"; print "  $_\n" foreach (@deity);
  print "\nWEAPON:\n"; print "  $_\n" foreach (@weapon);
  print "\nTIME:\n"; print "  $_\n" foreach (@time);
  print "\nWORD:\n"; print "  $_\n" foreach (@word);
  exit();
}
