#!/usr/bin/perl
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

use strict;

my %proxyKeys = (
	"imap4"	=> "zimbraImapProxyBindPort",
	"imap4s" => "zimbraImapSSLProxyBindPort",
	"pop3"	=> "zimbraPop3ProxyBindPort",
	"pop3s"	=> "zimbraPop3SSLProxyBindPort"
	);

my %serverKeys = (
	"imap4"	=> "zimbraImapBindPort",
	"imap4s" => "zimbraImapSSLBindPort",
	"pop3"	=> "zimbraPop3BindPort",
	"pop3s"	=> "zimbraPop3SSLBindPort"
	);

my $imapCapabilities = "--imap_capability \"IMAP4rev1 STARTTLS BINARY CHILDREN ID LITERAL+ LOGIN-REFERRALS NAMESPACE QUOTA SASL-IR UIDPLUS UNSELECT\"";

# NOTE  - the double spaces in this string are significant
my $popCapabilities = "--pop_capability \"TOP  USER  UIDL  STLS  EXPIRE 31 USER\"";

my %serverOpts = (
	"imap4"	=> "--ssl_mode tls_all $imapCapabilities",
	"imap4s" => "--ssl_mode ssl_all $imapCapabilities",
	"pop3"	=> "--ssl_mode tls_all $popCapabilities",
	"pop3s"	=> "--ssl_mode ssl_all $popCapabilities"
	);

my $cmd = shift;

foreach my $i (`/opt/zimbra/bin/zmlocalconfig -m shell`) {
	chomp $i;
	my ($k,$v) = split ('=',$i,2);
	$v =~ s/';$//;
	$v =~ s/^'//;
	$ENV{$k} = $v;
}

foreach my $i (`/opt/zimbra/bin/zmprov -l gs $ENV{zimbra_server_hostname}`) {
	chomp $i;
	my ($k,$v) = split (':',$i,2);
	$v =~ s/^ //;
	$ENV{$k} = $v;
}

if ($ENV{zimbraImapCleartextLoginEnabled} eq "TRUE") {
	$serverKeys{imap4s} = $serverKeys{imap4};
	$serverOpts{imap4s} = "--ssl_mode ssl_listen $imapCapabilities";
}

if ($ENV{zimbraPop3CleartextLoginEnabled} eq "TRUE") {
	$serverKeys{pop3s} = $serverKeys{pop3};
	$serverOpts{pop3s} = "--ssl_mode ssl_listen $popCapabilities";
}

sub start {
	if ( ! -f "/opt/zimbra/conf/perdition.conf" ) {
		`$ENV{zimbra_home}/libexec/zmmtaconfig perdition`;
	}
	foreach my $k (sort keys %proxyKeys) {
		if ($ENV{$proxyKeys{$k}} ne "" && $ENV{$proxyKeys{$k}} > 0) {
			print "Starting $k\n";
			my $cmd = "$ENV{zimbra_home}/perdition/sbin/perdition -P $k ".
				"--config_file=$ENV{zimbra_home}/conf/perdition.conf -p $ENV{$serverKeys{$k}} ".
				"$serverOpts{$k} ".
				"-l $ENV{$proxyKeys{$k}} -s localhost ".
				"--pid_file $ENV{zimbra_home}/log/perdition.$k.pid -u zimbra -g zimbra";
			#print "Running sudo $cmd\n";
			system ("sudo $cmd");
		}
	}
}

sub stop {
	foreach my $k (sort keys %proxyKeys) {
		my $p = getPid($k);
		print "Stopping $k\n";
		if ($p) { kill "TERM", $p; }
	}
}

sub status {
	my $status = 0;
	foreach my $k (sort keys %proxyKeys) {
		if ($ENV{$proxyKeys{$k}} ne "" && $ENV{$proxyKeys{$k}} > 0) {
			my $p = getPid($k);
			my $r = 0;
			if ($p) {
				$r = kill (0,$p);
			}
			if (!$p || !$r) {
				print "$k is not running\n";
				$status = 1;
			} else {
				print "$k is running\n";
			}
		}
	}
	return $status;
}

sub getPid {
	my $service = shift;
	if ( -f "/opt/zimbra/log/perdition.$service.pid" ) {
		my $pid = `cat /opt/zimbra/log/perdition.$service.pid`;
		chomp $pid;
		return $pid;
	}
	return 0;
}

if ($cmd eq "start") {
	start();
} elsif ($cmd eq "stop") {
	stop();
	exit 0;
} elsif ($cmd eq "restart") {
	stop();
	sleep 1;
	start();
} elsif ($cmd eq "status") {
} else {
	print "$0 start|stop|restart|status\n";
	exit 1
}

sleep 1;
exit (status());
