#!/usr/bin/perl 
# ememe
# Enhanced MEME generator
# By LionsPhil, based on AndyLandy's "meme" script.
#
# For super meme fun, try using ememe with automeme or autowikimeme!
# v  date
# 10 11022003 Recoded to use an adjective/verb method, and now supports
#             recursion with connector selection. It's twisted.
# 11 11022003 Fixed extra recursion-induced line breaks with -r flag.
#             Put the @common words back in from "meme".
#             Allowed for special "Null ? Exception".
#             Stopped duplicate adjectives.
#             Tweaked adjective count and reduced recursion probablility.
#             Removed '-w' from perl line.
#             Exits cleanly.
# 12 11022003 Now supports memewrappers. 'AllYourBase' and 'Insecure' converted.
#             ('Insecure' has been left as an adjective)
#             Tweaked memewrapper probability.
#    12022003 Added 'StoleMy'
# 13 16022003 Added a "DoYouHave'X'InYourHouse'

# AND NOW
#      some blatent code rape from EvilRich's "cheese" script to get parms
while ($ARGV[0] =~ /^-/) {
	my ($opts) = @ARGV;
	for $op ("r") {
		if ($opts =~ /^[^p]*$op[^p]*/ ) {
			$env{$op} = true;
		}
	}
	shift;
}

# before we get carried away generating tasty memeage, shall we wrap one?
# don't wrap recursed memes - the results or often garbled (and hard to debug!)
#if(((rand 20) > 18) && (!($env{"r"})))
# ah, heck, recurse away!
if((rand 20) > 16)
{
	# yay! Let us recurse, and be happy!
	# we must ready the wrappers, in a positively EVIL format (bwahaha)
	@wrapperstarts = qw(Null      AllYour     HelpDeskSays _       DoYouHave);
	@wrappercores  = qw(_         AreBelongTo _            StoleMy InYour   );
	@wrapperends   = qw(Exception _           IsInsecure   _       _        );

	#$meme = "Null" . `ememe -r` . "Exception"; # old v1.1 code

	# there's got to be a nicer way of doing this, but hey (can you say
	#                                                       F U D G E ?)
	$selectedwrapper = (rand scalar(@wrapperstarts));
	$meme = "";
	if(!($wrapperstarts[$selectedwrapper] eq "_"))
	{$meme = $wrapperstarts[$selectedwrapper];}
	$meme = $meme . `ememe -r`;
	if(!($wrappercores[$selectedwrapper] eq "_"))
	{
		$meme = $meme . $wrappercores[$selectedwrapper] . `ememe -r`;
	}
	if(!($wrapperends[$selectedwrapper] eq "_"))
	{
		$meme = $meme . $wrapperends[$selectedwrapper];
	}

	$end = "\n"; if($env{"r"}){$end="";}
	print $meme . $end;
	# now get out of here before you cause trouble
	exit (0);
}

# prepare the mighty arrays of discombobulation
@adjectives = qw(Bearded CaseSensitive Crufty DirtyDirty Evil Filthy Indeterminate Insecure Local Organic Pickled Shonky Hairy Lumpy BigBlue);
@adjectives = (@adjectives, qw(BaconOr Cheesy Tustin-o-));
@nouns = qw(Allann Allsopp BarberShopQuartet BASIC Beard BlitterChip Blumenkraft CATS Chicken ColinMochrie Cow CSLib Donkey DualBoot EyeBrows Foo GarbageCollection Grue Jg102 Kirsty Meme Pets Prizes Shoes Shonk Shop Soy Spong Spork Spoon Stein Telnet Trousers WiKi Yatta);
@nouns = (@nouns, qw(Bacon Cheese Llama Panda Stairs Telephone Tustin));
@pluralconnectors = qw(With Eating Fingering Programming);
@singularconnectors = qw(WithA EatingA FingeringA ProgrammingA);

# prepare storage for our precioussss meme
$meme = "";
# how many adjectives would you like today?
$adjectives = (rand 4);
for($adjectivesadded = 0; $adjectivesadded < $adjectives; $adjectivesadded++)
{
	# new ugly way to check for previously used adjectives
	$newtoken = $adjectives[rand(@adjectives)];
	$grepped = `echo "$meme" | grep $newtoken`;
	if(!chomp($grepped)){$meme = $meme . $newtoken;}
}
# and whack a noun on the end. Nouns are good.
$meme = $meme . $nouns[rand(@nouns)];

# shall we recurse? 'tis so much FUN!
if((rand 15) > 13)
{
	# debug line only!
	#print "meme2 feels like recursing today!\n";
	# old simplistic code
	#$meme = $meme . $connectors[rand(@connectors)] . `meme`;

	#Very simplistic plural connection
	$lastchar = chop($meme); $meme = $meme . $lastchar;
	$connector = "";
	if($lastchar eq "s")
	{$connector = $pluralconnectors[rand(@pluralconnectors)];} else
	{$connector = $singularconnectors[rand(@singularconnectors)];}
	$meme = $meme . $connector . `ememe -r`; #MAKE SURE that this matches
	                                         #the script's name!!!
}

# dump meme, with carriage return if not a level of recursion
$end = "\n"; if($env{"r"}){$end="";}
print $meme . $end;
exit(0);
