#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use File::Basename;
use Time::localtime;
use File::stat qw(:FIELDS);

$|=1;
my %opts;
my $me = basename($0);

getopts('amspuh', \%opts);
do { usage();exit } if exists $opts{h};
usage() if (@ARGV < 1);

if (keys %opts) {
    for my $file (@ARGV) {
        do { warn "File: $file not found\n"; next } unless -f $ARGV[0];
        print "\nFile: $file\n";
        stat($file) or do {warn "Cannot get statistics on $file", $!;next };
        SWITCH:for (keys %opts) {
            
                /^a$/i    && do {
                                    print "\tatime: ", ctime($st_atime), "\n";
                                    next SWITCH
                                };
    
                /^m$/i    && do {
                                    print "\tctime: ", ctime($st_mtime), "\n";
                                    next SWITCH
                                };
                
                /^s$/i    && do {
                                    print "\tsize:  $st_size\n";
                                    next SWITCH
                                };
                
                /^p$/i    && do {
                                    my $fmode = sprintf "%o", $st_mode;
                                    $fmode =~ s/^.{3}(.{3}$)/$1/;
                                    print "\tmode:  $fmode\n" ;
                                    next SWITCH
                                };
    
                /^u$/i    && do {
                                    
                                    printf "\towner: %s\n", ($^O =~ /^MSW/) ?
                                        "Unsupported in MS Windows"
                                        :
                                        (getpwuid $st_uid)[0];
                                    next SWITCH
                                };    
        }
    }    
} else {
    do_all($_) foreach (@ARGV)
}

sub do_all {
    my $file = shift;
    print "\nFile: $file\n";
    stat($file) or do {warn "Cannot get statistics on $file", $!;next };
    print "\tatime: ", ctime($st_atime), "\n";
    print "\tctime: ", ctime($st_mtime), "\n";
    print "\tsize:  $st_size\n";
    my $fmode = sprintf "%o", $st_mode;
    $fmode =~ s/^.{3}(.{3}$)/$1/;
    print "\tmode:  $fmode\n" ;
    printf "\towner: %s\n", ($^O =~ /^MSW/) ?
        "Unsupported in MS Windows"
        :
        (getpwuid $st_uid)[0];    
}

sub usage {
    die <<EOM

    Usage: $me [-a] [-s] [-m] [-p] [-u] [-h] file

                -a  Print files access time.
                -m  Print files modify time.
                -s  Print files size in bytes.
                -p  Print files permissions(mode).
                -u  Print files owner user id.
                -h  Print help(this screen).
        
        Any combination of options may be used with the exception of
        -h The help command is exclusive.
        If no options are present on the command line all statistics
        will be returned.

EOM
}

