#!/usr/bin/perl
#
# Catch222 (llama@osoal.org.nz)
# One Short Of A Llama [http://web.osoal.org.nz]

#
use strict;
use OneShortOfALlama;
use POSIX;

#
my(%finger, $load);

#
$load   = 300;
%finger = (
		   'DeadBeef'    => 'lincoln',
		   'Kismet'      => 'david',
		   'Catch22'     => 'llama',
		   'maharG'      => 'graham',
#                   'ZenSandwich' => 'colin'
		   );

#
sub printfile
{
	my($file, $load, $buf);
	local(*FILE);

	$file = shift(@_);
	$load = shift(@_);
	
	open(FILE, "<$file")
		or return ();

	while ($buf = readline(*FILE))
	{
		print("$buf<br>");
		
		unless (--$load)
		{
			last;
		}
	}

	close(FILE);
}

sub main
{
	my($nickname, $buf, $param);
	local(*FINGER);

	#
	$param = variables();

	unless ($param->{'load'} =~ m/^\d+$/ || $param->{'load'} == -1)
	{
		$param->{'load'} = $load;
	}

	#
	header('Finger', 'finger.png', '/');

	print('<div align="center">
           <table width="600" cellpadding="0" cellspacing="0" border="0">
           <tr>
           <td align="left">', "\n\n");

        #
	print('<div align="center">
           <form method="GET" action="', $ENV{'SCRIPT_NAME'}, '">
           <input type="hidden" name="user" value="', encode($param->{'user'}), '">
           [');

	for $nickname (keys(%finger))
	{
		if ($nickname eq $param->{'user'})
		{
			print(' <b>', $nickname, '</b> |');
		}
		else
		{
			print(' <a href="', $ENV{'SCRIPT_NAME'}, '?user=', $nickname, '&load=', $param->{'load'},
				  '"><b>', $nickname, '</b></a> |');
		}
	}

	print(' <b>Display</b> <input type="text" name="load" value="', $param->{'load'}, '" size="3">
            <b>Lines</b>
           | <a href="/search.pl?location=Finger"><b>Search</b></a> ]
           </form>
           </div>', "\n\n");
	
	#
	#
	#

	if ($nickname = $finger{$param->{'user'}})
	{
		open(FINGER, "/usr/bin/finger $nickname -p |")
			or die;

		while ($buf = readline(*FINGER))
		{
			print("$buf<br>");
		}

		close(FINGER);

		#
		#
		#

		if (-e "/home/$nickname/.project")
		{
			print('<br><b>.project:</b><br><br>', "\n");
			printfile("/home/$nickname/.project", $param->{'load'});
		}
		else
		{
			print('No project.</br>', "\n");
		}

		if (-e "/home/$nickname/.plan")
		{
			print('<br><b>.plan:</b><br><br>', "\n");
			printfile("/home/$nickname/.plan", $param->{'load'});
		}
		else
		{
			print('No plan.<br>', "\n");
		}
	}

    #
	print('</td>
           </tr>
           </table>
           </div>', "\n\n");
	footer();
}

#
main();
