#!perl

use strict;
use warnings;

use Getopt::Long qw(:config gnu_getopt no_ignore_case);

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2021-11-26'; # DATE
our $DIST = 'App-realpath'; # DIST
our $VERSION = '0.004'; # VERSION

my $opt_logical = 0;
sub parse_cmdline {
    my $res = GetOptions(
        'help|h' => sub {
            print <<USAGE;
Usage:
  realpath [OPTIONS]... <FILE>...
  realpath --version (-v)
  realpath --help (-h, -?)

Options:
  -L   use logical path (from PWD env) for working directory
  -P   use physical path (from Cwd::cwd()) for working directory (the default)

For more details, see the manpage/documentation.
USAGE
            exit 0;
        },
        'logical|L'  => sub { $opt_logical = 1 },
        'physical|P' => sub { $opt_logical = 0 },
        'version|v' => sub {
            no warnings 'once';
            print "realpath (perl-based) version ".($main::VERSION // 'dev').
                "\n";
            exit 0;
        },
    );
    if ($res) {
        if (!@ARGV) {
            warn "realpath: Please specify one or more files\n";
            $res = 0;
        }
    }
    exit 99 if !$res;
}

sub run {
    require Cwd;
    require File::Spec;

    for my $path (@ARGV) {
        if ($opt_logical) {
            print File::Spec->rel2abs($path, $ENV{PWD}), "\n";
        } else {
            print Cwd::realpath($path), "\n";
        }
    }
}

# MAIN

parse_cmdline();
run();

1;
# ABSTRACT: Print the resolved (absolute) path
# PODNAME: abspath

__END__

=pod

=encoding UTF-8

=head1 NAME

abspath - Print the resolved (absolute) path

=head1 VERSION

This document describes version 0.004 of abspath (from Perl distribution App-realpath), released on 2021-11-26.

=head1 SYNOPSIS

 % realpath [OPTION]... <FILE>...
 % abspath  [OPTION]... <FILE>...

To demonstrate how C<realpath>, C<quickabspath>, and C<relpath> give you
different results:

 % pwd
 /home/ujang

 % mkdir dir1
 % ln -s dir1 sym1

 % cd sym1

 % realpath .          ;# gives absolute path and resolve symlinks (-P, the default)
 /home/ujang/dir1
 % realpath -P .       ;# ditto (-P is the default)
 /home/ujang/dir1
 % abspath .           ;# an alias for realpath
 /home/ujang/dir1
 % realpath -L foo     ;# uses PWD for working directory (-L)
 /home/ujang/sym1/foo

 % quickabspath .      ;# gives absolute path but does not resolve symlinks
 /home/ujang/sym1

                        # relpath does the opposite, converting to relative path
 % relpath /home/ujang/dir1
 .
 % relpath /home/ujang/sym1
 ../sym1

=head1 DESCRIPTION

This is the Perl-based implementation alternative for the Unix utility
B<realpath> (also aliased to L<abspath>). It uses L<Cwd>'s C<realpath> (which is
the same as C<abs_path>) function. The function works by reading "." (the
current directory) and moving upwards until it reaches the root directory,
resolving symlinks along the way.

When L<-L> (C<--logical>) option is given, will use PWD environment variable and
not resolve symlinks.

=head1 EXIT CODES

0 on success.

99 on command-line options error.

=head1 OPTIONS

=head2 --physical, -P

=head2 --logical, -L

=head1 FAQ

=head1 ENVIRONMENT

=head2 PWD

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 SEE ALSO

Unix utility B<realpath>

L<relpath>, L<quickabspath>

L<Cwd>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 CONTRIBUTING


To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.

Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
beyond that are considered a bug and can be reported to me.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2019 by perlancar <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.

=head1 BUGS

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

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.

=cut
