#!/usr/bin/perl

if(defined($ARGV[0])) {
    if($ARGV[0] eq "q") {
	qx{grep -E "orrery|\\*\\*\\*|Solinari is|Lunitari is|Nuitari is| Day of |Local Time|o'clock|Uptime     : " nil_G* >.nil};
	qx{grep -Ev "says|shouts|tells" <.nil >.moons};
	exit(0);
    } elsif($ARGV[0] eq "p") {
	print qx(cat ????/.moons .moons |moons.pl);
#       print STDERR "DEBUG: Restricted database\n";print qx(cat .moons |moons.pl);
	exit(0);
    } else {
	print "Use 'q' to grep, 'p' to show results.\n";
	exit(0);
    }
}
       
#use:
#  moons.pl q
#  cat ????/.moons .moons |moons.pl|sort -n >nil

%rlcal = ( "Jan" => "01","Feb" => "02","Mar" => "03",
	       "Apr" => "04","May" => "04","Jun" => "06",
	       "Jul" => "07","Aug" => "08","Sep" => "09",
	       "Oct" => "10","Nov" => "11","Dec" => "12",
	       );

%cal = ( 
 "Winter" => "01",
 "Winter Wolf" => "02",
 "Frost Giant" => "03",
 "Old Forces" => "04",
 "Grand Struggle" => "05",
 "Spring" => "06",
 "Nature" => "07",
 "Futility" => "08",
 "Dragon" => "09",
 "Sun" => "10",
 "Heat" => "11",
 "Battle" => "12",
 "Dark Shades" => "13",
 "Shadows" => "14",
 "Long Shadows" => "15",
 "Ancient Darkness" => "16",
 "Great Evil" => "17",
"Aelmont" => "01",
"Winter Night" => "02",
"Deepkolt" => "03",
"Brookgreen" => "04",
"Spring Dawning" => "05",
"Chilsmont" => "06",
"Fleurgreen" => "07",
"Corij" => "08",
"Fierswelt" => "09",
"Summer Run" => "10",
"Paleswelt" => "11",
"Reorxmont" => "12",
"Reapember" => "13",
"Autumn Twilight" => "14",
"Darkember" => "15",
"Frostkolt" => "16",
"Winter Come" => "17",
 );

%mcycle = (
	   "Lunitari" => 28,
	   "Solinari" => 36,
	   "Nuitari" => 8,
	   );

%mphases =(
    "low" => 1,
    "new" => 2,
    "waxing" => 3,
    "high" => 4,
    "waning" => 5,
    "full" => 6,
);


$warn=0;
$time="";$date="";$process=0;
while(<>) {
    $line++;
    if(/look orrery/) {
	$time="";$date="";$clock="";$uptime="";undef(%phase);
	#if($process) {$warn++;} #print STDERR "Process warning 1 ($line)..\n";}

	#print "Processing..\n";
	$process=1;next;

    }
    if(/:\*{42}([^\*]+|\Z)/) {
	#print "Finished..\n";
	if(!$process) {$warn++;} #print STDERR "Process warning 2 ($line)..\n";}
	elsif(("$time" ne "") && ("$clock" ne "") && ("$date" ne "") &&
	      defined(%phase)) {
	    $date=sprintf("%s+%02d",$date,$clock);
	    foreach $moon (sort keys %phase) {
		$moons{"$moon"}{"$date"}{'phase'}=$phase{"$moon"};
		$moons{"$moon"}{"$date"}{'uptime'}=$uptime;
		$moons{"$moon"}{"$date"}{'time'}=$time;
	    }
	    $time="";$date="";$clock="";$uptime="";undef(%phase);
	}
	$process=0;
	next;
    }

    if($process) {
	if(/Local Time : \w+ (\w+) (\d+) (\d+):.* 20(\d+) ([A-Z]{3,4})/) { #PST|PDT|EST|EDT|CDT etc
	    $time="$4-$rlcal{$1}-$2+$3";	    
	    #print "Time matched: $time\n";
	    next;
	}
	if(/Uptime     : (.*)/) {
	    $uptime="$1"; $utd=0;$uth=0;$utm=0;
	    if($uptime=~/(\d+) day/) {$utd=$1;}
	    if($uptime=~/(\d+) hour/) {$uth=$1;}
	    if($uptime=~/(\d+) min/) {$utm=$1;}
	    $uptime=sprintf("%2dd %2dh %2dm",$utd,$uth,$utm);
	    #print "Uptime matched: $uptime\n";
	    next;
	}
	if(/It is (\d+).*clock (am|pm)/) {
	    $clock=$1;if("$2" eq "pm") {$clock+=12;}
	    #print "Clock matched: $clock\n";
	    next;
	}
	if(/The (\d+)\w* Day of the Month of (.*), Year (\d+)\./) {
	    $year=$3;$day=$1;
	    $mons=$2;$mons =~s/the //g;$mon=$cal{$mons};
	    $date=sprintf("%03d-%02d-%02d",$year,$mon,$day);
	    #print "Date matched: $date ($mons)\n";
	    next;
	} elsif (/The (\d+)\w* Day of (.*), Year (\d+)\./) {
	    $year=$3;$day=$1;
	    $mons=$2;$mons =~s/the //g;$mon=$cal{$mons};
	    $date=sprintf("%03d-%02d-%02d",$year,$mon,$day);
	    #print "Date matched: $date ($mons)\n";
	    next;
	}
	if(/(Nuitari|Solinari|Lunitari) is.* (high|low|waxing|waning|full|new) .*\./) {
	    $phase{"$1"}=$2;
	    #print "Phase matched: $1=$2\n";
	    next;
	}
    }
}
if($warn>0) {print STDERR "$warn warnings.\n";}

$prevPhase=0;$prevprevPhase=0;$prevDate=0;
foreach $keyMoon (sort keys %moons) {
    foreach $keyDate (sort keys %{$moons{"$keyMoon"}}) {
	$tmp=sprintf("$keyMoon:$keyDate\t=$moons{$keyMoon}{$keyDate}{'phase'}");
	$tmp=sprintf("%-30s $moons{$keyMoon}{$keyDate}{'uptime'} ($moons{$keyMoon}{$keyDate}{'time'}) ",$tmp);
	#Find out if we passed full-moon
	$currPhase=$mphases{$moons{$keyMoon}{$keyDate}{'phase'}};
	$keyDate=~ /\-(\d+)\-(\d+)\+/; $currDate=$1*$mcycle{$keyMoon}+$2;
	#Mark passing of full-moon with stars
	
	if($prevDate<($currDate-$mcycle{$keyMoon})) {$tmp.="**";}
	elsif($currPhase==$mphases{"full"}) {$tmp.=".";$passed=0;}
	elsif(!$passed && $prevprevPhase>$currPhase && $prevPhase>$currPhase &&
	      $prevprevPhase!=$mphases{"full"} && $prevPhase!=$mphases{"full"}) {
	    $passed=1;$tmp.="*";}
	
	print "$tmp ";
	print "\t($currPhase,$prevPhase,$prevprevPhase:$passed)\n";

	if("$prevPhase" ne "$currPhase") {
	    $prevprevPhase=$prevPhase; $prevPhase=$currPhase;$passed=0;}
	$prevDate=$currDate;	   
    }
}

