#!/proj/ahd03/CAisd/ActivePerl-5.6.0.618/bin/perl
#######/usr/local/bin/perl
#-------------------------------------------------------------------------------------------
# Script Name:	clientlogoff.pl  		 
# Script Version:  1.0
# Date: 10/12/2002
# Author:  vema
# Department:  AHD Support
# Description:zap any vbop's on the system.  

use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Terse = 1;

# strftime is the same as the c stringify time command
use POSIX 'strftime';

# today's date in parts
#   if today were Sunday, this returns "Sun"
my $today = strftime '%a', localtime( time + 24 * 60 * 60 );

#  NOTE: this is a 24-hour clock, it mimics "slstat"
#        which also uses a 24-hours clock.
my $cutoff_time = serial_time( '14:55:00' );

# step 1
my @results = qx( slstat| grep vbop );

unless ( $results[0] ) {
	print STDERR q/slstat| grep vbop' didn't return any results or returned erroneous results/;
	print STDERR "\@results:", Dumper @results;
	exit;
}

# step 2
my $zap_file = `. /home/paradigm/.profile >>/proj/ahd03/CAisd/site/mods/scripts/vbopzap.txt`;
open FH,'>',$zap_file or die "Cannot open $zap_file: $!";
	print FH @results;
close FH;

# step 3
chomp @results;
my %users_by_time;
foreach my $result ( @results ) {

	my @fields = split ' ', $result, 4;

	# ignore results from today
	next if $fields[-1] =~ /$today/oi;

	my $time = serial_time( get_time_from_date( pop @fields ) );

	my $id = $fields[0] || next;
	$users_by_time{ $time } = [ $result,  $id ];
}

# step 4
foreach my $time ( keys %users_by_time ) {
	if ( $time < $cutoff_time ) {
		print STDERR "Zapping: $users_by_time{ $time }[0]";
		print STDERR qx( slzap $users_by_time{ $time }[1] );
	}
}

sub get_work_station_id {
	# return W0010A40C06D0 from vbop-0x4E580000:W0010A40C06D0:windows
	my $id = shift;
	return $1 if $id =~ /:([^:]+):/;
	return;
}

sub get_time_from_date {
	# return 09:23:45 from Fri Dec 6 09:23:45 2002
	my $date = shift;
	my $time = ( split ' ', $date )[3];
	return $time;
}

sub serial_time {
	# return a decimal from hh:mm:ss time
	my( $hour, $minute, $second ) = split /:/, $_[0];
	return $hour + $minute / 60 + $second / 60 / 60;
}

__END__
