#!/usr/bin/perl
#
# SCRIPT THAT LISTS IN CHRONOLOGICAL ORDER THE OWNER
# OF FILE $file_login WITH RESPECT TO THE MODIFICATION
# TIME OF $file_login.

# ****************************************
# FILE TO DEFINE LOGIN TIME OF USERS
# ****************************************
$file_login = ".Xauthority";

# ****************************************
# TO PRINT THE DATE IN
# HUMAN READABLE FORMAT.
# ****************************************
require "ctime.pl";

# ****************************************
# TO CHECK ARGUMENTS
# ****************************************
( ( ( ($dir) = @ARGV ) == 1 ) && -d $dir )
  || die "Usage: $0 dir\n";

# ****************************************
# UNBUFFER OUTPUT
# ****************************************
$|=1;

# ****************************************
# GET UID-USERNAME RELATIONSHIP
# ****************************************
open(PASSWD, "ypcat passwd|") 
	|| die "Cannot open NIS password file: $!\n";

while (<PASSWD>)
{
	chop;
	($login,$passwd,$uid,$gid,$gcos,$home,$shell) = split(/:/);
	($uid_user{$uid}, $role) = split(/,/,$gcos);
# 	print "$uid, $uid_user{$uid}\n";
}

# ****************************************
# FIND COMMAND 
# ****************************************
open(FIND,"find $dir -name $file_login -maxdepth 2 -print|");

# ****************************************
# BUILD ARRAY
# ****************************************
while ($files_find = <FIND>) {

	# GET RID OF ENDING \n
	chop($files_find);

	# GET FILE PROPERTIES
	($ndev,$nino,$nmode,$nnlink,$nuid,$ngid,$nrdev,$nsize,
	  $natime,$nmtime,$nctime,$nblksize,$nblocks) 
		= stat($files_find);

#	print "$files_find -- $nmtime \n";

	# CONCATENATE RESULTS IN CASE OF DUPLICATE
	# ENTRIES
	$files_find .= "\n\t",$files_find;
	$var_date{$nmtime} .= $files_find;

	$temp_uid  = "";
	$temp_uid .= "\n";
	$temp_uid .= "\t";
 	$temp_uid .= $uid_user{$nuid};
	$var_uid{$nmtime} .= $temp_uid;
}

#print "\n";

# ****************************************
# PRINT RESULTS
# ****************************************
foreach $key ( reverse sort (keys(%var_date)) )
{
	# FORMAT THE MACHINE TIME INTO
	# HUMAN READABLE FORMAT
	$the_time = &ctime($key);
	# TAKE AWAY EXTRA CARRIAGE RETURNS
	chop $the_time;

 	chop $var_date{$key}; 
 	chop $var_date{$key}; 

	# PRINT RESULT
	print $the_time,
#		"\n\t",$var_date{$key}, 
		"\t",$var_uid{$key}, "\n";
}

# END