#!/usr/bin/perl

use ovhmlib;

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

&ReadParse();

&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);

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

use DB_File;
tie %LIST,'DB_File',"/etc/ovhm.db";

# bind
$sav = "/home/ovhm";
($sec,$min,$heure,$mjour,$mois,$annee) = localtime(time);
$date = $annee + 1900 . ((($mois + 1) < 10) ? "0" : "") . $mois + 1 . ((($mjour) < 10) ? "0" : "") . $mjour . $heure . $min . $sec;
ovhmlib::bind_effacer($sav, $date, $alias);

# apache

$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'});

@$lref = grep !/^ServerAlias $alias$/, @$lref;
@$lref = grep !/^ServerAlias www.$alias$/, @$lref;

&flush_file_lines();
&unlock_file($vconf->{'file'});

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

# fin
$str = $domaine . "_alias_" . $alias;
delete $LIST{$str};

untie(%LIST);
&redirect("alias.cgi?domaine=$domaine");
