#!perl

our $DATE = '2016-09-29'; # DATE
our $VERSION = '0.002'; # VERSION

#IFUNBUILT
# use strict;
# use warnings;
#END IFUNBUILT

if (@ARGV != 1) { print ".\n"; exit }

my $cwd = $ENV{PWD} || do { require Cwd; Cwd::cwd() };
my @elems = split m!/+!, $cwd;

for my $i (reverse 0..$#elems) {
    my @newelems = @elems; $newelems[$i] = $ARGV[0];
    my $newpath = join "/", @newelems;
    if (-d $newpath) { print "$newpath\n"; exit }
}

# no match
print ".\n";

# ABSTRACT: Change directory part
# PODNAME: cdpart-backend

__END__

=pod

=encoding UTF-8

=head1 NAME

cdpart-backend - Change directory part

=head1 VERSION

This document describes version 0.002 of cdpart-backend (from Perl distribution App-CdUtils), released on 2016-09-29.

=head1 SYNOPSIS

To use in shell:

 % cdpart() { cd `cdpart-backend "$1"`; }

 % cd /media/mv/en/c/celine-dion
 % cdpart fr; # /media/mv/fr/c/celine-dion

=head1 DESCRIPTION

B<cdpart> will try to substitute a directory element with specified argument,
starting from the deepest. B<cdpart> is useful if you have some branches with
identical structures and you want to move to another branch quickly.

Suppose we have this directory structure:

 media/
   mv/
     en/
       a/
       b/
       c/
         carly-rae-japsen/
         celine-dion/
         charlie-puth/
         ...
       ...
     fr/
       a/
       b/
       c/
         celine-dion/
         christophe-willem/
         ...
     de/
     ...
   music/
     en/
       a/
       b/
       c/
         celine-dion/
         ...
     fr/
       a/
       b/
       c/
         celine-dion/
         ...
       ...
     ...
  ...

You can see that the directories C<mv> and C<music> have the same structure: the
first subdirectory level is language code (C<en>, C<fr>, ...), the second level
is the first letter of artist's name (C<a>, C<b>, C<c>, ...) and the third level
is the name of the artist.

Suppose you are now in directory:

 /media/mv/en/c/celine-dion

and you want to see Céline's French music videos
(C</media/mv/fr/c/celine-dion>). If you use B<cd>, the command will be:

 % cd ../../fr/c/celine-dion

which is too much to type because you are retyping the substructure. With
B<cdpart> you just have to type:

 % cdpart fr

What B<cdpart> will do is try the following directories in order:

 /media/mv/en/c/fr
 /media/mv/en/fr/celine-dion
 /media/mv/fr/c/celine-dion

The third succeeds so it goes there. If no match is found until the topmost
directory, B<cdpart> will not change directory.

Another example:

 % cdpart music

will eventually change the directory to C</media/music/fr/c/celine-dion>.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-CdUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-CdUtils>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-CdUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
