#!/usr/bin/perl -w
use strict;

##############################################
#                                            #
#       pine2mutt 0.2 - A Pine to Mutt       #
#          Address Book Converter            #
#         written by Jan Schaumann           #
#                                            #
#   pine2mutt 0.2 is published under the     #
#     GPL and comes with no warranty         #
#                 whatsoever                 #
#                                            #
#  You can obtain pine2mutt 0.2 (or maybe    #
#      even more recent versions) from       #
#         http://www.netmeister.org          #
#                                            #
#       Please send all Bug-Reports to       #
#          <jschauma@netmeister.org>         #
#                                            #
#     This program is Free Software and      #
# is published under the GNU Public License  #
#         see COPYING for details            #
#                                            #
##############################################


#declaration of variables used up here
my $pine =  "$ENV{HOME}/.addressbook";
my $mutt =  "$ENV{HOME}/.pine2mutt";
my $version = "0.2";
my $pname = "pine2mutt";

###
### command-line arguments handling

if (($ARGV[0]) && ($ARGV[0] ne "-h") && ($ARGV[0] ne "-v"))
{
	$pine = $ARGV[0];
}

if ($ARGV[1])
{
	$mutt = $ARGV[1];
}

if ($ARGV[2] || ($ARGV[0] && ($ARGV[0] eq "-h")))
{
	print "$pname - a little script to convert a pine-addressbook to a mutt-aliases-file\n";
	print "Usage: $pname [-h] [from] [to]\n";
	print "from:\tpine-addressbook (default is ~/.address.book)\n";
	print "to:\tmutt-aliases-file (default is ~/.pine2mutt)\n";
	exit(0);
}

if ($ARGV[0] && ($ARGV[0] eq "-v"))
{
	print "$pname $version\n";
	exit(0);
}

### end command line arguments handling
###


###
### begin addressbook conversion

open(PINE, "$pine") or die "$! - Cannot open pine's addressbook \'$pine\'!\n";
open(MUTT, ">$mutt") or die "$! - Cannot open mutt's \'$mutt\'!\n";

my $nick;
my $name;
my $email;
my $tmp;

print MUTT "###\n# This file is generated by pine2mutt and will be overwritten";
print MUTT "\n# every time you run pine2mutt (unless you specify an alternate";
print MUTT "\n# ile, of course) - if you do not run pine2mutt again, you can";
print MUTT "\n# edit this file by hand.\n#";
print MUTT "\n# For details see pine2mutt(1) and/or";
print MUTT "\n# http://www.netmeister.org/apps/pine2mutt/\n###\n";
print MUTT "\n###\n# begin addresses imported from $pine\n###\n\n";

while (<PINE>)
{

	next if /^#DELETED/;	# pine keeps deleted email-addresses for a while

	if (s/\(//)				# pine has email-lists in parentheses
	{
		next unless ($nick, $name, $tmp) = /(.*?)\t(.*?)\t(.*?)$/;	# skip if it does not have three tabs
	}
	else
	{
		unless (/^\s/)	# ^\s means an email-list continues 
		{
			next unless ($nick, $name, $email) = /(.*?)\t(.*?)\t(.*?)$/;	# a normal entry is nick\tname\temail
			$email =~ s/(.*?)\s(.*?)$/$1/;	# sometimes additional information is appended to the email in $4
			print MUTT "alias $nick $email ($name)\n";
		}
	}

	if (($tmp) and (/^\s/))	# we have an email-list
	{
		if (s/\)//)		# end of the email-list
		{
			$tmp = $tmp . $_;
			$tmp =~ s/\s//g;
			$email = $tmp;
			print MUTT "alias $nick $email ($name)\n";	
		}
		else
		{
			$tmp = $tmp . $_;
		}
	}
}

print MUTT "\n###\n# end addresses imported from $pine\n###\n\n";

close (PINE);

### end addressbook conversion
###

###
### begin pine-like key-bindings

my $pinerc = `locate Pine.rc`;
if ($pinerc)
{
	print MUTT "\n###\n# Mutt brings some bindings to make it behave";
	print MUTT "\n# somewhat like pine - see that file for details:\n###";
	print MUTT "\nsource $pinerc";
}

###
### end pine-like key-bindings

###
### begin pine-like sent-folder

my $found = 0;
my $record;

open(MUTTRC, "$ENV{HOME}/.muttrc") or die "$!\n";
while(<MUTTRC>)
{
	if (/set record/)
	{
		$found = 1;
		chomp($record = $_);
	}
}

if ($found)
{
	print MUTT "\n\n###\n# WARNING - this overwrites\n";
	print MUTT "# \"$record\"\n";
	print MUTT "# from $ENV{HOME}/.muttrc\n";
}
else
{
	print MUTT "\n\n###\n# Pine-like sent-folder handling\n";
}

print MUTT "# Mutt does not delete the sent-folder at the end of the month\n";
print MUTT "# if you want this, you need to set up a crontab-entry\n";
print MUTT "###\n";
print MUTT "set record=\"=sent-mail-`date +%b-%Y`\"\n";

### end pine-like sent-folder
###


###
### now tell .muttrc where to look for pine-stuff

$found = 0;

open(MUTTRC, "$ENV{HOME}/.muttrc") or die "$!\n";
while(<MUTTRC>)
{
	if (/source (\~\/.pine2mutt)|($mutt)/)
	{
		$found = 1;
	}
}

close(MUTTRC);

if (!$found)
{
	open(MUTTRC, ">>$ENV{HOME}/.muttrc") or die "Can't open $ENV{HOME}/.muttrc!\n";
	print MUTTRC "\n###\n# aliases and pine-like keybindings from pine2mutt\n";
	print MUTTRC "# WARNING - these might overwrite otherwise specified bindings\n";
	print MUTTRC "# You should probably check that file to make sure all's well\n###\n";
	print MUTTRC "source $mutt\n";
	close(MUTTRC);
}

### we're done - bye bye
###
