#!/usr/bin/perl -w
#
# Project Builder main application
#
# $Id$
#
# Copyright B. Cornec 2007
# Provided under the GPL v2

# Syntax: see at end

use strict 'vars';
use Getopt::Std;
use Data::Dumper;
use English;
use File::Basename;
use File::Copy;
use Time::localtime qw(localtime);
use POSIX qw(strftime);

# Global variables
use lib qw (lib);
use ProjectBuilder::Distribution qw (pb_distro_init);
use ProjectBuilder::Version qw (pb_version_init);
use ProjectBuilder::Base qw (pb_conf_read pb_conf_get pb_cms_init pb_mkdir_p pb_system pb_rm_rf pb_get_filters pb_filter_file pb_filter_file_pb pb_filter_file_inplace pb_cms_export pb_cms_log pb_cms_isdiff pb_cms_copy pb_cms_checkout pb_get_date pb_log pb_log_init pb_get_pkg);

my %opts;					# CLI Options
my $action;					# action to realize
my $test = "FALSE";
my $option = "";
my @pkgs;
my $pbtag;					# Global Tag variable
my $pbver;					# Global Version variable
my $pbscript;				# Name of the script
my %pbver;					# per package
my %pbtag;					# per package
my $pbrev;					# Global REVISION variable
my @date=pb_get_date();
my $pbdate = strftime("%Y-%m-%d", @date);
my $debug = 0;
my $pbaccount;				# Login to use to connect to the VM
my $pbport;					# Port to use to connect to the VM
my $newver;					# New version to create
my $iso;					# ISO iage for the VM to create
my $LOG = \*STDOUT;

getopts('a:hi:l:m:P:p:qr:s:tvV:',\%opts);

my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
if (defined $opts{'h'}) {
	pb_syntax();
	exit(0);
}
if (defined $opts{'v'}) {
	$debug++;
}
if (defined $opts{'q'}) {
	$debug=-1;
}
if (defined $opts{'l'}) {
	open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
	$LOG = *LOG;
	$debug = 0  if ($debug == -1);
	}
pb_log_init($debug, $LOG);

# Handles test option
if (defined $opts{'t'}) {
	$test = "TRUE";
	# Works only for SVN
	$option = "-r BASE";
}

# Handle root of the project if defined
if (defined $opts{'r'}) {
	$ENV{'PBROOT'} = $opts{'r'};
}
# Handle virtual machines if any
if (defined $opts{'m'}) {
	$ENV{'PBVM'} = $opts{'m'};
}
if (defined $opts{'s'}) {
	$pbscript = $opts{'s'};
}
if (defined $opts{'a'}) {
	$pbaccount = $opts{'a'};
}
if (defined $opts{'P'}) {
	$pbport = $opts{'P'};
}
if (defined $opts{'V'}) {
	$newver = $opts{'V'};
}
if (defined $opts{'i'}) {
	$iso = $opts{'i'};
}

# Get Action
$action = shift @ARGV;
die pb_syntax() if (not defined $action);

my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
my $pbinit = undef;
$pbinit = 1 if ($action =~ /^newproj$/);

# Handles project name if any
# And get global params
if (defined $opts{'p'}) {
	($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) 
	= pb_env_init($opts{'p'},$pbinit);
} else {
	($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) 
	= pb_env_init(undef,$pbinit);
}

pb_log(0,"Project: $ENV{'PBPROJ'}\n");
pb_log(0,"Action: $action\n");

# Keep those project values to store them at the end each time
my $pbprojtag = $ENV{'PBTAG'};
my $pbprojver = $ENV{'PBVER'};

# Act depending on action
if ($action =~ /^cms2build$/) {
	pb_cms2build();
} elsif ($action =~ /^build2pkg$/) {
	pb_build2pkg();
} elsif ($action =~ /^cms2pkg$/) {
	pb_cms2build();
	pb_build2pkg();
} elsif ($action =~ /^build2ssh$/) {
	pb_build2ssh();
} elsif ($action =~ /^cms2ssh$/) {
	pb_cms2build();
	pb_build2ssh();
} elsif ($action =~ /^pkg2ssh$/) {
	pb_pkg2ssh();
} elsif ($action =~ /^build2vm$/) {
	pb_build2vm();
} elsif ($action =~ /^cms2vm$/) {
	pb_cms2build();
	pb_build2vm();
} elsif ($action =~ /^launchvm$/) {
	pb_launchvm($ENV{'PBVM'},0);
} elsif ($action =~ /^script2vm$/) {
	pb_script2vm($pbscript);
} elsif ($action =~ /^newver$/) {
	pb_newver();
} elsif ($action =~ /^newchroot$/) {
	pb_launchchroot($ENV{'PBCHROOT'},1);
} elsif ($action =~ /^newvm$/) {
	pb_launchvm($ENV{'PBVM'},1);
} elsif ($action =~ /^newproj$/) {
	# Nothing to do - already done in pb_env_init
} elsif ($action =~ /^clean$/) {
} else {
	pb_log(0,"\'$action\' is not available\n");
	pb_syntax();
}

