#!/usr/bin/perl -w
# $Id: pronto,v 1.1.1.1 2001/01/16 10:26:38 muhri Exp $

if(open(FILE_PREFS,  $ENV{"HOME"} . '/.prontorc')) {
	while(<FILE_PREFS>) {
	if (/^MailDir:\s*(.+)/i) { $prefs{'MailDir'} = $1; last }
	}
close(FILE_PREFS);
}
else {
	my $child;
        unless ($child = fork) {
        	die "cannot fork: $~" unless defined $child;
                exec("pronto-main",@ARGV);
        }
        exit;
}

if (defined $ARGV[0] and $ARGV[0] ne "") {
	if(open(TMP,"<$prefs{'MailDir'}/pronto.running")) {
        	my $proc = <TMP>;
                close(TMP);
                if(kill(0,$proc)) {
			open(TMP,">$prefs{'MailDir'}/pronto.address");
			foreach(@ARGV) {
				print TMP "$_\n";
			}
			close(TMP);
			kill('USR1',$proc);
                       	exit;
		}
		else {
			unlink("$prefs{'MailDir'}/pronto.running");
			my $child;
			unless ($child = fork) {
				die "cannot fork: $~" unless defined $child;
				exec("pronto-main",@ARGV);
			}
			exit;
		}
       	}
	else {
		my $child;
		unless ($child = fork) {
			die "cannot fork: $~" unless defined $child;
			exec("pronto-main",@ARGV);
		}
		exit;
	} 
} 
else {
	if(open(TMP,"<$prefs{'MailDir'}/pronto.running")) {
		my $proc = <TMP>;
		close(TMP);
		if(kill(0,$proc)) {
			kill('USR1',$proc);
			exit;
		}
		else {
                	unlink("$prefs{'MailDir'}/pronto.running");
			unless ($child = fork) {
				die "cannot fork: $~" unless defined $child;
				exec("pronto-main",@ARGV);
			}
			exit;
		}
	} 
	else {
		unless ($child = fork) {
			die "cannot fork: $~" unless defined $child;
			exec("pronto-main",@ARGV);
		}
		exit;
	}
}
