#!/usr/bin/env perl

$default_mws = 'SRC680';

sub usage()
{
    print STDERR "cws-commit-patch - automate the creation of CWSes
Usage: cws-commit-patch ...params... patch1.diff patch2.diff ...

  -i, --iz        IZ number (XXXX or iXXXX)
  -c, --cws       CWS name (to be created or existing
  -d              cvs root
  -h, --help      Help
  -m, --milestone Milestone to base the CWS (defaults to current)
  -w, --mws       Master Workspace name (defaults to $default_mws)
                  Use with care! Almost all CWSs are created for $default_mws.
                  They are cloned for branches by need.
  -s, --summary   Summary describing the change (for cvs commit)
  
Note: You should use the up-stream version available from m196 instead.\n";
    exit 1;
}

sub cws_create($$$)
{
    my ( $mws, $cws, $milestone ) = @_;
    system( "echo '===== cws_create =====' >> .log" );

    my $tmp = `mktemp -d /tmp/cws-$cws.XXXXXX`;
    chomp( $tmp );
    print "Temp dir:\t$tmp\n";

    chdir( $tmp );
    $ENV{SRC_ROOT} = $tmp;

    my $state = `cwsquery -m $mws -c $cws state 2>> .log`;
    $state =~ s/\n//g;
    if ( $state ne "" && $state ne "planned" ) {
        print "CWS:\t\t'$cws' already exists, its state is '$state'\n";
    }
    else {
        print "CWS:\t\tcreating '$cws'\n";

        if ( system( "cwscreate -f $mws $milestone $cws 2>> .log" ) != 0 ) {
            print STDERR "Unable to execute cwscreate.\n";
            exit 1;
        }
        print "CWS:\t\t'$cws' created\n";
    }
}

sub cws_modules($$)
{
    my ( $mws, $cws ) = @_;

    my $mod_str = `cwsquery -m $mws -c $cws modules 2>> .log`;
    return split( '\n', $mod_str );
}

sub cws_add_modules($$$@)
{
    my ( $mws, $cws, $milestone, @patches ) = @_;
    system( "echo '===== cws_add_modules =====' >> .log" );

    my %modules = ();

    for $patch ( @patches ) {
        if ( ! open( $PATCH, "$patch" ) ) {
            print STDERR "Unable to open patch '$patch'.";
            exit 1;
        }
        while (<$PATCH>) {
            if ( /^\+\+\+ ([^\/]*)/ ) {
                if ( $1 ne "" ) {
                    $modules{$1} = "add";
                }
                else {
                    print STDERR "Unable to guess module name ($1) from $patch.\n";
                    close( $PATCH );
                    exit 1;
                }
            }
        }
        close( $PATCH );
    }

    my @cws_modules = cws_modules( $mws, $cws );
    for $cws_mod ( @cws_modules ) {
        if ( defined( $modules{$cws_mod} ) ) {
            $modules{$cws_mod} = "checkout";
        }
    }

    while ( ( $module, $what ) = each( %modules ) ) {
        if ( $what eq "add" ) {
            print "Tagging:\tModule '$module'... Be patient, please ;)\n";
            if ( system( "cvs checkout -r " . uc($mws) . "_$milestone $module >> .log 2>&1" ) != 0 ) {
                print STDERR "Unable to execute cvs checkout.\n";
                exit 1;
            }
            if ( system( "cwsadd -a -f $module >> .log 2>&1" ) != 0 ) {
                print STDERR "Unable to execute cwsadd.\n";
                exit 1;
            }
            print "Module:\t\t'$module' added to $cws\n";
        }
        elsif ( $what eq "checkout" ) {
            print "Checking out:\tModule '$module'\n";
            if ( system( "cvs checkout -r cws_" . lc($mws) . "_$cws $module >> .log 2>&1" ) != 0 ) {
                print STDERR "Unable to execute cvs checkout.\n";
                exit 1;
            }
            print "Module:\t\t'$module' already exists in $cws, checked out\n";
        }
    }
}

sub cws_add_task($)
{
    my ( $iz ) = @_;
    system( "echo '===== cws_add_task =====' >> .log" );

    if ( system( "cwsaddtask i$iz 2>> .log" ) != 0 ) {
        print STDERR "Unable to add task number $iz.\n";
        exit 1;
    }

    print "IZ:\t\tSet to '$iz'\n";
}

sub apply_patches(@)
{
    my ( @patches ) = @_;
    system( "echo '===== apply_patches =====' >> .log" );

    for $patch ( @patches ) {
        print "Patch:\t\tApplying '$patch'\n";
        if ( system( "unset POSIXLY_CORRECT ; patch -p0 -i $patch" ) != 0 ) {
            print STDERR "Unable to apply patch $patch.\n";
            exit 1;
        }
        print "Patch:\t\t'$patch' OK\n";
    }
}

sub cvs_add_files(@)
{
    my ( @patches ) = @_;
    system( "echo '===== cvs_add_files =====' >> .log" );

    my %modules = ();

    for my $patch ( @patches ) {
        if ( ! open( $PATCH, "$patch" ) ) {
            print STDERR "Unable to open patch '$patch'.";
            exit 1;
        }
	my $file_add = "";
        while (<$PATCH>) {
            if ( /^\+\+\+ ([^\s]*)/ ) {
                if ( $1 ne "" ) {
		    $file_add = $1;
                }
            }
            elsif ( /^\@\@ -0,0 / && $file_add ne "" ) {
		my @to_add = split( /\//, $file_add );
		my $current = "";
		for my $add ( @to_add ) {
		    $current .= "/" unless ( $current eq "" );
		    my $where_add_dir = $current;
		    $current .= $add;

		    if ( ( -d "$current" && ! -d "$current/CVS" ) || ( -f "$current" ) ) {
			if ( system( "cd $where_add_dir && cvs add $add" ) != 0 ) {
			    print STDERR "Unable to exec 'cd $where_add_dir && cvs add $add'.\n";
			    close( $PATCH );
			    exit 1;
			}
		    }
		}
	    }
        }
        close( $PATCH );
    }
}

sub cws_commit($$$$$@)
{
    my ( $mws, $cws, $iz, $summary, $milestone, @patches ) = @_;

    cws_create( $mws, $cws, $milestone );
    cws_add_modules( $mws, $cws, $milestone, @patches );
    cws_add_task( $iz );

    apply_patches( @patches );

    cvs_add_files( @patches );

    my @cws_modules = cws_modules( $mws, $cws );
    my $commit_message = "#i$iz#\n" . $summary;
    $commit_message =~ s/'/'\\''/g;

    my $commit = "cvs commit -m '$commit_message' ";
    for $module ( @cws_modules ) {
        if ( -d $module ) {
            $commit .= $module . " ";
        }
    }
    print "CVS:\t\t$commit\n";
    if ( system( $commit ) != 0 ) {
        print STDERR "cvs commit failed.\n";
        exit 1;
    }
}

#
# main()
#
if ( !defined( $ENV{'SOLARENV'} ) || $ENV{'SOLARENV'} eq '' ) {
    my $my_path = $0;
    $my_path =~ s#/[^/]*$##;
    my $build_dir = `. $my_path/setup > /dev/null 2>&1 ; echo \$OOBUILDDIR`;
    if ( $build_dir eq "" ) {
        print STDERR "Unable to find build dir, check OOBUILDDIR in bin/setup.\n";
        exit 1;
    }
    $build_dir =~ s/\n//;
    if ( ! -f "$build_dir/LinuxIntelEnv.Set.sh" ) {
        print STDERR "Unable to find '$build_dir/LinuxIntelEnv.Set.sh'.\n";
        exit 1;
    }
    open( $VARS, "bash -c '. $build_dir/LinuxIntelEnv.Set.sh ; set'|");
    while ( <$VARS> ) {
        /([^=]*)=(.*)/ || next;
        $ENV{$1} = $2 unless "$1" eq "SHELLOPTS";
    }
    close( $VARS );
}

my $iz = "";
my $cws = "";
my $milestone = "";
my $summary = "";
my @patches = ();

( my $pwd = `pwd` ) =~ s/\n//;

while (@ARGV) {
    $opt = shift @ARGV;

    if ( $opt eq "-i" || $opt eq "--iz" ) {
        $iz = shift @ARGV;
    }
    elsif ( $opt eq "-c" || $opt eq "--cws" ) {
        $cws = shift @ARGV;
    }
    elsif ( $opt eq "-d" ) {
        $ENV{'CVSROOT'} = shift @ARGV;
    }
    elsif ( $opt eq "-h" || $opt eq "--help" ) {
        usage();
    }
    elsif ( $opt eq "-s" || $opt eq "--summary" ) {
        $summary = shift @ARGV;
    }
    elsif ( $opt eq "-m" || $opt eq "--milestone" ) {
        $milestone = shift @ARGV;
	if (! ( $milestone =~ m/^m/ ) ) {
	    $milestone = "m$milestone";
	    print "Re-writing milestone to $milestone\n";
	}
    }
    elsif ( $opt eq "-w" || $opt eq "--mws" ) {
        $mws = shift @ARGV;
    }
    else {
        my $patch = $opt;
        if ( $patch =~ /^[^\/]/ ) {
            $patch = "$pwd/$opt";
        }

        if ( -f $patch ) {
            push @patches, $patch;
        }
        else {
            print STDERR "Unable to find patch '$patch'.\n";
            exit 1;
        }
    }
}

if ( !@patches ) {
    print STDERR "No patches to apply.\n";
    exit 1;
}
if ( !defined( $ENV{'CVSROOT'} ) || $ENV{'CVSROOT'} eq "" ) {
    print STDERR "Please specify cvs root (using -d or environment variable).\n";
    exit 1;
}
if ( !defined( $iz ) || !( $iz =~ /^i?[0-9]+$/ ) ) {
    print STDERR "Please specify IZ number as XXXX, or iXXXX.\n";
    exit 1;
}
if ( !defined( $cws ) || $cws eq "" ) {
    print STDERR "Please specify CWS.\n";
    exit 1;
}
if ( !defined( $summary ) || $summary eq "" ) {
    print STDERR "Please provide summary.\n";
    exit 1;
}
if ( !defined( $mws ) || $mws eq "" ) {
    $mws = $default_mws;
    print "MWS:\tnot specified, assuming '$mws'\n";
}
if ( !defined( $milestone ) || $milestone eq "" ) {
    ( $milestone = `cwsquery -m $mws -c $cws current 2> /dev/null` ) =~ s/\n//;
}
if ( $milestone ne "" ) {
    print "Milestone:\t$milestone\n";
}
else {
    my $latest = `cwsquery -m $mws latest 2> /dev/null`;

    ( $_, $milestone ) = split( ' ', $latest );
    print "Milestone:\tnot specified, assuming '$milestone'\n";
}

$iz =~ s/^i//;
$ENV{'CWS_WORK_STAMP'} = $cws;
$ENV{'WORK_STAMP'} = $mws;

cws_commit( $mws, $cws, $iz, $summary, $milestone, @patches );

( my $tmp = `pwd` ) =~ s/\n//;
print "
Finished, please check '$tmp' that everything is as expected.
Then:

- Set 'http://www.openoffice.org/issues/show_bug.cgi?id=$iz' to FIXED
  with a 'Committed to CWS $cws.' comment.
- Fill the '$cws' info in 'http://eis.services.openoffice.org'.

It's also usually a good thing to mark the patch in the apply file as
committed to '$cws'.\n";
