#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

use lib '%VHFFS_LIB_DIR%';

use Vhffs::Services::Mail;
use Vhffs::Services::MailingList;
use Vhffs::Robots;
use Vhffs::Main;
use File::Path;
use File::Basename;


my $vhffs = init Vhffs::Main;

Vhffs::Robots::lock( $vhffs , "mail" );

my $mails = Vhffs::Services::Mail::getall( $vhffs , Vhffs::Constants::TO_DELETE );
my $mail;
my $lists;
my $list;
foreach $mail ( @{$mails} )
{
	my $lists = Vhffs::Services::MailingList::getall( $vhffs , undef , undef , undef , $mail->get_domain );
	if( @$lists != 0) {
		foreach $list ( @{$lists} ) {
			$list->set_status( Vhffs::Constants::TO_DELETE );
			$list->commit;
		}
		Vhffs::Robots::vhffs_log( sprintf( 'Cannot delete mail domain %s, remains lists, requests have been sent to delete them' , $mail->get_domain ) , $vhffs);
		# Do not call next right now, check for boxes to avoir further failure
	}

	if( $mail->nb_boxes() > 0 ) {
		foreach my $bo ( values( %{$mail->get_boxes()} ) ) {
			$mail->set_box_status( $bo->{'local_part'} , Vhffs::Constants::TO_DELETE );
		}
		Vhffs::Robots::vhffs_log( sprintf( 'Cannot delete mail domain %s, remains %d boxes, requests have been sent to delete them' , $mail->get_domain , $mail->nb_boxes() ) , $vhffs);
		next;
	} 

	next if(@$lists);

	Vhffs::Robots::vhffs_log( sprintf( "Delete mail domain %s" , $mail->get_domain ) , $vhffs);
	my $dir = $mail->get_dir();

	if( defined($dir) && -d $dir && -r _ && -w _ && -x _ )  {
		my $dir = $mail->get_dir;
		my $errors=[];
		File::Path::rmtree( $mail->get_dir, { error => \$errors });
		# Mail domain directories are hashed on two levels, so we've 3 potentially
		# empty directories to delete
		$dir = dirname($dir);
		rmdir $dir;
		$dir = dirname($dir);
		rmdir $dir;
		
		if(@$errors) {
			Vhffs::Robots::vhffs_log( sprintf( 'Something went wrong during %s mail domain deletion: %s', $mail->get_domain, join(', ', @$errors) ) );
		}
	} else {
		Vhffs::Robots::vhffs_log( sprintf( 'Unable to get mail domain directory or bad permission for mail domain %s, aborting file system deletion', $mail->get_domain ) );
	}
	$mail->delete;
}

Vhffs::Robots::unlock( $vhffs , "mail" );
exit 0;

