#!/usr/bin/perl
	
	$wget_loc = "fetch";

	# used to have some "which" here, replaced it..

	$home = (getpwuid($<))[7];
	$ok = chdir() || chdir($home);

# ** PROBABLY DEPRECATED, WMSTOCK NOW CHECKS THIS FOR US.
#make the dir if its not there
#	if ( !(-e ".wmstock") )
#	{
#		mkdir(".wmstock", 0775);
#	}


	chdir(".wmstock");

	if ($ARGV[0] eq "-newversion")
	{
		&look_for_new_version($ARGV[1]);
		exit(0);
	}

	else 
	{
		@h = (  "Symbol","Name","Last","Trade Date","Trade Time","Change","% Change",
		"Volume","Avg. Daily Volume","Bid","Ask","Prev. Close","Open",
		"Day's Range","52-Week Range","EPS","P/E Ratio","Div. Pay Date",
		"Div/Share","Div. Yield","Mkt. Cap"  );

		$count=0;
		@symbols = @ARGV;
# what was i thinking here?
#		system("touch /tmp/wmstock");
		&get_quote_data();
#		system("rm -r /tmp/wmstock");
		exit(0);
	}
		
sub get_quote_data
{
	# symbol = 		0
	# name = 		1
	# last_price = 		2
	# last trade date = 	3
	# last trade time =	4	 
	# change = 		5
	# %change = 		6
	# volume = 		7
	# avg volume = 		8
	# bid = 		9
	# ask = 	       10
	# prev. close =        11
	# open = 	       12 
	# day's range =        13
	# 52wk range =         14
	# EPS =		       15
	# P/E Ratio =	       16
	# Div Pay Date =       17
	# Div/Share =	       18
	# Div Yield =	       19
	# mkt cap =	       20

	$URL = "http://quote.yahoo.com/d?f=snl1d1t1c1p2va2bapomwerr1dyj1&s=";
	my($x,@q,@qr,$ua,$url);
	$x = $";
	$" = "+";
	$URL.="@symbols";
	$" = $x;

	#
	#  I think some of these wget command line options may cause problems
	#  for some people? Dont know why... (Perhaps they have a ~/.wgetrc file
	#  that overrides command line options?).
	#

	$tempfile = "stock.tmp.$$";
	$wget_args = " -p -o $home/.wmstock/$tempfile \"$URL\"";
	$command = $wget_loc.$wget_args;
	$result = system ("$wget_loc$wget_args");
	if ($result != 0) {
		die "I could not execute $wget_loc.  Is wget installed and in your path???\n result = $result\n";
	}
	
	sleep(3);

	# Parse HTML File. 
	#
	open(TmpFile, "<$tempfile") || die "Couldn't open temporary html file $tempfile\n";
	@filedata = <TmpFile>;
	close (TmpFile);

	$count = 0;
	foreach $line (@filedata)
	{ 
		$wmfile = "$ARGV[$count].dat";
		open (WMFILE, ">$wmfile") or die "$0:: Unable to create $wmfile.";

		@fields = split /,/, $line;
		foreach $ed (@fields)
		{
			if ($ed=~/^\"(.+)"$/)
			{
				$ed = $1; 
			}
			print WMFILE "$ed\n";
		}
		close (WMFILE);
		$count++;
	}
	unlink($tempfile);
}

sub look_for_new_version
{
        $version = $_[0];
	$vtempfile = "newest";
        $vers_url="http://www.mattfischer.com/wmstock/src/$vtempfile";
	$wget_args = " -p -o $home/.wmstock/$vtempfile \"$vers_url\"";

        $command = $wget_loc.$wget_args;

	print "Please wait while I check for a new version.\n";
	system ("$command") == 0
                or die "I could not execute $wget_loc.  Is wget installed and in your path???\n";
        sleep(2);

	open (VFILE, "<$vtempfile") || die "Could not open the version file.  Are you connected to the Internet?\n";
	@fd = <VFILE>;
	close (VFILE);
	unlink ($vtempfile);

	print "Finished.\n";

	if ($fd[0]=~/(\d\.\d\d)/)
	{
		if ($1 > $version)
		{
			print "There is a new version available!  You have version $version and the newest is version $1.\nPlease go to http://www.mattfischer.com/wmstock to get the newest release.\n\n";
		}
		elsif ($1 == $version)
		{
			print "Your version is up to date.\n\n";	
		}
		else 
		{
			print "Either I am confused or you appear to have a version greater than the current release.\n\n";
		}
			
	}
	else 
	{
		print "I could not read any data from $vtempfile.\nPerhaps http://www.mattfischer.com is down.\n";
	}
}

