#!/usr/bin/perl

use ovhmlib;
$sav = "/home/ovhm/";

do '../web-lib.pl';
$|=1;
&init_config();
%access = &get_module_acl();

&ReadParse();

&error ("Le module de gestion de mysql n'est pas utilisable") unless &foreign_check("mysql");
&foreign_require("mysql","mysql-lib.pl");

&error ("Le module de gestion d'apache n'est pas utilisable") unless &foreign_check("apache");
&foreign_require("apache","apache-lib.pl");

&error ("Le module de gestion de bind8 n'est pas utilisable") unless &foreign_check("bind8");
&foreign_require("bind8","bind8-lib.pl");

$domaine=$in{domaine};
&error ("Quel domaine ?") if (!$domaine);

$serveur = &get_system_hostname();

use DB_File;
tie %LIST,'DB_File',"/etc/ovhm.db";
$login = $LIST{"$domaine"};
untie(%LIST);

$mysql = "";
$mysql = foreign_call("mysql","execute_sql_logged","mysql","select * from user where User=\'$login\'");


if (!$in{oui}) {
&header("Effacer ?", "", undef, 0, 1, 1, "Written by<BR><A HREF=mailto:hardware\@ovh.net>OVH</A><BR><A HREF=http://sd.ovh.fr>http://sd.ovh.fr</A>");
        print <<EOF;
<p>Etes vous sur de vouloir effacer le domaine $domaine, toutes les données seront effacées  ?
<form action=effacer_domaine.cgi>
<input type=hidden name=domaine value="$domaine">
<br><input type=radio name=oui value=oui>Oui
<br><input type=radio name=oui value=non checked>Non
<p><input type=submit value=Go>
</form>
</body></html>
EOF
        exit;
} 

if ($in{oui} eq "oui") {

	mkdir($sav,0755) if (! -d $sav);
	($sec,$min,$heure,$mjour,$mois,$annee) = localtime(time);
	$date = $annee + 1900 . ((($mois + 1) < 10) ? "0" : "") . $mois + 1 . ((($mjour) < 10) ? "0" : "") . $mjour . $heure . $min . $sec;

	# alias
	tie %LIST,'DB_File',"/etc/ovhm.db";
	foreach my $key (sort keys %LIST){
	        $str = $domaine . "_alias_";
	        if ($key =~ /$str/) {
	                my $val = $LIST{$key};
			# bind
			ovhmlib::bind_effacer($sav, $date, $val);

			# apache
			
			# qmail
			$cmd = "/home/vpopmail/bin/vdeldomain $val";
			system ("$cmd");

			# fin
			$str = $domaine . "_alias_" . $val;
			delete $LIST{$str};
	        }
	}
	untie(%LIST);

	# apache
	system "cp /usr/local/apache/conf/httpd.conf $sav/httpd.conf.$date";
	$conf = &foreign_call("apache","get_config");
	@virt = &foreign_call("apache","find_directive_struct","VirtualHost", $conf);
	foreach $v (@virt) {
	        $vm = $v->{'members'};
	        $sname = &foreign_call("apache","find_directive","ServerName", $vm);
		if ($sname eq "www.$domaine") {
			$id = &indexof($v, @$conf);
			last;
		}
	}
	$vconf = $conf->[$id];
	&lock_file($vconf->{'file'});
        $lref = &read_file_lines($vconf->{'file'});
        splice(@$lref, $vconf->{'line'},$vconf->{'eline'} - $vconf->{'line'} + 1);
        &flush_file_lines();
        &unlock_file($vconf->{'file'});

	# bind
	ovhmlib::bind_effacer($sav, $date, $domaine);	

	# qmail
	system "/home/vpopmail/bin/vdeldomain $domaine";

	# logs
	tie %LIST,'DB_File',"/etc/ovhm.db";
	system "rm -f /etc/webalizer.conf.$login";
	open LOGROTATE, ">/etc/cron.daily/0logrotate-webalizer";
	print LOGROTATE "#!/bin/sh\n\n";
	while (($key,$val) = each %LIST) {
        	print LOGROTATE "sudo -u $val /usr/local/bin/webalizer -c /etc/webalizer.conf.$val\n";
	}
	print LOGROTATE "\n/usr/sbin/logrotate /etc/logrotate.conf\n";
	close LOGROTATE;
	system("chmod +x /etc/cron.daily/0logrotate-webalizer");

	# user
	system ("/usr/sbin/userdel -r $login");

	# enlever de la base
	delete $LIST{$domaine};
	untie(%LIST);

	# mysql (en dernier car peut poser qq problèmes)
	if ($mysql) {
	        foreign_call("mysql","execute_sql_logged","mysql", "REVOKE ALL PRIVILEGES ON $login.* from $login\@localhost");
	        foreign_call("mysql","execute_sql_logged","mysql", "REVOKE ALL PRIVILEGES ON $login.* from $login\@$serveur");
	        foreign_call("mysql","execute_sql_logged","mysql", "drop database $login");
	        foreign_call("mysql","execute_sql_logged","mysql", "delete from user where User = \'$login\'");
	        foreign_call("mysql","execute_sql_logged","mysql","FLUSH PRIVILEGES");
	}


	# bye bye
	&redirect("");
	exit;
}

&redirect("modifier.cgi?domaine=$domaine");

