#!/usr/bin/perl
 
use LWP::Simple qw(get $ua);
use LWP::Simple;
use Date::Calc qw(Delta_DHMS);
use Net::SMTP;
 
# $ua->proxy('http', 'http://proxy.com.au:8080/');
# $ua->no_proxy('localhost');

# LIVE 
$iris="http://www.iris.washington.edu/cgi-bin/seismon_events.pl?date=00/00/00&lat=0&lon=0";

# TEST
# $iris="http://203.41.19.155/iris.html";
# $iris="http://localhost/iris.html";

$|=1;
$version = "Seismonitor 0.9"; 
$datfile = "/seismonitor/seis.dat";
$tmpfile = "/seismonitor/.seis.tmp";
$logfile = "/seismonitor/seis.log";
$pidfile = "/seismonitor/seis.pid";
 
$init=0;

while (1) {
sleep(45); # poll rate # 45 secs
$is_up=0; 

PING: while ($is_up==0) {
	my $url = getstore($iris, $tmpfile) or warn($!);
	if (is_error($url)) {
		$i=0 if ($i==15);
		print STDOUT qq(Can't ping site ...\n);
		&logFile("Couldn't ping site") if ($i++==0); 
		sleep(60); # 60
		next PING;
	} else {
		$is_up=1;
	}
}

open(TMP, "<$tmpfile") or warn("Can't open tmp file for reading: $!");
while (<TMP>) {
	@line = split(/\n/, $_);
	$line[0] = &bodStr($line[0]);
	@data = split(/\s+/, $line[0]);
 
		if ($data[0] =~ /^\d\d\/\d\d\/\d\d/) { 
			$mag = $data[5];
			@location = ($data[7],$data[8],$data[9],$data[10],$data[11]);
			$printinfo = "Last event:\nDate: 20$data[0]\nTime: $data[1] (UTC)\nLatitude: $data[2]\nLongitude: $data[3]\nMagnitude: $mag\nLocation: @location\n";
			# print qq($printinfo\n);
			&processFile;
		} 
	}
close(TMP);
# unlink $tmpfile or warn "Can't delete tmp file: $!";
}
	
sub processFile {		
&calcLists;
$thistime = &timeStr($data[1]);
system("clear");
sleep(1);
print STDOUT qq($version\n\n$printinfo\n[Interval: $interval]\n);
# print qq(\n[$thistime : $lasttime]\n);
	if ($thistime != $lasttime) {
		&datFile;
		&logFile;
	}
$lasttime = &timeStr($data[1]);
&datFile;
	if ($init==0) {
		&logFile("Script started");
		&pidFile;
	}
$init=1;
last;
}	

sub calcLists {
$rmtDate = &dateStr($data[0]); 
$rmtTime = &timeStr($data[1]);
$rmtUTC = "$rmtDate"."$rmtTime";
$rmtUTC =~ /(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
@rmtUTCList = ("20$1","$2","$3","$4","$5","$6");

$lclUTC = `date -u +%y%m%d%H%M%S`;
$lclUTC =~ /(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
@lclUTCList = ("20$1","$2","$3","$4","$5","$6");

@diff = Delta_DHMS(@rmtUTCList, @lclUTCList);
$interval = "$diff[1]:$diff[2]:$diff[3]";
# if ($interval =~ /12:00?:\d{1,2}/s) {
	if ($interval =~ /12:0:\d{1,2}/s) {
		&logFile("USGS notice"); 
		
	}
}

sub datFile {
$mag = ($mag*10);
open(DAT, ">$datfile") or die "Can't open data file for writing: $!";
print DAT "$lasttime $mag\n";
close(DAT);
}

sub logFile {
$logDate = `date`;
chop($logDate);
open(LOG, ">>$logfile") or die "Can't open log file for writing: $!";
	if ($_[0]) {
		print LOG "$_[0] $logDate\n\n";
	} else {
		$mag*=.10;
		print LOG "Localtime $logDate, LocalUTC @lclUTCList, Remote @rmtUTCList, Magnitude $mag, Location @location\n\n";
	}
close(LOG);
}

sub pidFile {
open(PID, ">$pidfile") or die "Can't open pid file for writing: $!";
print PID "$$";
close(PID);
}

sub bodStr {
my ($str) = @_;
$str =~ s/<a href(.*).html>//sig;
$str =~ s/<\/a>//sig;
$str =~ s/<font color="00FF00">//sig;
$str =~ s/<\/font>//sig;
$str =~ s/<strong>//sig;
$str =~ s/<\/strong>//sig;
return $str;
}

sub timeStr {
my ($str) = @_;
$str =~ /(\d\d):(\d\d):(\d\d)/;
$str = "$1"."$2"."$3";
return $str;
}
		
sub dateStr {
my ($str) = @_;
$str =~ /(\d\d)\/(\d\d)\/(\d\d)/;
$str = "$1"."$2"."$3";
return $str;
}


