#!/Perl

use File::stat;

print "Hello, World...\n";
$dbgem = 0;
#$emreg = '(\\w+\\@{1})(hotmail\\.com)';
$emreg = '(geoff\\w+\\@{1})(hotmail\\.com)';
#$emreg = '([^:]\\S+\\@{1})hotmail\\.com';

$email = 'my email:geoffmclane@hotmail.com is';
$email2 = 'you can email : geoffair@hotmail.com me';
$email3 = 'my eamil is geoffmclane@hotmail.com is the';
$email4 = 'no email @ hotmail.com here';

print "[$email] to \n[".fix_email($email)."]\n";
print "[$email2] to \n[".fix_email($email2)."]\n";
print "[$email3] to \n[".fix_email($email3)."]\n";
print "[$email4] to \n[".fix_email($email4)."]\n";

$sb = stat('test2.pl');

print "Time ".scalar localtime($sb->mtime)." to ".YYYYMMDD($sb->mtime)." ...\n";


sub fix_email {
	my ($eml) = shift;
	my $nem = $eml;
	if ($eml =~ /$emreg/i) {
		$nm = $1.$2;
		$sm = mangled_email($nm);
		$ind = index($eml, $nm);
		if (!($ind == 1)) {
			$nem = substr($eml,0,$ind);
			$nem .= $sm;
			$nem .= substr($eml, $ind+length($nm));
		}
		print "got [$nm] ... now [$sm] ... ind $ind ...\n" if $dbgem;
	} else {
		print "failed\n" if $dbgem;
	}
	return $nem;
}

sub mangled_email {
	my ($em) = shift;
	$em =~ s/geoffmclane/geoffair/i;
	$em =~ s/\./ _dot_ /;
	$em =~ s/\@/ _at_ /;
	return $em;
}

################################################
# My particular time 'translation'
sub YYYYMMDD {
	#  0    1    2     3     4    5     6     7     8
	my ($tm) = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tm);
	$year += 1900;
	my $ymd = "$year/";
	if ($mon < 10) {
		$ymd .= '0'.$mon.'/';
	} else {
		$ymd .= "$mon/";
	}
	if ($mday < 10) {
		$ymd .= '0'.$mday;
	} else {
		$ymd .= "$mday";
	}
	return $ymd;
}

