#!/usr/bin/perl -w
#
# patch-split
# John Simpson <jms1@jms1.net> 2007-08-31
#
# reads a unified diff file
# writes separate .patch files for each file that the diff modifies
#
###############################################################################
#
# Copyright (C) 2007 John Simpson.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

require 5.003 ;
use strict ;

my $oname = "" ;

while ( my $line = <> )
{
	if ( $line =~ /^diff (.*)\n/ )
	{
		my @w = split ( /\s+/ , $1 ) ;
		my $f = $w[-1] ;
		$f =~ s|^.*/|| ;

		if ( $oname )
		{
			close O ;
		}

		$oname = "split-$f.patch" ;
		open ( O , ">$oname" )
			or die "Can\'t create $oname: $!\n" ;
	}

	if ( $oname )
	{
		print O $line ;
	}
}

if ( $oname )
{
	close O ;
}