#!/usr/bin/perl -T 

use v5.8 ; use strict ; use warnings ; 
use Getopt::Std ; #getoptions 'l' , \my %o ;
use Time::Local ; 
use Term::ANSIColor qw[:constants] ; $Term::ANSIColor::AUTORESET = 1 ;


#@ARGV = @{[grep m/^-/ , @ARGV]} , @{[grep ! m/^-/ , @ARGV ]} if ! grep /^--$/ , @ARGV ;
#print join ", " , @ARGV ; ex
getopts '~.:dhlm',  \my %o ; 
do { select STDERR ; HELP_MESSAGE () } if ! @ARGV ; 
my $dig = $o{'.'} // 1 ; # 小数点以下何桁まで表示をするか
my $t0 = timelocal ( localtime ) ; # 
& main ; 
exit ; 

sub main { 
    my $fmt = "%0.${dig}f" ; # "%.4g" # 出力の printf形式のフォーマット
    sub info ( $ ); 
    my $ info= $o{l} ? sub ($){ lstat $_[0] }  : sub ($) { stat $_[0] }  ;
    for ( @ARGV ) { 
        do { print STDERR CYAN "$_ :- Not exists.\n" ; next } unless  -e ; 
        my @out = map { $t0 - $_ } @{ [ $info -> ($_) ] }[ 8,9,10 ] ;
        if    ( $o{m} ) { @out = map { sprintf $fmt , $_ / 60 } @out } 
        elsif ( $o{h} ) { @out = map { sprintf $fmt , $_ / 3600 } @out }
        elsif ( $o{d} ) { @out = map { sprintf $fmt , $_ / 86400 } @out }

        splice @out , $o{'~'} ? 0 : scalar @out , 0 , $_  ; # scalar は不要か??
        print join ( "\t" , @out ) , "\n" ;
    }
}

## ヘルプの扱い
sub VERSION_MESSAGE {}
sub HELP_MESSAGE {
    use FindBin qw[ $Script ] ; 
    $ARGV[1] //= '' ;
    open my $FH , '<' , $0 ;
    while(<$FH>){
        s/\$0/$Script/g ;
        print $_ if s/^=head1// .. s/^=cut// and $ARGV[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
    }
    close $FH ;
    exit 0 ;
}


=encoding utf8

=head1

 $0  ファイル名のリスト

  各ファイルについて、次の時刻から現在までの秒数を出力する。Seconds after the following for each file specified.
   (1) アクセス access (2) 更新時刻 update (3) i-nodeの変更時刻

 利用例 usage : 

   $0 -m *

 オプション options :

    -d : 日単位で出力する。 in day unit.
    -h : 時間単位で出力する。 in hour unit.
    -m : 分単位で出力する。 in minute unit.
    -l : シンボリックリンクの先のファイルの情報を取得する。

    -~  : ファイル名を各行の先頭に置く。 File names put in the mostleft column.
    -. N : 小数点以下何桁まで表示をするか(秒単位なら意味なし)。 Digits after the decimal point in the output.

=cut
