##!/usr/bin/perl
#directory to put config files
#$confdir = "/mnt/raidB/xenlib/conf"; 
$confdir = "/root/test";
if ($#ARGV + 1 != 2) {
	die("Error, append masterfile and output folder (conf directory) as argument\n");
}
$input = $ARGV[0];
$confdir = $ARGV[1];
print "Using Master File: $input\n";
open(INFILE, "$input") or die("Error: cannot open input file!\n");

print "Using configuration dir: $confdir\n";
#`rm -rf $confdir/*`;

$count=0;
while(<INFILE>) {
	$count++;
	$line = $_;
	chomp($line);
	if($line =~ /^#.*/) {
		next;
	}
	@terms = split(/[\s\t]+/, $line);
	$name = $terms[0];
	$root = $terms[1];
	$swap = $terms[2];
	$mem = $terms[3];
	$macA = $terms[4];
	$macB = $terms[5];
	mkconfig();
}

sub mkconfig {
	$path="$confdir/$name";
	print "Creating image: $name Root: $root Swap: $swap Memory: $mem MacA: $macA MacB: $macB\n";
	open(OUTFILE, ">$path") or die("Error: cannot open output file! \"$path\"\n");
	print OUTFILE contents();
	close(OUTFILE);
}

sub contents {
	$ret="";
	$ret.="#Script Generated Xenconfig for: $name\n";
	$ret.="#root: $root\n";
	$ret.="#swap: $swap\n";
	$ret.="name=\"$name\"\n";
	$ret.="memory=$mem\n";
	$ret.="kernel=\"/boot/vmlinuz-2.6-xenU\"\n";
	$ret.="ramdisk=\"/boot/initrd-2.6-xenU\"\n";
	$ret.="disk=['phy:/dev/disk/by-uuid/$root,sda1,w','phy:/dev/disk/by-uuid/$swap,sda2,w','file:/root/modules.disk,sda3,r']\n";
	$ret.="root=\"/dev/sda1 ro\"\n";
	$ret.="vif = [ 'mac=$macA, bridge=xenbr2','mac=$macB, bridge=xenbr3' ]\n";
}
