#!/usr/bin/env perl
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use strict;
use warnings;
use FindBin qw( $Bin );
use File::Find;

# make sure we are at the top-level dir
if ( !-d 'perl' and !-d 'core' ) {
    chdir("$Bin/../../");
}

my $usage = "$0 version\n";
my $version = shift(@ARGV) or die $usage;

# standardize version strings
my ( $x, $y, $z, $dev_sep, $dev );
if ( $version =~ m/^(\d+)\.(\d+)\.(\d+)$/ ) {
    ( $x, $y, $z, $dev ) = ( $1, $2, $3, 0 );
}
elsif ( $version =~ m/^(\d+)\.(\d+)\.(\d+)([._])(\d+)$/ ) {
    ( $x, $y, $z, $dev_sep, $dev ) = ( $1, $2, $3, $4, $5 );
}
elsif ( $version =~ m/^(\d+)\.(\d\d\d)(\d\d\d)$/ ) {
    ( $x, $y, $z, $dev ) = ( int($1), int($2), int($3), 0 );
}
elsif ( $version =~ m/^(\d+)\.(\d\d\d)(\d\d\d)(_)?(\d\d\d)$/ ) {
    ( $x, $y, $z, $dev_sep, $dev )
        = ( int($1), int($2), int($3), defined($4) ? $4 : '.', int($5) );
}
else {
    die "Unknown version syntax. Try X.Y.Z or X.YYYZZZ\n";
}
my $x_y_z_version    = sprintf( "%d.%d.%d", $x, $y, $z );
my $x_y_z_d_version  = $dev
                       ? $dev_sep eq '_'
                         ? sprintf( "%d.%d.%d_%d", $x, $y, $z, $dev )
                         : sprintf( "%d.%d.%d.%d", $x, $y, $z, $dev )
                       : sprintf( "%d.%d.%d", $x, $y, $z );
my $x_yyyzzz_version = $dev
                       ? $dev_sep eq '_'
                         ? sprintf( "%d.%03d%03d_%03d", $x, $y, $z, $dev )
                         : sprintf( "%d.%03d%03d%03d", $x, $y, $z, $dev )
                       : sprintf( "%d.%03d%03d", $x, $y, $z );

print "Using version: $x_y_z_version ( $x_yyyzzz_version )\n";

my $buf;

# Update charmonizer.
for my $path ('common/charmonizer.c', 'common/charmonizer.main') {
    $buf = read_file($path);
    $buf =~ s/(lucy_version\[\]\s+=\s+)"[\d.]+"/$1"$x_y_z_version"/
        or die "no match";
    $buf =~ s/(lucy_major_version\[\]\s+=\s+)"[\d.]+"/$1"$x.$y"/
        or die "no match";
    $buf =~ s/(cfish_lib_name\s+=\s+)"clownfish-[\d.]+"/$1"clownfish-$x.$y"/
        or die "no match";
    write_file($path, $buf);
}

# Update Lucy.pm.
$buf = read_file('perl/lib/Lucy.pm');
$buf =~ s/(our \$VERSION\ +=\ +)'.+?'/$1'$x_yyyzzz_version'/g
    or die "no match";
$buf =~ s/(bootstrap\s+Lucy\s+)'[\d\._]+'/$1'$x_y_z_d_version'/
    or die "no match";
if ( $z == 0 && $dev == 0 ) {
    $buf =~ s/(our \$MAJOR_VERSION\ +=\ +)[\d._]+/$1$x_yyyzzz_version/g
        or die "no match";
    $buf =~ s/(use Clownfish +)[\d\._]+/$1$x_yyyzzz_version/g
        or die "no match";
    $buf =~ s/(if \$Clownfish::MAJOR_VERSION\ +>\ +)[\d._]+/$1$x_yyyzzz_version/g
        or die "no match";
}
write_file( 'perl/lib/Lucy.pm', $buf );

# Update Lucy/Test.pm.
$buf = read_file('perl/lib/Lucy/Test.pm');
$buf =~ s/(our \$VERSION\ +=\ +)'.+?'/$1'$x_yyyzzz_version'/g
    or die "no match";
$buf =~ s/(bootstrap\s+Lucy::Test\s+)'[\d\._]+'/$1'$x_y_z_d_version'/
    or die "no match";
write_file( 'perl/lib/Lucy/Test.pm', $buf );

# Update Lucy.pod.
$buf = read_file('perl/lib/Lucy.pod');
$buf =~ s/(^=head1\s+VERSION\s+)([\d._]+)/$1$x_y_z_d_version/m
    or die "no match";
