#!/usr/bin/perl

use Time::Local;

$delimiter = "--";

if (@ARGV)
	{
	$curdatestring = shift(@ARGV);
	@curdatearr = CheckForDate($curdatestring,time);
	if ($curdatearr[0] > 0)
		{
		#print("Found date in arg list, $curdatearr[0]. \n");
		$curdate = scalar($curdatearr[0]);
		}
	else
		{
		$curdate = 0;
		}
	}

$line = <stdin>;
$done = 0;

$dates = "";

@monthnames = ("Error","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

#^(.*)(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*[\s|,|-]*([0-9][0-9]?)[\s|,|-]+([0-9][0-9]([0-9][0-9])?)(.*)$/i )




while ($done == 0)
	{
	chomp($line);
	$moretosearch=1;

	while ($moretosearch==1) 
	    {
	    $found = 0;

	    #print("Checking:  $line \n");
	    #print("curdate is $curdate \n");

	    @thisdate = CheckForDate($line,scalar($curdate));


	    if ($thisdate[0] > 0)
		{
		#print("Found date: $thisdate[0] \n");

		push (@datenumbers, $thisdate[0]);
		$line = $thisdate[1];
		}
	    else
		{
		$moretosearch = 0;
		}		    
    	    }

	$line = <stdin>;

	if ($line =~ /end-of-input/)
		{
		$done = 1;
		}
	if (!defined($line))
		{
		$done = 1;
		}
	}


# Dump out all dates.

@datenumbers = RemoveDuplicates(@datenumbers);
@datenumbers = (sort {$a <=> $b} @datenumbers);

while (scalar(@datenumbers))
	{
	$thisdatenum = shift(@datenumbers);
	($thisday,$thismonth,$thisyear)=(localtime($thisdatenum))[3,4,5];
	$thismonth = $thismonth+1;
	$thisyear = $thisyear+1900;
	$dateform = $monthnames[$thismonth]." ".$thisday.", ".$thisyear;
	print("$dateform");
	if (scalar(@datenumbers))
		{
		print("$delimiter");
		}
	}



sub MonthNum
#
#       Function takes a month name (string) and returns
#       the month number (1-12).
#
#
#       INPUT:
#               0) - String containing month name.
#
#       OUTPUT:
#               0) - Month number.  (0 if not recognized.)
#

        {
        my($count);
        my(@months) = ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");

        $retval = 0;

        for ($count=0; $count<12; $count++)
                {
                $mon = $months[$count];
                if ($_[0] =~/$mon/i  )
                        {
                        $retval = $count+1;
                        }
                }
        return($retval);
        }


sub daymonthyear2datenum
#
#
#
	{
	my ($day) = shift(@_);
	my ($month) = shift(@_);
	my ($year) = shift(@_);


	if (scalar($year) == 0)
		{
		$year = (localtime(time))[5]+1900;
		}
	else	
		{
		if (scalar($year) < 70)
			{
			$year = $year + 2000;
			}
		else
			{
			if (scalar($year) < 200)
				{
				$year = $year + 1900; 
				}
			}
		}
	my($datenum) = timelocal(0,0,0,$day,$month-1,$year);
	#print("Converted $day/$month/$year to date number $datenum. \n");
	return ($datenum);
	}


sub CheckForDate
#
#	Subroutine looks at the given string for a date.
#	Returns epoch-time,excess-characters
#	(if D==0, no date found)
#	epoch time is the number of seconds since Jan 1 1970.
#

	{
	my($dateline) = shift(@_);
	my($relativedate) = shift(@_);
	#print("Relative date is $relativedate \n");
	
	my($mstart) = "^(.*)";
	my($mmonth) = "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*";
	my($msep1) = "[\\s|,|-|\\\/]?[\\s|,|-|\\\/]?";	# space, comma, - (0,1, or 2).
	my($msep2) = "[\\s|,|-|\\\/][\\s|,|-|\\\/]?";	# space, comma, - (1, or 2).
	my($msep3) = "[^0-9]";			# non-number.
	my($msep4) = "[-|\\\/]";			#  - or /
	my($mday) = "([0-9][0-9]?)";		# one or two digits.
	my($mdayth) = "([0-9][0-9]?)(th|rd|st)?";		# one or two digits.
	my($myear) ="([0-9][0-9]([0-9][0-9])?)?";
	my($mend) = "(.*)\$";



	$output[0] = 0;

	# --------------------- Check 1:  Form "Jan 4, 2001" --------------
	$matchstr1 = $mstart.$mmonth.$msep1.$mdayth.$msep2.$myear.$msep3.$mend;
	if ($output[0]==0)
	    {
	    if (($dateline." xxx") =~ /$matchstr1/i)

		{

		$day = scalar($3);
		$year = scalar($5);
		$month = MonthNum($2);

		#print("Found date $day / $month / $year \n");
		$excess = $1 . "X" . $7;	# rest of line.

		@output = (daymonthyear2datenum($day,$month,$year), $excess);
		}
	    }

	# --------------------- Check 2:  Form "12/25/2001"  --------------
	$matchstr2 = $mstart.$msep3.$mday.$msep4.$mday.$msep4.$myear.$msep3.$mend;
	if ($output[0]==0)
	    {
	    if (("x".$dateline."x") =~ /$matchstr2/i)

		{
		$month = scalar($2);
		$day = scalar($3);
		if ($month > 12)	# assume M/D/Y, but support D/M/Y 
			{
			$temp = $month;
			$month = $day;
			$day = $month;
			}
		$year = scalar($4);
		$excess = $1 . "X" . $6;	# rest of line.
		#print("Found date $day / $month / $year \n");

		@output = (timelocal(0,0,0,$day, $month-1, $year), $excess);
		#@output = ($day, $month, $year, $excess);
		}
	    }

	# --------------------- Check 3:  Form "2001/25/1"  --------------
	$matchstr3 = $mstart.$msep3.$myear.$msep4.$mday.$msep4.$mday.$msep3.$mend;
	if ($output[0]==0)
	    {
	    if (("x".$dateline."x") =~ /$matchstr3/i)

		{
		$year = scalar($2);
		$month = scalar($4);
		$day = scalar($5);
		$excess = $1 . "X" . $6;	# rest of line.
		#print("Found date $day / $month / $year \n");

		@output = (timelocal(0,0,0,$day, $month-1, $year), $excess);
		#@output = ($day, $month, $year, $excess);
		}
	    }

	# --------------------- Check 4:  Form "4 Jan 2001" --------------
	$matchstr4 = $mstart.$msep3.$mday.$msep1.$mmonth.$msep2.$myear.$msep3.$mend;
	if ($output[0]==0)
	    {
	    if (("x".$dateline."x") =~ /$matchstr4/i)

		{
		$day = scalar($2);
		$year = scalar($4);
		$month = MonthNum($3);
		$excess = $1 . "X" . $6;	# rest of line.
		#print("Found date $day / $month / $year \n");

		@output = (timelocal(0,0,0,$day, $month-1, $year), $excess);
		#@output = ($day, $month, $year, $excess);
		}
	    }

	# --------------------- Check 5:  Today ------------------

	$matchstr5 = $mstart."today".$mend;
	if (($output[0]==0) && (scalar($relativedate)>0))
	    {
	    #print("Checking for today.\n");
	    if (("x".$dateline."x") =~ /$matchstr5/i)

		{
	    	#print("Found today.\n");
		$excess = $1 . "X" . $2;	# rest of line.
		@output = ($relativedate, $excess);
		}
	    }

	# --------------------- Check 6:  Tomorrow ------------------
	$matchstr6 = $mstart."tomorrow".$mend;
	if (($output[0]==0) && (scalar($relativedate)>0))
	    {
	    if (("x".$dateline."x") =~ /$matchstr6/i)

		{
		$excess = $1 . "X" . $2;	# rest of line.
		@output = ($relativedate+86400, $excess);
		}
	    }

	return(@output);
	}



sub RemoveDuplicates
#
#	Removes any elements that occur twice or more in an array.
#

	{
	my (@arr) = @_;
	my ($arrsize) = scalar(@arr);

	my ($notdone) = 1;

	while ($notdone)
	    {
	    $notdone = 0;
	    for ($count1 = 0; $count1 < $arrsize; $count1++)
		{
		for ($count2 = $count1+1; $count2 < $arrsize; $count2++)
			{
			if ( $arr[$count1] eq $arr[$count2] )
				{	
				splice(@arr,$count2,1);
				$count2--;
				$arrsize--;
				$notdone=1;
				}
			}
		}
	    }
	return (@arr);
	}
