#!/usr/bin/perl
use CGI qw(param);

$home="/usr/local/e-cron";
$server = param("server");
$tmp ="/tmp/${server}.delete.map.tmp";

@jobnet_list=`sudo -u ejobmgr ${home}/bin/for_cgi/distributed_jobnets $server`;

print "Content-type: text/html\n\n";
###############contents insert#########
system("cat ../elements/header");
unless($server){
  print "Server is not defined, please retry it";
  system("cat ../elements/footer");
  exit 1
}

print "<pre>";
foreach (@jobnet_list) {
  @project_jobnet=split(/,/, $_);
  $project=@project_jobnet[0];
  $jobnet=@project_jobnet[1];
  system("sudo -u ejobmgr $home/bin/for_cgi/delete_jobnet $project/$jobnet $server");
}

system("cat $home/map.conf | grep -v ${server}: > $tmp");
system("sudo -u ejobmgr cp $tmp $home/map.conf");
system("rm $tmp");
system("sudo -u ejobmgr rm -rf ${home}/servers/${server}");
system("sudo -u ejobmgr rm ${home}/que/server_status/${server}");
system("sudo -u ejobmgr rm ${home}/que/server_status/${server}_mirror");
print "</pre>";

if ( -d "${home}/servers/${server}"){
  print "<p>fail to delete $server</p>";
}else{
  print "<p>$server successfully deleted. please delete ejobmgr at $server manually</p>";
}

system("cat ../elements/footer");