write_file( 'perl/lib/Lucy.pod', $buf );

# Update Build.PL
$buf = read_file('perl/Build.PL');
$buf =~ s/(dist_version\ +=>\ +)'.+?'/$1'$x_y_z_d_version'/
    or die "no match";
if ( $z == 0 && $dev == 0 ) {
    $buf =~ s/(Clownfish::CFC::Perl::Build'\s*=>\s*)(.*?),/$1$x_yyyzzz_version,/g
        or die "no match";
    $buf =~ s/(Clownfish::CFC'\s*=>\s*)(.*?),/$1$x_yyyzzz_version,/g
        or die "no match";
    $buf =~ s/(Clownfish'\s*=>\s*)(.*?),/$1$x_yyyzzz_version,/g
        or die "no match";
}
write_file( 'perl/Build.PL', $buf );

# Update ruby/apache_lucy.gemspec
$buf = read_file('ruby/apache_lucy.gemspec');
$buf =~ s/(spec\.version\s+=\s+)'.+?'/$1'$x_y_z_version'/
    or die "no match";
write_file( 'ruby/apache_lucy.gemspec', $buf );

# Update ruby/lib/apache_lucy.rb
$buf = read_file('ruby/lib/apache_lucy.rb');
$buf =~ s/(VERSION\s+=\s+)'.+?'/$1'$x_y_z_version'/
    or die "no match";
write_file( 'ruby/lib/apache_lucy.rb', $buf );

# Update Lucy.cfp.
$buf = read_file('core/Lucy.cfp');
$buf =~ s/("version":\s+)"v\d+\.\d+\.\d+"/$1"v$x_y_z_version"/
    or die "no match";
if ( $z == 0 && $dev == 0 ) {
    $buf =~ s/("major_version":\s+)"v\d+\.\d+\.\d+"/$1"v$x_y_z_version"/
        or die "no match";
    $buf =~ s/("Clownfish":\s+)"v\d+\.\d+\.\d+"/$1"v$x_y_z_version"/
        or die "no match";
}
write_file( 'core/Lucy.cfp', $buf );

# Update TestLucy.cfp
$buf = read_file('test/TestLucy.cfp');
$buf =~ s/("version":\s+)"v\d+\.\d+\.\d+"/$1"v$x_y_z_version"/
    or die "no match";
if ( $z == 0 && $dev == 0 ) {
    $buf =~ s/("Clownfish":\s+)"v\d+\.\d+\.\d+"/$1"v$x_y_z_version"/
        or die "no match";
}
$buf =~ s/("Lucy":\s+)"v\d+\.\d+\.\d+"/$1"v$x_y_z_version"/
    or die "no match";
write_file( 'test/TestLucy.cfp', $buf );

# Update all other Perl modules.
find sub {
    return unless /[.]pm$/;
    my $name = $_;
    return if $name eq 'Lucy.pm';
    my $buf = read_file($name);
    $buf =~ s/(our \$VERSION\ +=\ +)'.+?'/$1'$x_yyyzzz_version'/g
        or die "no match in $File::Find::name";
    write_file($name, $buf);
}, 'perl';

# Update c/install.sh
$buf = read_file('c/install.sh');
$buf =~ s/\bversion=[\d.]+/version=$x_y_z_version/
    or die "no match";
$buf =~ s/\bmajor_version=[\d.]+/major_version=$x.$y/
    or die "no match";
write_file( 'c/install.sh', $buf );

# utility functions
sub read_file {
    my ($file) = @_;
    local $/;
    open( F, "< $file" ) or die "Cannot read $file: $!\n";
    my $buf = <F>;
    close(F) or die "Cannot close $file: $!\n";
    return $buf;
}

sub write_file {
    my ( $file, $buf ) = @_;
    open( F, "> $file" ) or die "Cannot write $file: $!\n";
    print F $buf;
    close(F) or die "Cannot close $file: $!\n";
}

print "Done.  Consider running git grep to search for the old version.\n";
exit();

__END__

=head1 NAME

update_version - update Lucy version strings in source files

=head1 SYNOPSIS

 perl devel/bin/update_version version

=head1 DESCRIPTION

Updates key source files with I<version>, using correct syntax
depending on the file format and type.

I<version> may be specified in either format:

 X.Y.Z
 X.Y.Z_D (CPAN developer release)
 X.YYYZZZ
 X.YYYZZZ_DDD (CPAN developer release)

and update_version will convert the specified string into the 
correct format for each relevant file.

=cut