#!/usr/bin/perl

# Copyright (c) 2004 Joachim Breitner
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


# This little script takes a gnomeicu icq log directory and 
# adds the entries to gaim's log.

# This was a quick hack, improvement ideas are welcome.
# Contact me at nomeata@debian.org, or via jabber: joachimbreitner@amessage.de
# or via icq: 74513189

use strict;
use warnings;

my ($mynick, $gaimdir) = @ARGV;

unless ($gaimdir) {
	print "Syntax: $0 mynick ~/.gaim/logs/icq/12345\n";
	print "Where mynick is your nickname and 12345 is your ICQ number.\n";
	exit;
}

chdir "$ENV{HOME}/.icq/history" or die "Could not change to ~/.icq/history\n";

for my $infile (<*.txt>) {
	print "Reading $infile\n";
	unless ($infile =~ /^(\d+).txt$/) {
		print "Not the right format (54321.txt), skipping\n";
	}
	my $icqnum = $1;

	unless (-d "$gaimdir/$icqnum") {
		print "Creating gaim history directory for $icqnum\n";
		mkdir "$gaimdir/$icqnum" or
			die "Failed: $!\n";
	}

	open INFILE, '<', $infile or die "Could not open $infile: $!\n";
	
	my $lastdate = '';
	my $message;
	my $outfh;
	while (<INFILE>) {
		if (/^$/) { next };
		if (/^\*\*\* [RS] /)  {
			my ($type, $d, $m, $y, $t) =
				/^\*\*\* ([RS]) \d+ .* (\d\d) ([\w]+|Mär) (\d\d\d\d) (\d\d:\d\d:\d\d) .* \*\*\*$/;
			   ($type, $m, $d, $t, $y) =
			   	/^\*\*\* ([RS]) \d+ .* ([\w]+|Mär)  ?(\d+) (\d\d:\d\d:\d\d) (\d\d\d\d)/ unless $type;
			die "Could not parse:\n$_" unless $type;
			if ($message) {
				if ($message->{date} ne $lastdate) {
					if ($outfh) {
						close $outfh;
						print "|";
					}
					my $outfile = "$gaimdir/$icqnum/$message->{date}.000000.txt";
					open $outfh, '>', $outfile or die "Failed to open $outfile: $!\n";
				}
				print $outfh "($message->{time}) ";
				print $outfh "$mynick: " if $message->{type} eq 'S';
				print $outfh "$icqnum: " if $message->{type} eq 'R';
				print $outfh $message->{msg};
				print ".";
				
				$lastdate = $message->{date};
			}
			$message = {};
			$message->{date} = sprintf "%04i-%02i-%02i",$y,month($m),$d;
			$message->{time} = $t;
			$message->{type} = $type;
			next;
		}
		$message->{msg} .= $_;
	}
	if ($message) {
		if ($message->{date} ne $lastdate) {
			if ($outfh) {
				close $outfh;
				print "\n";
			}
			my $outfile = "$gaimdir/$icqnum/$message->{date}.000000.txt";
			print "Opening $outfile\n";
			open $outfh, '>', $outfile or die "Failed: $!\n";
		}
		print $outfh "($message->{time}) ";
		print $outfh "$mynick: " if $message->{type} eq 'S';
		print $outfh "$icqnum: " if $message->{type} eq 'R';
		print $outfh $message->{msg};
	}
	close $outfh or die "$!";
	print "\n";

	close INFILE;
}
		

	
sub month {
	my %monthhash = (qw/Jan 01 Feb 02 Mär 03 Apr 04 Mai 05 Jun 06 Jul 07 Sep 08 Aug 09 Okt 10 Nov 11 Dez 12/,
                        ,qw/Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Sep 08 Aug 09 Oct 10 Nov 11 Dec 12/);
	my $str=shift;
	return  $monthhash{$str} if exists $monthhash{$str};
	die "Unknown month $str\n";
}
		