sub pb_cms2build {

	my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
	@pkgs = @$ptr;
	my ($scheme,$uri)=pb_cms_init($ENV{'PBPROJ'});

	my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");

	# declare packager for filtering
	my ($tmp) = pb_conf_get("packager");
	my $pbpackager = $tmp->{$ENV{'PBPROJ'}};

	foreach my $pbpkg (@pkgs) {
		$ENV{'PBPKG'} = $pbpkg;
		$ENV{'PBVER'} = $pbprojver;
		$ENV{'PBTAG'} = $pbprojtag;
		if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
			$pbver = $pkgv->{$pbpkg};
			$ENV{'PBVER'} = $pbver;
		} else {
			$pbver = $ENV{'PBVER'};
		}
		if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
			$pbtag = $pkgt->{$pbpkg};
			$ENV{'PBTAG'} = $pbtag;
		} else {
			$pbtag = $ENV{'PBTAG'};
		}

		$pbrev = $ENV{'PBREVISION'};
		pb_log(2,"\n");
		pb_log(2,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
		die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
		# Clean up dest if necessary. The export will recreate it
		my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
		pb_rm_rf($dest) if (-d $dest);

		# Export CMS tree for the concerned package to dest
		# And generate some additional files
		$OUTPUT_AUTOFLUSH=1;

		# computes in which dir we have to work
		my $dir = $defpkgdir->{$pbpkg};
		$dir = $extpkgdir->{$pbpkg} if (not defined $dir);
		pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");

		# If it isn't a flat CMS, then we have the choice to export subdir
		$dir = "$uri/$dir" if ($ENV{'PBREVISION'} ne "flat");
		pb_cms_export($scheme,"$dir",$dest);

		# Get project info on authors and log file
		my $chglog = "$ENV{'PBCONF'}/$pbpkg/pbcl";
		$chglog = "$ENV{'PBCONF'}/pbcl" if (! -f $chglog);
		$chglog = undef if (! -f $chglog);

		my $authors = "$ENV{'PBCONF'}/$pbpkg/pbauthors";
		$authors = "$ENV{'PBCONF'}/pbauthors" if (! -f $authors);
		$authors = "/dev/null" if (! -f $authors);

		# Extract cms log history and store it
		if ((defined $chglog) && (! -f "$dest/NEWS")) {
			pb_log(2,"Generating NEWS file from $chglog\n");
			copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
		}
		pb_cms_log($scheme,$dir,$dest,$chglog,$authors);

		my %build;

		my ($ptr) = pb_conf_get_if("vmlist","chrootlist");
		foreach my $d (split(/,/,$ptr->{$ENV{'PBPROJ'}})) {
			my ($name,$ver,$arch) = split(/-/,$d);
			chomp($arch);
			my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
			pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n");
			pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");

			# Filter build files from the less precise up to the most with overloading
			# Filter all files found, keeping the name, and generating in dest

			# Find all build files first relatively to PBROOT
			# Find also all specific files referenced in the .pb conf file
			my %bfiles = {};
			my %pkgfiles = {};
			pb_log(2,"DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n");
			$build{"$ddir-$dver"} = "yes";

			if (-d "$ENV{'PBROOT'}/$pbpkg/$dtype") {
				pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$dtype",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
			} elsif (-d "$ENV{'PBROOT'}/$pbpkg/$dfam") {
				pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
			} elsif (-d "$ENV{'PBROOT'}/$pbpkg/$ddir") {
				pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
			} elsif (-d "$ENV{'PBROOT'}/$pbpkg/$ddir-$dver") {
				pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
			} else {
				$build{"$ddir-$dver"} = "no";
				next;
			}
			pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");

			# Get all filters to apply
			my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);

			# Apply now all the filters on all the files concerned
			# destination dir depends on the type of file
			if (defined $ptr) {
				foreach my $f (values %bfiles,values %pkgfiles) {
					pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$defpkgdir,$extpkgdir,$pbpackager,$chglog);
				}
			}
		}
		my @found;
		my @notfound;
		foreach my $b (keys %build) {
			push @found,$b if ($build{$b} =~ /yes/);
			push @notfound,$b if ($build{$b} =~ /no/);
		}
		pb_log(0,"Build files generated for ".join(',',@found)."\n");
		pb_log(0,"No Build files found for ".join(',',@notfound)."\n") if (@notfound);
		# Get the generic filter (all.pbf) and 
		# apply those to the non-build files including those
		# generated by pbinit if applicable

		# Get only all.pbf filter
		$ptr = pb_get_filters($pbpkg);

		my $liste ="";
		if (defined $filteredfiles->{$pbpkg}) {
			foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
				pb_filter_file_inplace($ptr,"$dest/$f",$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
				$liste = "$f $liste";
			}
		}
		pb_log(2,"Files ".$liste."have been filtered\n");

		# Prepare the dest directory for archive
		if (-x "$ENV{'PBROOT'}/$pbpkg/pbinit") {
			pb_filter_file("$ENV{'PBROOT'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
			chmod 0755,"$ENV{'PBTMP'}/pbinit";
			pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOT'}/$pbpkg/pbinit");
		}

		# Archive dest dir
		chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
		# Possibility to look at PBSRC to guess more the filename
		pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
		pb_log(2,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");

		# Keep track of what is generated by default
		open(LAST,"> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
		#print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
		# Why not use pbproj ?
		print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOT'}\n";
		close(LAST);

		# Keep track of per package version
		my ($pkg) = pb_conf_read_if("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
		$pkg = { } if (not defined $pkg);
		if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
			$pkg->{$pbpkg} = "$pbver-$pbtag";
		}

		pb_log(2,"DEBUG pkg: ".Dumper($pkg)."\n");
		open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
		foreach my $p (keys %$pkg) {
			print PKG "pbpkg $p = $pkg->{$p}\n";
		}
		close(PKG);

		# Final cleanup
		pb_rm_rf($dest) if (-d $dest);
	}
}

sub pb_build2pkg {

	# Get list of packages to build
	my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
	@pkgs = @$ptr;

	# Get the running distro to build on
	my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
	pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");

	# Get content saved in cms2build
	my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
	$pkg = { } if (not defined $pkg);

	# declare packager
	my ($tmp) = pb_conf_get("packager");
	my $pbpackager = $tmp->{$ENV{'PBPROJ'}};

	chdir "$ENV{'PBBUILDDIR'}";
	my $made = ""; # pkgs made during build
	foreach my $pbpkg (@pkgs) {
		my $vertag = $pkg->{$pbpkg};
		# get the version of the current package - maybe different
		($pbver,$pbtag) = split(/-/,$vertag);

		my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
		pb_log(2,"Source file: $src\n");

		pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
		if ($dtype eq "rpm") {
			foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
				if (! -d "$ENV{'PBBUILDDIR'}/$d") {
				pb_mkdir_p("$ENV{'PBBUILDDIR'}/$d") || die "Please ensure that you can write into $ENV{'PBBUILDDIR'} to create $d\nchown the $ENV{'PBBUILDDIR'} directory to your uid";
				}
			}

			# Remove in case a previous link/file was there
			unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
			symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
			# We need to first extract the spec file
			my @specfile;
			@specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");

			pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
			# set LANGUAGE to check for correct log messages
			$ENV{'LANGUAGE'}="C";
			foreach my $f (@specfile) {
				if ($f =~ /\.spec$/) {
					pb_system("rpmbuild --define \'packager $pbpackager\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
					last;
				}
			}
			$made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
			if (-f "/usr/bin/rpmlint") {
				pb_system("rpmlint $made","Checking validity of rpms with rpmlint");
			}
		} elsif ($dtype eq "deb") {
			chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
			pb_system("tar xfz $src","Extracting sources");

			chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
			pb_rm_rf("debian");
			symlink "pbconf/$ddir-$dver","debian" || die "Unable to symlink to pbconf/$ddir-$dver";
			chmod 0755,"debian/rules";
			pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package");
			$made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
			if (-f "/usr/bin/lintian") {
				pb_system("lintian $made","Checking validity of debs with lintian");
			}
		} elsif ($dtype eq "ebuild") {
			my @ebuildfile;
			# For gentoo we need to take pb as subsystem name
			# We put every apps here under sys-apps. hope it's correct
			# We use pb's home dir in order o have a single OVERLAY line
			my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
			pb_mkdir_p($tmpd) if (! -d "$tmpd");
			pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");

			# We need to first extract the ebuild file 
			@ebuildfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$tmpd");

			# Prepare the build env for gentoo
			my $found = 0;
			my $pbbd = $ENV{'HOME'};
			$pbbd =~ s|/|\\/|g;
			if (-r "/etc/make.conf") {
				open(MAKE,"/etc/make.conf");
				while (<MAKE>) {
					$found = 1 if (/$pbbd\/portage/);
				}
				close(MAKE);
			}
			if ($found == 0) {
				pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
			}
			#$found = 0;
			#if (-r "/etc/portage/package.keywords") {
			#open(KEYW,"/etc/portage/package.keywords");
			#while (<KEYW>) {
			#$found = 1 if (/portage\/pb/);
			#}
			#close(KEYW);
			#}
			#if ($found == 0) {
			#pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
			#}

			# Build
			foreach my $f (@ebuildfile) {
				if ($f =~ /\.ebuild$/) {
					move($f,"$tmpd/$pbpkg-$pbver.ebuild");
					pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package");
					# Now move it where pb expects it
					pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
					move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
				}
			}

			$made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
		} elsif ($dtype eq "slackware") {
			$made="$made build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
			pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
		} else {
			die "Unknown dtype format $dtype";
		}
	}
	# Keep track of what is generated so that we can get them back from VMs
	open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
	print KEEP "$made\n";
	close(KEEP);
}

sub pb_build2ssh {
	pb_send2ssh("Sources");
}

sub pb_pkg2ssh {
	pb_send2ssh("Packages");
}

# By default deliver to the the public site hosting the 
# ftp structure (or whatever) or a VM
sub pb_send2ssh {

	my $cmt = shift;
	my $vm = shift || undef;
	my $vmexist = shift || 0;			# 0 is FALSE
	my $vmpid = shift || 0;				# 0 is FALSE
	my $host = shift || "sshhost";
	my $login = shift || "sshlogin";
	my $dir = shift || "sshdir";
	my $port = shift || "sshport";
	my $tmout = shift || "vmtmout";
	my $cmd = "";

	# Get list of packages to build
	my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
	@pkgs = @$ptr;

	# Get the running distro to consider
	my ($odir,$over) = (undef, undef);
	if (defined $vm) {
		($odir,$over) = split(/_/,$vm);
	}
	my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
	pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");

	# Get content saved in cms2build
	my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
	$pkg = { } if (not defined $pkg);

	my $src = "";
	chdir "$ENV{'PBBUILDDIR'}";
	foreach my $pbpkg (@pkgs) {
		my $vertag = $pkg->{$pbpkg};
		# get the version of the current package - maybe different
		($pbver,$pbtag) = split(/-/,$vertag);

		if (($cmt eq "Sources") || ($cmt eq "VMs")) {
			$src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
			if ($cmd eq "") {
				$cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
			} else {
				$cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
			}
		}
	}
	if ($cmt eq "VMs") {
		$src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
	} elsif ($cmt eq "Script") {
		$src="$src $ENV{'PBDESTDIR'}/pbscript";
	} elsif ($cmt eq "Packages") {
		# Get package list from file made during build2pkg
		open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
		$src = <KEEP>;
		chomp($src);
		close(KEEP);
		if ($dtype eq "rpm") {
			# Also make a pbscript to generate yum/urpmi bases
			# $src = "$src $ENV{'PBDESTDIR'}/pbscript"
		} elsif ($dtype eq "deb") {
			# Also make a pbscript to generate apt bases
			# $src = "$src $ENV{'PBDESTDIR'}/pbscript"
		}
	}
	# Remove potential leading spaces (cause pb with basename)
	$src =~ s/^ *//;
	my $basesrc = "";
	foreach my $i (split(/ +/,$src)) {
		$basesrc .= " ".basename($i);
	}

	pb_log(2,"Sources handled ($cmt): $src\n");
	my ($sshhost,$sshlogin,$sshdir,$sshport,$vmtmout,$testver) = pb_conf_get($host,$login,$dir,$port,$tmout,"testver");
	my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
	# Overwrite account value if passed as parameter
	$mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
	my $tdir;
	my $bdir;
	if (($cmt eq "Sources") || ($cmt eq "Script")) {
		$tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
	} elsif ($cmt eq "VMs") {
		$tdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/delivery";
		$bdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/build";
		# Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
		$bdir =~ s|\$ENV.+\}/||;
	} elsif ($cmt eq "Packages") {
		$tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
		if ((defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} eq "true")) {
			# This is a test pkg => target dir is under test
			$tdir .= "/test";
		}
	} else {
		return;
	}
	my $nport = $sshport->{$ENV{'PBPROJ'}};
	$nport = "$pbport" if (defined $pbport);

	# Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
	$tdir =~ s|\$ENV.+\}/||;

	my $tm = $vmtmout->{$ENV{'PBPROJ'}};
	pb_system("ssh -q -p $nport $mac \"mkdir -p $tdir ; cd $tdir ; echo \'for i in $basesrc; do if [ -f \$i ]; then rm -f \$i; fi; done\ ; $cmd' | bash\"","Preparing $tdir on $mac");
	pb_system("cd $ENV{'PBBUILDDIR'} ; scp -p -P $nport $src $mac:$tdir 2> /dev/null","$cmt delivery in $tdir on $mac");
	pb_system("ssh -q -p $nport $mac \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $mac  if needed");
	if ($cmt eq "VMs") {
		# Get back info on pkg produced, compute their name and get them from the VM
		pb_system("scp -p -P $nport $mac:$bdir/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $bdir on $mac");
		open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
		my $src = <KEEP>;
		chomp($src);
		close(KEEP);
		$src =~ s/^ *//;
		pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
		# Change pgben to make the next send2ssh happy
		my $made = "";
		open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
		foreach my $p (split(/ +/,$src)) {
			my $j = basename($p);
			pb_system("scp -p -P $nport $mac:\'$bdir/$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $bdir from $mac");
			$made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
		}
		print KEEP "$made\n";
		close(KEEP);
		pb_system("ssh -q -p $nport $mac \"rm -rf $tdir $bdir\"","VM cleanup on $mac");
		pb_send2ssh("Packages","$odir"."_"."$over");
		if (! $vmexist) {
			pb_system("ssh -q -p $nport $mac \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $vm halt (pid $vmpid)");
		}
		pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
	}
}

