#Script by cythrawll and honker from irc.7sinz.net
#Should be compatible for Linux systems and Xchat 2.0.8 and above
Xchat::register("cyth's epenis script","1.4","","");
Xchat::hook_command("epenis","epenis");

use Math::Complex;
my $home;
if( defined($ENV{HOME}) ) {
	$home = $ENV{HOME};
} else { 
	die "\$HOME is not set.\n";
}

my $penisfile = $home . "/.epenis.txt";

sub epenis {
	my($os,$up,$in,$big,$cm,$bigcm);
	$os = get_os();
	$up = get_up();
	$big = get_big();
	if (!defined($big) || $up > $big){
		set_big($up);
		$big = $up;
	}
	$ever = sprintf("%.2f",sqrt(0.0005 * $big));
	$in = sprintf("%.2f",sqrt(0.0005 * $up));
	$cm = sprintf("%.2f",$in * 2.540);
	$bigcm = sprintf("%.2f",$ever * 2.540);
	Xchat::command("say Based on the uptime of my \002$os\002 box, my e-Penis is \002$in\002 inches ($cm cm) erect! Biggest erection ever was \002$ever\02 inches ($bigcm cm)!");
	return Xchat::EAT_ALL;
} 

sub get_os {
  my ($version);
  open(VERSION, "uname -sr|");
  $version = <VERSION>;
  chomp $version;
  return $version;
}

sub get_up {
  open(UPTIME, "</proc/uptime") or die Xchat::print("ERROR: could not find \'uptime'\ in your path");
  @foo = <UPTIME>;
  my @uptime = split /\s+/, $foo[0]; 
  return $uptime[0];
}

sub get_big {

	if ( -e $penisfile ) {
		open(IN, "<$penisfile") or die Xchat::print("cannot open file $penisfile: $!");
		my $big = <IN>;
		chomp $big;
		close IN;
		return $big;
	}

	return 0;
}

sub set_big {
	my($big) = @_;
	open(OUT, ">$penisfile") or die Xchat::print("cannot open file $penisfile: $!");
	print OUT "$big\n";
	close OUT;
}

