#!/usr/bin/perl

use v5.8 ; use strict ; use warnings ; 
use Getopt::Std ; 
use Time::Local ; 

@ARGV = ( ( grep m/^-/ , @ARGV ) , ( grep !m/^-/ , @ARGV ) ) if ! grep /^--$/ , @ARGV ;
getopts '~.:dhm',  \my %o ; 

my $dig = $o{'.'} // 1 ; # 小数点以下何桁まで表示をするか

if ( ! @ARGV ) { 
    select STDERR ; 
    HELP_MESSAGE () ; 
}

my $t0 = timelocal ( localtime ) ; 
my $fmt = "%0.${dig}f" ; # "%.4g" # 出力の printf形式のフォーマット
for ( @ARGV ) { 
    next unless  -e ; 
    my @out = map { $t0 - $_ } @{ [stat $_ ] }[ 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.

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

=cut
