#!/usr/local/bin/perl -w
#
#  $Id: logconv,v 1.1 2000/10/23 20:34:48 dgregor Exp $
#
# Copyright (c) 1998-2000 Daniel J. Gregor, Jr., All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# 	This product includes software developed by Daniel J. Gregor, Jr.
# 4. The name of Daniel J. Gregor, Jr. may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
# 
# THIS SOFTWARE IS PROVIDED BY DANIEL J. GREGOR, JR. ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL DANIEL J. GREGOR, JR. BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
 
use Time::Local;

%months = (
	'Jan' => 0,
	'Feb' => 1,
	'Mar' => 2,
	'Apr' => 3,
	'May' => 4,
	'Jun' => 5,
	'Jul' => 6,
	'Aug' => 7,
	'Sep' => 8,
	'Oct' => 9,
	'Nov' => 10,
	'Dec' => 11
);

$smallyear = (localtime())[5];	# it really sucks that syslog doesn't log
				# the year, so we assume the current year.
				# XXX probably should add a command-line
				# XXX option to select the year

while (<>) {
    # Let me describe this fairly large block of gunk below:
    # Here, a pattern match is done on $_, and the appropriate information is
    # placed in variables.  The if statement that wraps the pattern match
    # checks to see that it matches everything.  If not, it checks to see
    # if this line is a continuation, and if not, it warns.
    if (! (
      ($monthname, $day, $hour, $minute, $second, $host, $program, $PID, $rest)
       = /^([A-Z][a-z]+) +(\d+) (\d+):(\d+):(\d+) (\w+) (\w+)\[(\d+)\]: (.*)$/)
      ) {

	# see if this is a continuation, and if so, just print the info
	if (($monthname, $day, $hour, $minute, $second, $host, $rest)
            = /^([A-Z][a-z]+) +(\d+) (\d+):(\d+):(\d+) (\w+) (.*)$/ ) {
	  print $rest;
	} else {
          warn("Pattern match error--poorly formatted line $.: $_\n");
	}

	next;
    }

    if (($month = $months{$monthname}) eq "") {
        warn("Bad month on line $.: $monthname");
	next;
    }

    $time = &timelocal($second, $minute, $hour, $day, $month, $smallyear);

    print "\n" if ($. > 0);	# end the previous line, if there was one
    print "$time\t$host\t$program\t$PID\t$rest";

}

if ($. > 0) {
	print "\n";
}
