#!/usr/local/bin/perl
#
# gon - Show who's logged into goofey, highlighting watched people
#
# 1996 Andrew J. Cosgriff
# ajc@bing.wattle.id.au
#
# Creation Date : Mon Apr 15 17:26:51 1996
# $Revision: 1.1 $
# $Date: 1998/07/07 12:38:03 $
#
# Free for use as long as you don't claim you wrote it or charge money for it.
# I'm not responsible for your inability to use this program for its intended
# purpose.  Use at your own risk.
#
########################################
# 
# Set this to 1 if you want two spaces between each username, like
# "goofey -W" does.
$yes_i_am_blind=0;
#
# How to highlight and unhighlight text - Set to taste
#
$highlight_on=chr(27) . '[36m';
$highlight_off=chr(27) . '[0m';
#
# Name for the extra watch file (people we want to highlight but don't watch)
#
$extra_watch_file=".goofey_extrawatch";
$extra_highlight_on=chr(27) . '[33m';
$extra_highlight_off=chr(27) . '[0m';
########################################
use Getopt::Std;
use File::Basename;

my $version = substr q$Revision: 1.1 $, 10;
chop $version;

# 
# Command line guff
#
getopts('awfbuh');
if ($opt_h) {
  print << "EOM";
gon v.$version Andrew J Cosgriff <ajc\@bing.wattle.id.au>
usage: gon [-a|-w|-f|-b]
  -a : show idle people as well (like the difference between goofey -w and -W)
  -w : just show people in your watch
  -f : just show people in your watch + people in your "extra" watch
  -b : put double spaces between names, like goofey -w does
  -u : update local cache of your watch

people in your goofey watch look ${highlight_on}like this${highlight_off}
people in your "extra" watch (~/${extra_watch_file}) look ${extra_highlight_on}like this${extra_highlight_off}
EOM
  exit 1;
}
########################################
#
# On a slow link, cache our goofey watch to ~/.goofey_watch and read
# from there (I keep my watch in ~/.goofey_watch already, since I also
# use it for shell completion)
#
$watch_file = $ENV{'HOME'} . "/.goofey_watch";
if ($opt_u or (-M $watch_file > 1) or (! -f $watch_file)) {
  print "Caching goofey watch into ~/.goofey_watch\n";
  system "goofey -a+ watch > $ENV{'HOME'}/.goofey_watch";
  chmod(0600, $watch_file);
}
# 
# Read the watch into an associative array keyed on the name. 
#
foreach $x (split(/\s+/, `cat $watch_file`)) {
   $watch{$x}=1;
}
# 
# Read the extra watch into an associative array keyed on the name. 
#
$extra_watch_file = $ENV{'HOME'} . "/" . $extra_watch_file;
if (-r $extra_watch_file) {
  foreach $x (split(/\s+/, `cat $extra_watch_file`)) {
    $extra_watch{$x}=1;
  }
}
######################################## 
# 
# If -a ("all") was given as a command line arg, show idle people too.
#
if ($opt_a) {
  $opts = "-w";
} else {
  $opts = "-W";
}
# 
# Misc. initialization guff 
#
$watched=0;
$extra_watched=0;
$count=0;
if ($yes_i_am_blind || $opt_b) {
  $space = '  ';
} else {
  $space = ' ';
}
$screenwidth = $ENV{'COLUMNS'} || 80;
$line='';
$curlen=0;

open (Goofey, "goofey $opts |") || die "Can't start goofey";
while (<Goofey>) {
  chomp;
  die $_ if ($_ eq "Pluto failed to reply within 20 seconds");

  @list=split(/\s+/, $_);

  foreach $x (@list) {
    #
    # Remove () and * before checking if people are in our watch list 
    #
    $name = $x;
    $name =~ tr/()*//d;
    $count++;

    if ($watch{$name} == 1) {
      if ($opt_w or $opt_f) {
	&add_name("$x", length($x));
      } else {
	&add_name("$highlight_on$x$highlight_off", length($x));
      }
      $watched++;
    } elsif ($extra_watch{$name} == 1) {
      if ($opt_f) {
	&add_name("$x", length($x));
      } else {
	&add_name("$extra_highlight_on$x$extra_highlight_off", length($x));
      }
      $extra_watched++;
    } elsif (! ($opt_w or $opt_f)) {
      &add_name("$x", length($x));
    }
  }
}
printf "$line\n";
# 
# Print a total, with percentage watched. 
#
printf("[%s%d%s watched + %s%d%s extras = %s%.2f%%%s of %s%d%s people]\n",
       $highlight_on, $watched, $highlight_off,
       $highlight_on, $extra_watched, $highlight_off,
       $highlight_on, (($watched + $extra_watched) * 100 / $count), $highlight_off, 
       $highlight_on, $count, $highlight_off);

close Goofey;

sub add_name {
  my($name, $strlen) = @_;

  if (($curlen + $strlen) >= ($screenwidth -1)) {
    print $line,"\n";
    $line = $name . $space;
    $curlen = $strlen + length($space);
  } else {
    $line .= $name . $space;
    $curlen += $strlen + length($space);
  }
}
########################################
#
# $Id: gon,v 1.1 1998/07/07 12:38:03 ajc Exp $
#
