#!/usr/local/SunOS/bin/perl
#
$perldir = "/usr/local/lib/perl5";
push(@INC, "$perldir");
require Time::Local;

$tol =  $ARGV[0];
#
# edit this to change the tolerance in seconds
#

&do_squash();

exit; 

sub get_month_mday {
	local @days_in_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	local $isleap = 0;
	local $mday = 0;

	my ($year, $yday) = @_;
	
	# is leap if divisible by 4  and (not divisible by 100 || divisilbe by  400)
	if (!($year%4) && ($year%100 || !($year%400))) {
                $days_in_month[1] = 29;
        }

	$mon = 0;
	$mday = $yday;

	while ($mday > $days_in_month[$mon]) {
		$mday -= $days_in_month[$mon];
		$mon++;
	}

	$mon++;

	return ($mon, $mday);
}

sub timediff() {

	my ($date1, $date2) = @_;

	local ($year, $mon, $jday, $mday) = ();
	local ($hours, $min, $sec) = ();
	local ($time1, $time2)=();

	($year, $jday, $hours, $min, $sec) = split ('[,:]', $date1);
	($mon, $mday) = get_month_mday($year, $jday);
	$year -= 1900;
	$mon--;
	$time1 = Time::Local::timegm($sec, $min, $hours, $mday, $mon, $year);

	($year, $jday, $hours, $min, $sec) = split ('[,:]', $date2);
	($mon, $mday) = get_month_mday($year, $jday);
	$year -= 1900;
	$mon--;
	$time2 = Time::Local::timegm($sec, $min, $hours, $mday, $mon, $year);

	return ($time1 - $time2);

}

sub today ()
{
        local ($mon, $year, $hours, $min, $sec) = ();
        local ($time, $wday, $yday, $isdst) = ();
        local ($today) = ();

        ($sec, $min, $hours, $mday, $mon, $year, $wday, $yday, $isdst)
                = gmtime(time());
        $yday ++;
        $year += 1900;

        $today = sprintf ("%04d,%03d", $year,$yday);
        return ($today);
}

sub do_squash { 

	local ($infile,$network,$station,$location,$channel)=(); 
	local ($name, $oldname)=();
	local ($yyyymmdd1,$hhmmss1,$yyyymmdd2,$hhmmss2,$xyyyymmdd,$xhhmmss)=();
	local ($yyyymmdd3,$hhmmss3,$yyyymmdd4,$hhmmss4)=();
	local ($tdiff, $absdiff, $duration)=();
	local ($date1, $date2, $date3, $date4)=();

	# append squash info to header line
	$today = &today();
	$firstline = $_;	
	$firstline =~ s/\n//g;
	print "$firstline DMC/Squash,tolerance=$tol|$today\n";

	# get first net sta...  line
	while (<STDIN>) {
		($network, $station, $location, $channel,
			$date1, $date2, $max_drift) = split(/[\t|]/);

		if ("$date2" eq "") { next; }

		$oldname = $name;
		$name = "$network.$station.$location.$channel";
		print "$network|$station|$location|$channel|$date1";
		last;
	
	}

	while (<STDIN>) {
		($network, $station, $location, $channel,
			$date3, $date4, $max_drift) = split(/[\t|]/);

		if ("$date2" eq "") { next; }

		$oldname = $name;
		$name = "$network.$station.$location.$channel";
		if ("$name" ne "$oldname") { 
	
			# end old continuous
			print "|$date2|||||||||$today||\n";
	
			# start new continuous
			print "$network|$station|$location|$channel|$date3";
			$date2 = $date4;
			next; 
		}

		# same channel, see if continuous
		$tdiff = &timediff($date2, $date3);
		if ($tdiff < 0 && abs($tdiff) > $tol) {

			# end old continuous
			print "|$date2|||||||||$today||\n";
	
			# start new continuous	
			print "$network|$station|$location|$channel|$date3";
		}
	
		$date2 = $date4;
	}
	
	# end continuous
	print "|$date2|||||||||$today||\n";
}


sub gettimegm() {

	local ($mon, $year, $hours, $min, $sec) = ();
	my ($yyyymmdd, $hhmmss) = @_;

	($hours, $min, $sec) = split (':', $hhmmss);
	$sec = int ($sec);

	($year, $mon, $mday) = split ('-', $yyyymmdd);

	$mon--;
	$year -= 1900;

	$time = Time::Local::timegm($sec, $min, $hours, $mday, $mon, $year);

	return ($time);

}

sub gettime() {

	local ($mon, $year, $hours, $min, $sec) = ();
	local ($wday, $yday, $isdst) = ();
	local ($yyyymmdd, $hhmmss) = ();
	my ($time) = @_;

	($sec, $min, $hours, $mday, $mon, $year, $wday, $yday, $isdst) = 
		gmtime($time);
	
	$year += 1900;
	$mon++;
	$yyyymmdd = sprintf("%04d-%02d-%02d", $year, $mon, $mday);
	$hhmmss = sprintf("%02d:%02d:%02d", $hours, $min, $sec);

	return ($yyyymmdd, $hhmmss);

}

sub jdatetoepoch ()
{

	local ($mon, $year, $hours, $min, $sec) = ();
	local $time;
	
	my ($jday, $year) = @_;

	($mon, $mday) = &get_month_mday ($year, $jday);

	$mon--;
	$year -= 1900;

	$sec = 0;
	$min = 0;
	$hours = 0;
	$time = Time::Local::timegm($sec, $min, $hours, $mday, $mon, $year);

	return ($time);

}
