#!/usr/bin/perl -w

# lab 11 solution
#----------------------------------------------------------------------
use Time::Local;

# 1. Have the user enter a date (YYYY MM DD)
print "Please enter a date (MM DD YYYY): ";
chomp($input = <STDIN>);

($mon, $day, $year) = split(/\s+/, $input); # split the input

# convert the input date to seconds using timelocal
$input_sec = timelocal(0,0,0,$day,$mon -1,$year);

# 2. Calculate the date/time six days fourteen hours in the future from the 
#    entered date and print it in a human readable format.
$sixdays = 60 * 60 * 24 * 6;
$fourteen_hrs = 60 * 60 * 14;
$futureseconds = $input_sec + $sixdays + $fourteen_hrs;

print 'The date in six days, fourteen hours would be '.localtime($futureseconds)."\n";


# 3. Calculate the date/time 45 days in the past from the entered date and 
#    print it in a human readable format.
$fortyfivedays = 60 * 60 * 24 * 45;
$pastSeconds = $input_sec - $fortyfivedays;
print 'The date 45 days in the past would be '.localtime($pastSeconds)." or $pastSeconds\n";

# 4. Calculate and display, in minutes, the difference between the entered 
#    date and the present time.
$now = time;
$diff = abs($now - $input_sec);
$diff_min = int($diff / 60);
print "The difference between the input date and now is $diff_min minutes.\n";


# 5. Prome the user to enter a path and list the size and creation time (in 
#    human readable format) for each file or directory contained in the 
#    requested directory. 
do {
    print "Please enter a path to scan: ";
    chomp($path = <STDIN>);
}until(-d $path);


opendir(PTH, $path) || die "cannot open path for read: $!";
while($item = readdir(PTH)) {
    next if(($item eq '.') || ($item eq '..'));
    $fullpath = $path .'/'.$item;
    @finfo = stat($fullpath);
    print "File: $fullpath\n";
    print "\tFile Size: $finfo[7]\n";
    print "\tCreation Date: ".localtime($finfo[10])."\n";
}
closedir(PTH);

# 6. Prompt the user to enter their birth date. With that date, convert and
#    display their age in days and hours to the terminal.
print "Enter your birth date (MM DD YYYY): ";
chomp($bday = <STDIN>);
($mon, $day, $year) = split(/\s+/, $bday);
$bdSeconds = timelocal(0,0,0,$day,$mon - 1,$year);
$diffHRS = int(($now - $bdSeconds)/(60 * 60));
$diffDYS = int($diffHRS/24);

print "You are $diffHRS hours old or $diffDYS days.\n";
print "Or $diffDYS days and ".($diffHRS - ($diffDYS * 24))." Hours old.\n";

exit;