#!/usr/bin/perl
# ARG @mac


require '/usr/lib/kwartz/control/sharelib.pl'; 	 
require 'hostlib.pl'; 	 #load host management library
require 'deploy.pl'; 	 


my $AddrMAC=shift @ARGV;

my $host=&GetHostByMac($AddrMAC);


if ( $host ) {
	$host = (split '\.', $host)[0];
	# poste existe, on maj le menu
	# TODO voir quel menu gnrer selon config poste
	my $menu= uc($AddrMAC);
	$menu =~s/\://g;
	my $hostmenu="$deploydir/bootmenus/$menu";
	system("cp $deploydir/bootmenus/header $hostmenu");
	if ( -f $hostmenu ) {
		# maj menu en mode creation image par defaut apres KwartzSysprep
		system("/bin/sed -e 's/^default [0-9]/default 1/' -i $hostmenu")
			if (-f "$deploydir/computers/$host/image.iuuid");
	}
	if (open MENU,">>$hostmenu") {
		my @images;
		my $bootimages = $hostlist{$host}{'bootimage'};
		if ($bootimages) {
			if ($bootimages eq ' ') {
				$bootimages = $domainlist{$hostlist{$host}{'subdomain'}}{'bootimage'};
			}
			foreach (split ' ', $bootimages) {
				push (@images, $_) if (-f "$deploydir/masters/$_/conf.txt");
			}
		}
	
		foreach (@images) {
			printf MENU &ImgMenu($_);
		}
		# ajout memtest
		my $memtest="$deploydir/bootmenus/memtest";
		if ( open MEMTEST, "<$memtest") {
			while (<MEMTEST>) { print MENU $_; }
			close MEMTEST
		}
		close MENU;
	}
	
	chmod 0644, $hostmenu;
}
