#!/usr/bin/perl -w
# Convert from a ptools XFS/DMAPI mod to a bitkeeper changeset.
# Adds in two "Signed-off-by:" lines now also, one for the person
# who commits the mod, and one for the person running this script
# (unless they're the same).
# 
use strict;
use Text::Wrap;
use File::Basename;
unshift @INC, "/home/$ENV{'USER'}/bin";
require 'developers.pl';
 
if ($#ARGV != 2) {
	print STDERR "Usage: p_mod2bk </path/to/bk> <modid> <summary>\n";
	exit 1;
}
if (!defined($ENV{'WORKAREA'})) {
	print STDERR "Error: WORKAREA must be set to workarea for <modid>\n";
	exit 1;
}
 
my $BK_TREE = $ARGV[0];
my $MODID = $ARGV[1];
my $TREEID = $ARGV[1];
my $MODNUMBER = $ARGV[1];
$MODNUMBER =~ s/(\S+):(\S+):(\S+)/$3/g;

# 
# Find kernel version (if its a kernel), then create patch file name by
# plucking a prefix from modid - X:dmapi:Y, X:xfs-kern:Y or xfs-cmds:X:Y
# then appending the mod number and the one-line-summary.
# 
my $KERNEL = 0;
if ( -d "$BK_TREE/fs/xfs/linux-2.4" ) {
	$KERNEL = 2.4;
} elsif ( -d "$BK_TREE/fs/xfs/linux-2.6" ) {
	$KERNEL = 2.6;
}
if ($KERNEL == 0) {	# xfs-cmds
	$TREEID =~ s/(\S+):(\S+):(\S+)/$1/g;
	print "Target: non-kernel tree\n";
} else {		# xfs-kern/dmapi
	$TREEID =~ s/(\S+):(\S+):(\S+)/$2/g;
	print "Target: $KERNEL kernel tree\n";
}
my $SUMMARY = 'bk-' . $TREEID . '-' . $MODNUMBER . '-' . $ARGV[2];

my @FILES;
open FILELIST, "p_modinfo -b -m $MODID |" or die "modinfo for affected files";
while (<FILELIST>) {
	if (($KERNEL == 2.4) && (m/^linux-2.6/)) {
		next;
	} elsif (($KERNEL == 2.6) && (m/^linux-2.4/)) {
		next;
	} elsif ((m/^xfsidbg.c/) || (m/^Makefile/)) {
		next;
	}
	push @FILES, $_;
}
close FILELIST;

# Create patch file and input stream from p_modinfo
open PATCH, ">$SUMMARY" or die "cannot open summary patch file\n";
open MODINFO, "p_modinfo -b -H Author,PV,Description $MODID |" or die "modinfo";
my $AUTHOR = <MODINFO>;
$AUTHOR =~ s/(\w+).*\n/$1/;
my $PVNUMBER = <MODINFO>;
$PVNUMBER =~ s/(\d+).*\n/$1/;

my $MODSIGN = &developers::developer($AUTHOR);
my $OWNSIGN = &developers::developer($ENV{'USER'});

# Set some environment variables for bk
$ENV{'BK_USER'} = $AUTHOR;
$ENV{'BK_HOST'} = 'sgi.com';

# Create the changeset description
undef $/;
my $DESCRIPTION = <MODINFO>;
$/ = "\n";
close MODINFO;
$Text::Wrap::columns = 75;
$DESCRIPTION = wrap('[XFS] ', '', split(/\n/, $DESCRIPTION));

# Generate a -p1 patch from the mod, extracting unwanted pieces as we go
open PRDIFF, "p_rdiff -puM ${MODID} |" or die "rdiff";
$/ = "\n===========================================================================\n";
my $skip = 0;
while (<PRDIFF>) {
	if ($skip == 1) {
		$skip = 0;
	} elsif (($KERNEL == 2.4) &&
		((m/^Index: linux-2.6/) || (m/^Index: Makefile-linux-2.6/))) {
		$skip = 1;
	} elsif (($KERNEL == 2.6) &&
		((m/^Index: linux-2.4/) || (m/^Index: Makefile-linux-2.4/))) {
		$skip = 1;
	} elsif ((m/^Index: xfsidbg.c/) || (m/^Index: Makefile/)) {
		$skip = 1;
	} elsif ($TREEID eq 'dmapi') {
		s,^\-\-\- a/(\S+),--- a/fs/dmapi/$1,m;
		s,^\+\+\+ b/(\S+),+++ b/fs/dmapi/$1,m;
		print PATCH;
	} elsif ($TREEID eq 'xfs-kern') {
		s,^\-\-\- a/(\S+),--- a/fs/xfs/$1,m;
		s,^\+\+\+ b/(\S+),+++ b/fs/xfs/$1,m;
		print PATCH;
	} else {
		print PATCH;
	}
}
$/ = "\n";
close PRDIFF;

# The patch is now complete, apply it
close PATCH;
print "Created: $SUMMARY\n";
my $CWD = $ENV{'PWD'};
chdir $BK_TREE;
`patch -p1 -g1 < "$CWD/$SUMMARY"`;
if ($? != 0 ) {
	print STDERR "WARNING: patch didn't apply cleanly, needs manual merge!";
	print STDERR "[ setenv BK_USER $ENV{'BK_USER'} ]\n";
}
 
# Update the affected bitkeeper SCCS files
open CSET, ">SCCS/c.ChangeSet" or die "cannot open ChangeSet SCCS file";
print CSET "$DESCRIPTION\n\n";
print CSET "SGI-PV: $PVNUMBER\n";
print CSET "SGI-Modid: $MODID\n";
print CSET "Signed-off-by: $MODSIGN\n";
print CSET "Signed-off-by: $OWNSIGN\n" unless ($MODSIGN eq $OWNSIGN);
close CSET;
foreach my $f (@FILES) {
	$f = ">$BK_TREE/fs/xfs/" . dirname($f) . '/SCCS/c.' . basename($f);
	open FILE, $f or die "cannot open file $f";
	print FILE $DESCRIPTION;
	close FILE;
}
 
# Finally, run the bitkeeper checkin tool
`bk citool -l`;
