#!/usr/bin/perl
use strict;

my $VERSION = '1.0';

my $ConfDir    = "$ENV{HOME}/.spamassassin/";
my $ConfFile   = "$ConfDir/imapsa.conf";

# Read in configuration file

my (%Conf) = readConfigFile($ConfFile);

# Print the variable values requested from the command line
foreach( @ARGV ) {
	print "$Conf{$_}\n" if defined $Conf{$_};
}

sub readConfigFile {
	my %Conf = ();
	open CONF,$ConfFile;
	while (my $line = <CONF>) {
		chomp $line; $line =~ s/^\s+//; $line =~ s/\s+$//;
		next unless $line;
		next if substr($line,0,1) eq "#";
		$line =~ m/^([\w\d]+)\s+(.+)$/;
		my $key = $1;
		my $value = $2;
		$key =~ tr/[A-Z]/[a-z]/;
		$Conf{$key} = $value;
	}
	close CONF;
	return (%Conf);
}

=pod

=head1 NAME

ImapAssassinConfig.pl - utility to read the ImapAssassin configuration file

=head1 SYNOPSIS

$ perl ImapAssassinConfig.pl server
imap.server.com
$ perl ImapAssassinConfig.pl search
UNDELETED NOT HEADER X-Spam-Status ""
$ perl ImapAssassinConfig.pl username password
your@mailbox.com
secretphrase

=head1 DISCUSSION

This utility is used by the AppleScript GUI for ImapAssassin to read the 
configuration reliably. 

It does not substitute defaults where there is no value set. 

The result does not include the variable keys on purpose, so users can
use the entire result as the value without additional parsing.

=head1 CHANGES

Mon Apr  9 15:07:41 PDT 2007
+ created with a copy of readConfigFile from ImapAssassin

=head1 CREDITS

* Adam Kalsey, who wrote IMAPAssassin. http://kalsey.com/2002/09/imapassassin/
* SpamAssassin. http://spamassassin.apache.org/

=head1 AUTHOR

Jonathan Buhacoff <jonathan@buhacoff.net>

=head1 COPYRIGHT AND LICENSE

Copyright 2007 Jonathan Buhacoff

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
