#!/usr/bin/perl
# Copyright mbligh@aracnet.com, Dec 2003

$kernel = shift(@ARGV);

$kernel =~ /^(2.[56].\d+(-test\d+|-rc\d+)?)-mjb(\d+)$/ || die "Kernel invalid: $kernel";
$base = $1; $mjb = $3;

$basedir = $ENV{'HOME'} . "/linux";
$viewdir = "$basedir/views/$kernel";
$patchdir = "$basedir/patches/$kernel";
$user = 'mbligh@master.kernel.org';
$targetdest = 'ftp';
$dest = "$user:$targetdest";

print "Releasing ${base}-mjb${mjb} from $patchdir\n";

########## init
print "\n";

print "chdir $viewdir\n";
chdir $viewdir || die "Can't switch to $viewdir";

printsys ("rm $patchdir/[0-9]*");

printsys ("rm $patchdir/patch-*");

########## the main patch
print "\n";

$lastview = `ls -d [0-9]* | tail -1`;
$lastview =~ s/\n//g;

printsys ("diffview 000-virgin $lastview patch-$kernel");
printsys ("gzip $patchdir/patch-$kernel");
printsys ("/usr/bin/scp $patchdir/patch-$kernel.gz $dest/$base/.patch-$kernel.gz");
printsys ("/usr/bin/ssh $user mv $targetdest/$base/.patch-$kernel.gz $targetdest/$base/patch-$kernel.gz");

########## the incremental patch
print "\n";

if ($mjb > 1) {
	$prevmjb = $base . "-mjb" . ($mjb-1);
	$incpatch = "patch-${prevmjb}-mjb$mjb";
	print "Attempting to create incremental patch $incpatch ... ";
	$oldlastview = `cd ../$prevmjb; ls -d [0-9]* | tail -1`;
	$oldlastview = "../$prevmjb/$oldlastview";
	if (-e $oldlastview) {
		print "\n";

		printsys("diffview $oldlastview $lastview $incpatch");

		printsys("gzip $patchdir/$incpatch");
		
		printsys("/usr/bin/scp $patchdir/$incpatch.gz $dest/$base/");

	} else {
		print "failed.\n";
	}
}

########## the individual patches
print "\n";

printsys ("creatediffs");

printsys ("/usr/bin/ssh $user mkdir -p $targetdest/patches/$base/$kernel");
printsys ("/usr/bin/scp $patchdir/notes $patchdir/[0-9]* $dest/patches/$base/$kernel/");

sub printsys
{
	my $command = shift(@_);

	print $command . "\n";
	system $command;
}