sub pb_script2vm {
	my $pbscript=shift;

	# Prepare the script to be executed on the VM
	# in $ENV{'PBDESTDIR'}/pbscript
	if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
		copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
		chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
	}

	my ($vm,$all) = pb_get_vm();

	foreach my $v (@$vm) {
		# Launch the VM
		my ($vmexist,$vmpid) = pb_launchvm($v,0);

		# Skip that VM if it something went wrong
		next if (($vmpid == 0) && ($vmexist ==0));

		# Gather all required files to send them to the VM 
		# and launch the build thourgh pbscript
		pb_send2ssh("Script","$v",$vmexist,$vmpid,"vmhost","vmlogin","$ENV{'PBDESTDIR'}/pbrc","vmport","vmtmout");

	}
}

sub pb_launchchroot {
	# Virtual env.
	my $ve = shift;
	my $create = shift || 0;		# By default do not create a chroot

	#die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
	#die "No VM defined, unable to launch" if (not defined $ve);

	# Keep only the first chroot in case many were given
	$ve =~ s/,.*//;

	# Launch the chroot
	my ($ptr,$veopt,$veport,$vepath,$vetmout,$vesize) = pb_conf_get("vetype","veopt","veport","vepath","vetmout","vesize");
	my $vetype = $ptr->{$ENV{'PBPROJ'}};
	if (not defined $ENV{'PBCHROOTOPT'}) {
		$ENV{'PBCHROOTOPT'} = "";
	}
	if (defined $veopt->{$ENV{'PBPROJ'}}) {
		$ENV{'PBVMOPT'} .= " $veopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $veopt->{$ENV{'PBPROJ'}}/);
	}
	my $nport = $veport->{$ENV{'PBPROJ'}};
	$nport = "$pbport" if (defined $pbport);

	my $cmd;
	my $vecmd;		# has to be used for pb_check_ps
	my $vem;		# has to be used for pb_check_ps
	if ($vetype eq "qemu") {
		my $arch = `uname -m`;
		chomp($arch);
		my $qemucmd32;
		my $qemucmd64;
		if ($arch eq "x86_64") {
			$qemucmd32 = "/usr/bin/qemu-system-i386";
			$qemucmd64 = "/usr/bin/qemu";
		} else {
			$qemucmd32 = "/usr/bin/qemu";
			$qemucmd64 = "/usr/bin/qemu-system-x86_64";
		}
		if ($ve =~ /_64/) {
			$vecmd = "$qemucmd64 -no-kqemu";
		} else {
			$vecmd = "$qemucmd32";
		}
		$vem = "$vepath->{$ENV{'PBPROJ'}}/$ve.qemu";
		if ($create != 0) {
			$ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
		}
		$cmd = "$vecmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vem"
	} elsif ($vetype eq "xen") {
	} elsif ($vetype eq "vmware") {
	} else {
		die "VM of type $vetype not supported. Report to the dev team";
	}
	my ($tmpcmd,$void) = split(/ +/,$cmd);
	my $veexist = pb_check_ps($tmpcmd,$vem);
	my $vepid = 0;
	if (! $veexist) {
		if ($create != 0) {
			pb_system("/usr/bin/qemu-img create -f qcow2 $vem $vesize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
		}
		if (! -f "$vem") {
			pb_log(0,"Unable to find VM $vem\n");
		} else {
			pb_system("$cmd &","Launching the VM $vem");
			pb_system("sleep $vetmout->{$ENV{'PBPROJ'}}","Waiting for VM $ve to come up");
			$vepid = pb_check_ps($tmpcmd,$vem);
		}
	} else {
		pb_log(0,"Found an existing VM $vem (pid $veexist)\n");
	}
	return($veexist,$vepid);
}

sub pb_launchvm {
	my $vm = shift;
	my $create = shift || 0;		# By default do not create a VM

	die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
	die "No VM defined, unable to launch" if (not defined $vm);
	# Keep only the first VM in case many were given
	$vm =~ s/,.*//;

	# Launch the VMs
	my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize");
	my $vmtype = $ptr->{$ENV{'PBPROJ'}};
	if (not defined $ENV{'PBVMOPT'}) {
		$ENV{'PBVMOPT'} = "";
	}
	if (defined $vmopt->{$ENV{'PBPROJ'}}) {
		$ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
	}
	my $nport = $vmport->{$ENV{'PBPROJ'}};
	$nport = "$pbport" if (defined $pbport);

	my $cmd;
	my $vmcmd;		# has to be used for pb_check_ps
	my $vmm;		# has to be used for pb_check_ps
	if ($vmtype eq "qemu") {
		my $arch = `uname -m`;
		chomp($arch);
		my $qemucmd32;
		my $qemucmd64;
		if ($arch eq "x86_64") {
			$qemucmd32 = "/usr/bin/qemu-system-i386";
			$qemucmd64 = "/usr/bin/qemu";
		} else {
			$qemucmd32 = "/usr/bin/qemu";
			$qemucmd64 = "/usr/bin/qemu-system-x86_64";
		}
		if ($vm =~ /_64/) {
			$vmcmd = "$qemucmd64 -no-kqemu";
		} else {
			$vmcmd = "$qemucmd32";
		}
		$vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu";
		if ($create != 0) {
			$ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
		}
		$cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
	} elsif ($vmtype eq "xen") {
	} elsif ($vmtype eq "vmware") {
	} else {
		die "VM of type $vmtype not supported. Report to the dev team";
	}
	my ($tmpcmd,$void) = split(/ +/,$cmd);
	my $vmexist = pb_check_ps($tmpcmd,$vmm);
	my $vmpid = 0;
	if (! $vmexist) {
		if ($create != 0) {
			if (($vmtype eq "qemu") || ($vmtype eq "xen")) {
				pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
			} elsif ($vmtype eq "vmware") {
			} else {
			}
		}
		if (! -f "$vmm") {
			pb_log(0,"Unable to find VM $vmm\n");
		} else {
			pb_system("$cmd &","Launching the VM $vmm");
			pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $vm to come up");
			$vmpid = pb_check_ps($tmpcmd,$vmm);
		}
	} else {
		pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
	}
	return($vmexist,$vmpid);
}

sub pb_build2vm {
	# Prepare the script to be executed on the VM
	# in $ENV{'PBDESTDIR'}/pbscript
	my ($ntp) = pb_conf_get("vmntp");
	my $vmntp = $ntp->{$ENV{'PBPROJ'}};
	open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
	print SCRIPT "#!/bin/bash\n";
	print SCRIPT "echo ... Execution needed\n";
	print SCRIPT "# This is in directory delivery\n";
	print SCRIPT "# Setup the variables required for building\n";
	print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
	print SCRIPT "# Preparation for pb\n";
	print SCRIPT "mkdir -p ../pbconf\n";
	print SCRIPT "mv $ENV{'PBPROJ'}.pb ../pbconf\n";
	print SCRIPT "mv .pbrc \$HOME\n";
	print SCRIPT "cd ..\n";
	# Force new date to be in the future compared to the date of the tar file by adding 1 minute
	my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
	$date[1]++;
	my $upddate = strftime("%m%d%H%M%Y", @date);
	print SCRIPT "echo Setting up date on $vmntp...\n";
	# Or use ntpdate if available
	print SCRIPT "sudo date $upddate\n";
	print SCRIPT "export PBROOT=\`pwd\`\n";
	print SCRIPT "# Build\n";
	my $p = "";
	$p = join(' ',@ARGV) if (defined $ARGV[0]);
	print SCRIPT "echo Building packages on VM...\n";
	print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
	close(SCRIPT);
	chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";

	my ($vm,$all) = pb_get_vm();

	# Send tar files when we do a global generation
	pb_build2ssh() if ($all == 1);

	foreach my $v (@$vm) {
		# Launch the VM
		my ($vmexist,$vmpid) = pb_launchvm($v,0);

		# Skip that VM if it something went wrong
		next if (($vmpid == 0) && ($vmexist == 0));

		# Gather all required files to send them to the VM 
		# and launch the build thourgh pbscript
		pb_send2ssh("VMs","$v",$vmexist,$vmpid,"vmhost","vmlogin","$ENV{'PBDESTDIR'}/pbrc","vmport","vmtmout");
	}
}


sub pb_newver {

	die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
	my $scheme=pb_cms_init($ENV{'PBPROJ'});
	if ($scheme ne "svn") {
		die "Only SVN is supported at the moment";
	}
	my $res = pb_cms_isdiff($scheme);
	die "You need to have no differences before creating a new version" if ($res != 0);
	my $cmsurl = pb_cms_getinfo($scheme,$ENV{'PBROOT'},"URL:");
	my $newurl = dirname($cmsurl)."/$newver";
	pb_cms_copy($scheme,$cmsurl,$newurl);
	pb_cms_checkout($scheme,$newurl,"$ENV{'PBROOT'}/../$newver");
	my $oldver=basename($cmsurl);
	open(FILE,"$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb";
	open(OUT,"> $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new";
	while(<FILE>) {
		s/projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
		print OUT $_;
	}
	close(FILE);
	close(OUT);
	rename("$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb");
	pb_cms_checkin($scheme,"$ENV{'PBROOT'}/../$newver");
}

#
# Return the list of VMs we are working on
# $all is a flag to know if we return all of them 
# or only some (if all we publish also tar files in addition to pkgs
#
sub pb_get_vm {

my @vm;
my $all = 0;

# Get VM list
if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) {
	my ($ptr) = pb_conf_get("vmlist");
	my $ptr2 = $ptr->{$ENV{'PBPROJ'}};
	$ptr2 =~ s/,/_64,/g;
	$ptr2 =~ s/$/_64/g;
	$ENV{'PBVM'} = "$ptr->{$ENV{'PBPROJ'}},$ptr2";
	$all = 1;
}
pb_log(2,"VMs: $ENV{'PBVM'}\n");
@vm = split(/,/,$ENV{'PBVM'});
return(\@vm,$all);
}

# Returns the pid of a running VM command using a specific VM file
sub pb_check_ps {
	my $vmcmd = shift;
	my $vmm = shift;
	my $vmexist = 0;		# FALSE by default

	open(PS, "ps auxhww|") || die "Unable to call ps";
	while (<PS>) {
		next if (! /$vmcmd/);
		next if (! /$vmm/);
		my ($void1, $void2);
		($void1, $vmexist, $void2) = split(/ +/);
		last;
	}
	return($vmexist);
}


sub pb_extract_build_files {

my $src=shift;
my $dir=shift;
my $ddir=shift;
my @files;

if ($src =~ /tar\.gz$/) {
	pb_system("tar xfpz $src $dir","Extracting build files");
} elsif ($src =~ /tar\.bz2$/) {
	pb_system("tar xfpj $src $dir","Extracting build files");
} else {
	die "Unknown compression algorithm for $src";
}
opendir(DIR,"$dir") || die "Unable to open directory $dir";
foreach my $f (readdir(DIR)) {
	next if ($f =~ /^\./);
	move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
	pb_log(2,"mv $dir/$f $ddir\n");
	push @files,"$ddir/$f";
}
closedir(DIR);
# Not enough but still a first cleanup
pb_rm_rf("$dir");
return(@files);
}

sub pb_list_bfiles {

my $dir = shift;
my $pbpkg = shift;
my $bfiles = shift;
my $pkgfiles = shift;
my $supfiles = shift;

opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
foreach my $f (readdir(BDIR)) {
	next if ($f =~ /^\./);
	$bfiles->{$f} = "$dir/$f";
	$bfiles->{$f} =~ s~$ENV{'PBROOT'}~~;
	if (defined $supfiles->{$pbpkg}) {
		$pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
	}
}
closedir(BDIR);
}

sub pb_syntax {

	print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
	print "\n";
	print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account -P port] -m \"mach-1[,...]\"][-i iso] <action> [<pkg1> ...]\n";
	print "\n";
	print "-h: This help file\n";
	print "-q: Quiet mode\n";
	print "-t: Test mode (not done yet)\n";
	print "-v: Verbose mode\n";
	print "\n";
	print "-m machine: Name of the Virtual Machines (VM) you want\n";
	print "            to build on (coma separated). All if none precised\n";
	print "            (or use the env variable PBVM)       \n";
	print "\n";
	print "-s script:  Name of the script you want\n";
	print "            to execute on the related VMs.\n";
	print "\n";
	print "-i iso:     Name of the ISO image of the distribution you want\n";
	print "            to install on the related VMs.\n";
	print "\n";
	print "-a account: Name of the account to use\n";
	print "            to connect on the related VMs.\n";
	print "\n";
	print "-P port:    Port number to use\n";
	print "            to connect on the related VMs.\n";
	print "\n";
	print "-p project: Name of the project you're working on\n";
	print "            (or use the env variable PBPROJ)     \n";
	print "\n";
	print "-r pbroot:  Path Name of project under the CMS \n";
	print "            (or use the env variable PBROOT)   \n";
	print "\n";
	print "-V newver:  New version of the project to create\n";
	print "            from the current one.              \n";
	print "\n";
	print "<action> can be:\n";
	print "\n";
	print "\tcms2build:    Create tar files for the project under your CMS\n";
	print "\t              CMS supported are SVN and CVS\n";
	print "\t              parameters are packages to build\n";
	print "\t              if not using default list\n";
	print "\n";
	print "\tbuild2pkg:    Create packages for your running distribution  \n";
	print "\n";
	print "\tcms2pkg:      cms2build + build2pkg\n";
	print "\n";
	print "\tbuild2ssh:    Send the tar files to a SSH host               \n";
	print "\n";
	print "\tcms2ssh:      cms2build + build2ssh\n";
	print "\n";
	print "\tpkg2ssh:      Send the packages built to a SSH host          \n";
	print "\n";
	print "\tbuild2vm:     Create packages in VMs, launching them if needed\n";
	print "\t              and send those packages to a SSH host once built\n";
	print "\t              VM type supported are QEMU            \n";
	print "\n";
	print "\tbuild2chroot: Create packages in a chroot, creating it if needed\n";
	print "\t              and send those packages to a SSH host once built\n";
	print "\n";
	print "\tcms2vm:       cms2build + build2vm\n";
	print "\n";
	print "\tlaunchvm:     Launch one virtual machine\n";
	print "\n";
	print "\tscript2vm:    Launch one virtual machine if needed        \n";
	print "\t              and executes a script on it                 \n";
	print "\n";
	print "\tnewvm:        Create a new virtual machine\n";
	print "\n";
	print "\tnewchroot:    Create a new chroot environment\n";
	print "\n";
	print "\tnewver:       Create a new version of the project derived \n";
	print "\t              from the current one                        \n";
	print "\n";
	print "\tnewproj:      Create a new project and a template set of  \n";
	print "\t              configuration files under pbconf            \n";
	print "\n";
}
