#!/usr/bin/perl -w

# lab 10 

#######################################################################
# Change the current directory to /mnt/homes/CLASSES/CIS2279/LAB10/stuff.
$classdir = '/mnt/homes/CLASSES/CIS2279/'.$ENV{'LOGNAME'};
$outfile = $classdir.'/lab10.temp';
$directory = '/mnt/homes/CLASSES/CIS2279/LAB10/stuff';

chdir($directory);


# Reads the contents of the directory /mnt/homes/CLASSES/CIS2279/LAB10/stuff.
opendir(DIR, $directory) || die "cannot open directory: $!";
open(OUT, ">$outfile") || die "Cannot open file: $!";

while($item = readdir(DIR)) {
    # Print the size of each file, its modification time and its last access time to the screen.
    $fullpath = $directory.'/'.$item;
    @info = stat($fullpath);
    print "for $item:\n\tsize: $info[7]\n\tmodification time: $info[9]\n".
        "\tlast access time: $info[8]\n";
    # Print the file name to the screen and to a file in your class directory named "lab10.temp" (along with some descriptive text) if:
    #     the file has a size greater than 10k (10000)
    if($info[7] > 10000) {
        print "The file, $item, is greater than 10k in size.\n"; 
        print OUT "The file, $item, is greater than 10k in size.\n"; 
    }
    #     the file name ends with the letters "gz"
    if($item =~ /gz$/) {
        print "The file, $item, ends in gz\n";
        print OUT "The file, $item, ends in gz\n";
    }
    #     the file's UID is "3005"
    if($info[4] == 3005) {
        print "The file, $item, is owned by uid 3005\n";
        print OUT "The file, $item, is owned by uid 3005\n";
    }

}
closedir(DIR);
# Close the output file.
close(OUT);
    # When completed with reading the files in the LAB10 directory, do the following:
    #     Rename lab10.temp as lab10.output in your class directory.
rename("$outfile", "$classdir/lab10.output");

exit;