#!/usr/bin/perl -w
# lab12 solution
#----------------------------------------------------------------------
use Time::Local;

# 1. From the output of the ipconfig command, extract and display the 
#    computer's IP address (IPv4)
@ipconf = `ipconfig`;

foreach $line (@ipconf) {
    chomp($line);
    if($line =~ /IPv4/) {
        @ldata = split(/\s+/, $line);
        print "IP Address is $ldata[$#ldata].\n";
    }
}

# 2. Using the command net time capture the time from the server and compare 
#    it with the current time on the local computer. Print the difference in 
#    seconds between the two.
@nettime = `net time`;
foreach $k (@nettime) {
    chomp($k);
    next if($k !~ /^Current/);
    @ldata = split(/\s+/, $k);
    $date = $ldata[5];
    $ntime = $ldata[6];
    $ampm = $ldata[7];
    ($mon, $day, $year) = split('/', $date);
    ($hour, $min, $sec) = split(':', $ntime);
    $mon--;
    $hour += 12 if($ampm eq 'PM');
    $seconds = timelocal($sec, $min, $hour, $day, $mon, $year);
    $now = time;
    $diff = abs($now - $seconds);
    print "The difference in time between the server and local machine is $diff seconds.\n";
}

# 3. Run the command C:\program files (x86)\notepad++\notepad++ with the file
#    "K:\perl\lab12\lab12_0.pl" as an argument to the command.
$notepad = 'C:/program files (x86)/notepad++/notepad++';
$arg = 'K:/perl/lab12/lab12.pl';
$retval = system($notepad, $arg);

# 4. Check the return code from the previous command and print to the screen
#    whether the command completed successfully or not.
if($retval == 0) {
    print "Return value is good.\n";
}else{
    print "Bad return value\n";
}

exit;