#!/usr/bin/perl
# end_image mac iuuid

use File::Path qw(rmtree);

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

my $AddrMAC=shift @ARGV;
my $iuuid=shift @ARGV;

my $host=&GetHostByMac($AddrMAC);

system("echo \"`date +'%F %T'`: end_image $AddrMAC($host) $iuuid\" >>/var/log/deploy/endimage.log");

if ( ! $host ) {
	print "\@mac $AddrMAC non trouve";
	exit 1;
}
$host = (split '\.', $host)[0];

my $iuuidfile="$deploydir/computers/$host/image.iuuid";
unlink $iuuidfile if (-f $iuuidfile);

# recup iuuid 
my $masterdir="$deploydir/masters/$iuuid";	
if ( -d $masterdir ) {
	if ( -f "$masterdir/OSinfo.txt" ) {
		# genre le postinst
		mkdir "$masterdir/postinst.d", 0755;
		system("cp $deploydir/kwartz/postinst.sh $masterdir/postinst.d/kwartz-sysprep");
	}

	# Sauvegarde le Hash NTPassword
	my $hashnt = `pdbedit -Lw $host\$ | awk -F \":\" \'\/[0-9]\/ {print \$4}\'`; chomp $hashnt;
	system("echo \"hashnt=$hashnt\" >> $masterdir/reginfo.txt");

	rmtree("$masterdir.bak") if ( -d "$masterdir.bak" );
	# change owner
	my @id=`id pulse2`;
	if ( $?==0 ) {
		`chown -R pulse2: $masterdir`;
	}
} else {
	warn "$masterdir non trouv\n";
	exit 1;
}
exit 0
