#!@PERL@ -w
# -*- perl -*-
#
# $NetBSD: check-cvs-diff,v 1.7 2022/11/19 13:16:00 mef Exp $
#
# Copyright (c) 2014 The NetBSD Foundation, Inc.
#
# This code was originally contributed to the NetBSD Foundation, Inc.
# by Makoto Fujiwara <mef@NetBSD.org>.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of author nor the names of its contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# read output of (cd /usr/pkgsrc; cvs diff -u ) and check if
# py* and ruby* strings are left in buildlink3.mk.
# They are then modified to Variable representation.
#"
use strict;
use Getopt::Std;
our(@ARGV);
my (%opts);

my($my_name) = $0;
$my_name =~ s#(.*)/##;

sub usage(){
    print <<HELP;
$my_name:
    Read cvs diff -u output (of named file) and substitute
    if the string py27, py34 or ruby200 is found in the corresponding
    changes in diff.
Synopsys:
    $my_name  [-h] [-p pkgsrc_dir] [-v] [cvs-diff-u_output-file]
Where:
    -h  Show this help
    -p  pkgsrc directory other than /usr/pkgsrc
    -v  verbose (not actually implemented yet)
    cvs-diff-u_output_file:
        The name of input. Usually the file including 'cvs diff -u'
        output (default cvs-diff)
See Also:
    revbump(1) for how to use it.
HELP
}

sub main() {
    my ($PKGSRCDIR);
    my ($CVS_DIFF)  = 'cvs-diff';
    my ($file_to_edit);
    my ($stay, $mod) = (0,0);
    my ($Makefile, $buildlink3, $other) = (0, 0, 0);

    $PKGSRCDIR = $ENV{PKGSRCDIR};
    if (! $PKGSRCDIR) {
        $PKGSRCDIR = "/usr/pkgsrc";
    }

    getopts('hvp:',\%opts);
    if ($ARGV[0])   { $CVS_DIFF = $ARGV[0]}
    if ($opts{'p'}) { $PKGSRCDIR = $opts{'p'}; }
    if ($opts{'h'}) { usage() ; exit ;}

   # for using rename, unlink
    chdir $PKGSRCDIR;

    open(CVS_DIFF, $CVS_DIFF) || die "Problem opening file $CVS_DIFF: $!\n";
    while(<CVS_DIFF>){
	# Looking for the +++ filename line in cvs diff (supposed to use 'cvs diff -u')
	$file_to_edit = '';
	if ( /^\+\+\+ (\S+)/) {
	    $file_to_edit = $1; }
	if ( $file_to_edit && -f $file_to_edit ) {
	    if    ( $file_to_edit =~ /Makefile$/ )      { $Makefile++;}
	    elsif ( $file_to_edit =~ /buildlink3.mk$/ ) { $buildlink3++;
		#	    print __LINE__, ' ', $file_to_edit,"\n";
		my ($new_file) = $file_to_edit. '.new';
		my ($edit) = 0;
		open(NEW,  "> $new_file" ) || print STDERR "Problem opening file $new_file: $! \n";
		open(EDIT, $file_to_edit ) || print STDERR "Problem opening file $file_to_edit: $! \n";
		while(<EDIT>) {
		    if ( /^BUILDLINK.*py27/ )    {  $_=~    s/py27/\${PYPKGPREFIX}/   ; $edit++;}
    		    if ( /^BUILDLINK.*py3[0-9]*/ )    {  $_=~    s/py3[0-9]*/\${PYPKGPREFIX}/   ;
						    print STDERR "   py3[0-9]* found at $file_to_edit\n";
						    ; $edit++;}
		    if ( /^BUILDLINK.*ruby[0-9][0-9]*/ ) {  $_=~ s/ruby[0-9][0-9]*/\${RUBY_PKGPREFIX}/ ; $edit++;}
		    print NEW $_;
		}
		close(EDIT);
		close(NEW);
		if ($edit) { unlink $file_to_edit;
			     rename $new_file, $file_to_edit;
			     $mod++;}
		else {
		    $stay++;
		    unlink $new_file; };
	    } else  { $other++; print STDERR "  (other) ", $file_to_edit,"\n"}
	    }
        }
	close(CVS_DIFF);
    printf STDERR "Makefile:      %4d\n", $Makefile;
    printf STDERR "buildlink3.mk: %4d\n", $buildlink3;
    printf STDERR "  Modified:    %4d\n", $mod;
    printf STDERR "  Untouched:   %4d\n", $stay;
    printf STDERR "Other:         %4d\n", $other;
}

main();
exit;

