#!/usr/bin/perl -w
# Generate the SUSE-prefered patch format with ptools metadata.
# TODO: add an option for inserting SUSE bug# in References.
# 
use strict;
use POSIX qw(strftime);
unshift @INC, "/home/$ENV{'USER'}/bin";
require 'developers.pl';

if ($#ARGV != 3) {
	print STDERR "Usage: p_mod2suse </path/to/suse/kernel> <bugzilla#> <modid> <one-line-summary>\n";
	exit 1;
}
if (!defined($ENV{'WORKAREA'})) {
	print STDERR "Error: WORKAREA must be set to workarea for <modid>\n";
	exit 1;
}
 
my $SUSE_TREE = $ARGV[0];
my $BUGZILLA = $ARGV[1];
my $MODID = $ARGV[2];
my $TREEID = $ARGV[2];
my $MODNUMBER = $ARGV[2];
$MODNUMBER =~ s/(\S+):(\S+):(\S+)/$3/g;

my $KERNEL = 0;
if ( -d "$SUSE_TREE/fs/xfs/linux-2.4" ) {
	$KERNEL = 2.4;
} elsif ( -d "$SUSE_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 = $TREEID . '-' . $MODNUMBER . '-' . $ARGV[3];
my $SUBJECT = $ARGV[3];
$SUBJECT =~ s/-/ /g;

# 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 $DATE = strftime "%a, %b %e %Y %H:%M:%S %z", localtime;
my $AUTHOR = <MODINFO>;
$AUTHOR =~ s/(\w+).*\n/$1/;
$AUTHOR = &developers::developer($AUTHOR);
my $PVNUMBER = <MODINFO>;
$PVNUMBER =~ s/(\d+).*\n/$1/;

# Patch header
print PATCH "Date: ", $DATE, "\n";
print PATCH "From: ", $AUTHOR, "\n";
print PATCH "References: $BUGZILLA, SGI:PV$PVNUMBER\n";
print PATCH "Subject: $SUBJECT\n\n";

# Patch description
undef $/;
my $DESCRIPTION = <MODINFO>;
$/ = "\n";
print PATCH $DESCRIPTION;
close MODINFO;

#
# example of output from p_rdiff
# ===========================================================================
# Index: quota/xfs_dquot.c
# ===========================================================================
# 
# --- a/quota/xfs_dquot.c 2005-08-29 17:31:04.000000000 +1000
# +++ b/quota/xfs_dquot.c 2005-08-29 17:31:04.000000000 +1000
# 
# The mod refers to an xfs-linux tree with xfs at top level.
# Need to add in a/fs/xfs to the front for full tree.
#

# Patch body
open PRDIFF, "p_rdiff -upPM ${MODID} |" or die "rdiff";
$/ = "\n===========================================================================\n";
my $skip = 0;
while (<PRDIFF>) {
	if ($skip == 1) {
		$skip = 0;
	} elsif (($KERNEL == 2.6) &&
		((m/^Index: linux-2.4/) || (m/^Index: Makefile-linux-2.4/))) {
		$skip = 1;
	} elsif (($KERNEL == 2.4) &&
		((m/^Index: linux-2.6/) || (m/^Index: Makefile-linux-2.6/))) {
		$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
close PATCH;
print "Created: $SUMMARY\n";

# Finally, check it applies cleanly to the SUSE tree
my $HERE = $ENV{'PWD'};
chdir $SUSE_TREE;
`patch --force --dry-run -p1 < "$HERE/$SUMMARY"`;
if ($? == 0 ) {
	print "Checked: applied cleanly to $SUSE_TREE\n";
} else {
	print STDERR "WARNING: didn't apply cleanly to $SUSE_TREE\n";
	exit 1;
}
