#!/usr/bin/perl

=encoding utf8

=begin metadata

Name: whoami
Description: display effective user ID
Author: Oğuz Ersen, oguzersen@protonmail.com
License: artistic2

=end metadata

=cut

use strict;

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

if (@ARGV) {
	warn "usage: whoami\n";
	exit EX_FAILURE;
}
my @coderefs = (
	sub { getpwuid($>) },
	sub { require Win32; Win32::LoginName() },
	sub { getlogin },
	sub { $ENV{'USER'} },
	sub { $ENV{'USERNAME'} },
	);

foreach my $coderef ( @coderefs ) {
	my $user = eval { $coderef->() };
	next unless defined $user;
	print "$user\n";
	exit EX_SUCCESS;
	}

exit EX_FAILURE;

=head1 NAME

whoami - display effective user ID

=head1 SYNOPSIS

B<whoami>

=head1 DESCRIPTION

Print the username associated with the current effective user ID,
same as B<id -un>.

=head1 AUTHOR

Oğuz Ersen (oguzersen@protonmail.com)

=head1 SEE ALSO

whoami(1), id(1)

=head1 COPYRIGHT and LICENSE

Copyright (c) by Oğuz Ersen 2021.

This code is licensed under the Artistic License 2.

=cut
