#!/usr/bin/perl
#
#  Take stdin - fixup the data and dump it to stdout
#
#open (OUTPUTfile, ">no_email-userMissing.txt") or print ("open no_email-usermissing.txt failed\n");
#
# Format is 15/12/13  11:29:00 AM,C,5   - C is Celcius, 5 is temp reading

while (<>){
	next if ! /[0-9]\/[0-9]/;		# skip to next line if it's not a datestamped data line
	s/[\n\r]+//;            # strip CR and LF chars
	@line_args = split /,/, $_;
	$temp_reading = $line_args[2];

	$date_part = $line_args[0];
	@date_args = split /\s{1,}/, $date_part;	# split by any number of whitespaces
	$date = $date_args[0];
	$time = $date_args[1];
	$am_pm = $date_args[2];

	@date_args = split /:/, $time;

# Thermocron data goes from 11am to 12pm to 1pm  and 11pm to 12am to 1am - so just adding 12 fails

	if ($am_pm eq 'PM'){
		$hour = $date_args[0] + 12;
		$hour = 12 if $hour == 24;
		$time = $hour . ':' . $date_args[1] . ':' . $date_args[2];
	} else {
		$hour = $date_args[0];
		$hour = 0 if $hour == 12;
		$time = $hour . ':' . $date_args[1] . ':' . $date_args[2];
	}

	print "$date $time $temp_reading\n";
}
