#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  stat.pl - Show full status info for files
#
#SYNOPSIS
#  stat.pl [file]..
#REQUIRES
#
#DESCRIPTION
#
#OPTIONS
# None yet, but there may be some soon.
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 2;	# Verbose level.
push @INC, '.', 'pm', 'sh', split(':',$ENV{'PATH'});
print "$0: INC='" . join(':',@INC) . "'\n" if $V>2;
require "DT.pm";

foreach $file (@ARGV) {
	print "Status for \"$file\" at $now=$cymd_hms:\n";
	if ($V>0) {
		($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
		print "\tdev: $dev ino: $ino at $now=" . lCYMD_hms($cymd_hms)\n";
		print "\tmode: $mode\tnlink: $nlink\n";
		print "\tuid: $uid\tgid: $gid\n";
		print "\trdev: $rdev\tsize: $size\n";
		print "\tctime: $ctime=" . dhms($ctime) . " atime: $atime=" . dhms($atime) . "\n";
		print "\tmtime: $mtime=" . dhms($mtime);
		print "\tblksize: $blksize\tblocks: $blocks\n";

	}
}

exit $exitstat;

sub dhms {my $F='dhms'; local($ptime) = @_;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Convert a second count to days, minutes, hours and seconds.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($val,$d,$h,$m,$s);
	$s = $ptime % 60; $ptime /= 60;
	$m = $ptime % 60; $ptime /= 60;
	$h = $ptime % 24; $d = $ptime /24;
	$val = sprintf("%dd%dh%dm%ds",$d,$h,$m,$s);
	$val =~ s/^[0hmds]+//;
	$val = '0s' unless $val;
	return $val;
}

sub DT {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Get the current date and time.  We leave the integer  timestamp  in #
# $now,  and  the OSI date/time in $cymdhms.  We also leave a shorter #
# date/time string without the century and year in $mdhms. Our return #
# value is $now, the Unix integer timestamp.                          #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($ss,$mm,$hh);		# Should these be global? Not needed yet ...
	($ss,$mm,$hh,$DD,$MM,$CY) = gmtime($now = time);
	$CY += 1900;
	$MM += 1;
	$md = sprintf('%02d%02d',$MM,$DD); 
	$hm = sprintf('%02d%02d',$hh,$mm);
	$hh_mm = sprintf('%02d:%02d',$hh,$mm);
	$hms = sprintf('%s%02d',$hm,$ss);
	$cymd = sprintf('%04d%02d%02d',$CY,$MM,$DD); 
	$mdhms = sprintf('%02d%02d%02d%02d%02d',$MM,$DD,$hh,$mm,$ss); 
	$cymdhm = "$cymd$hm"; 
	$cymdhms = sprintf('%04d%02d%02d%02d%02d%02d',$CY,$MM,$DD,$hh,$mm,$ss); 
	$cymdhms = "$cymdhm$ss"; 
	$cymd_hms = "$cymd $hms";
	return $now;
}

sub lCYMD_hms { local($time) = @_;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Convert a time value to the corresponging "date time string. #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($ss,$mm,$hh,$DD,$MM,$CY);	
	($ss,$mm,$hh,$DD,$MM,$CY) = localtime($time);
	$CY += 1900;
	$MM += 1;
	return sprintf('%04d-%02d-%02d %02d:%02d:%02d',$CY,$MM,$DD,$hh,$mm,$ss); 
}
