#!/afs/athena/contrib/perl/perl
#
# tick
#
# from urlsnarf: $Id: urlsnarf,v 1.3 1994/02/17 05:21:45 condit Exp $
#
# Original Authors (phonebook):
# Allen Condit
# condit@isri.unlv.edu
#
# John Kilburg
# john@cs.unlv.edu
#
# was phonebook script originally
# became tick 1994/07/12 14:40:25
#
# Ramesh Viswanathan
# snoopy@cs.unlv.edu
#
# 26Jul94 fixed up by allen to work
# again because of SECAPL server changes
#

require 'getopts.pl';
require 'sys/socket.ph';
require 'netinet/in.ph';

$month{"Apr"} = 0;
$month{"May"} = 30;
$month{"Jun"} = 61;
$month{"Jul"} = 91;
$month{"Aug"} = 122;
$month{"Sep"} = 153;

$mon{"Apr"} = 4;
$mon{"May"} = 5;
$mon{"Jun"} = 6;
$mon{"Jul"} = 7;
$mon{"Aug"} = 8;
$mon{"Sep"} = 9;

($prog = $0) =~ s,^.*/([^/]*)$,$1,;
&Getopts('shl:');

&Usage if ($opt_h);

if ($opt_l) {
	$log_file = $opt_l;
	open(LOG, ">$log_file") || die "$prog:  $log_file:  $!";
}

$TeamSymbol{"Atlanta"} = "atl";
$TeamSymbol{"Baltimore"} = "bal";
$TeamSymbol{"Boston"} = "bos";
$TeamSymbol{"California"} = "cal";
$TeamSymbol{"Chicago Cubs"} = "chc";
$TeamSymbol{"Chicago White Sox"} = "chw";
$TeamSymbol{"Cincinnati"} = "cin";
$TeamSymbol{"Cleveland"} = "cle";
$TeamSymbol{"Colorado"} = "col";
$TeamSymbol{"Detroit"} = "det";
$TeamSymbol{"Florida"} = "fla";
$TeamSymbol{"Houston"} = "hou";
$TeamSymbol{"Kansas City"} = "kan";
$TeamSymbol{"Los Angeles"} = "los";
$TeamSymbol{"Milwaukee"} = "mil";
$TeamSymbol{"Minnesota"} = "min";
$TeamSymbol{"Montreal"} = "mon";
$TeamSymbol{"NY Mets"} = "nym";
$TeamSymbol{"NY Yankees"} = "nyy";
$TeamSymbol{"Oakland"} = "oak";
$TeamSymbol{"Philadelphia"} = "phi";
$TeamSymbol{"Pittsburgh"} = "pit";
$TeamSymbol{"San Diego"} = "sdg";
$TeamSymbol{"Seattle"} = "sea";
$TeamSymbol{"San Francisco"} = "sfo";
$TeamSymbol{"St Louis"} = "stl";
$TeamSymbol{"Texas"} = "tex";
$TeamSymbol{"Toronto"} = "tor";

$testing = 1;

&GetTeamData("atl", "Atlanta") unless $testing;
&GetTeamData("bal", "Baltimore") unless $testing;
&GetTeamData("bos", "Boston");
&GetTeamData("cal", "California") unless $testing;
&GetTeamData("chc", "Chicago Cubs") unless $testing;
&GetTeamData("chw", "Chicago White Sox") unless $testing;
&GetTeamData("cin", "Cincinnati") unless $testing;
&GetTeamData("cle", "Cleveland") unless $testing;
&GetTeamData("col", "Colorado") unless $testing;
&GetTeamData("det", "Detroit") unless $testing;
&GetTeamData("fla", "Florida") unless $testing;
&GetTeamData("hou", "Houston") unless $testing;
&GetTeamData("kan", "Kansas City") unless $testing;
&GetTeamData("los", "Los Angeles") unless $testing;
&GetTeamData("mil", "Milwaukee") unless $testing;
&GetTeamData("min", "Minnesota") unless $testing;
&GetTeamData("mon", "Montreal") unless $testing;
&GetTeamData("nym", "NY Mets") unless $testing;
&GetTeamData("nyy", "NY Yankees") unless $testing;
&GetTeamData("oak", "Oakland") unless $testing;
&GetTeamData("phi", "Philadelphia") unless $testing;
&GetTeamData("pit", "Pittsburgh") unless $testing;
&GetTeamData("sdg", "San Diego") unless $testing;
&GetTeamData("sea", "Seattle") unless $testing;
&GetTeamData("sfo", "San Francisco") unless $testing;
&GetTeamData("stl", "St Louis") unless $testing;
&GetTeamData("tex", "Texas") unless $testing;
&GetTeamData("tor", "Toronto") unless $testing;

exit(0);

sub GetTeamData {
  local ($symbol, $name) = @_;
  local ($won, $lost);
    $port   = "80";
    $server = "espnet.sportszone.com";
    $path   = "/mlb/sched/$symbol.html";

    # connect to the server
    local($sockaddr,$here,$there,$response,$tries) = ("Snc4x8");
    $here  = pack($sockaddr, &AF_INET, 0, &getaddress("localhost"));
    $there = pack($sockaddr, &AF_INET, $port, &getaddress($server));
    die "socket: $!\n" if (!socket(S,&AF_INET,&SOCK_STREAM,&PF_UNSPEC));
    die "connect: $!\n" if (!connect(S,$there));
    select(S);       $| = 1;		# make unbuffered
    select(STDOUT);

    # send the request
    $qlen = length($query) + 5;

    print S "GET $path HTTP/1.0\r\n\r\n";
    
    # snarf the header stuff
    $_ = <S>;
    while ($_ !~ /^\s*$/) {
	if ($log_file) {
	    print LOG $_;
	}
	$_ = <S>;
    }

#    print "newteam;\n($name) teamname\n";
    # snarf the file
    while (<S>) {
      s/<[^>]*>//g;  # eliminate all markup
      s/\n//;
      if (/([A-Z][a-z][a-z]) ([ 0-9][0-9]).* ([WL]) /)
	{
	  $won++ if ($3 eq "W");
	  $lost++ if ($3 eq "L");
	  $date = $month{$1} + $2;
	  printf("%d %4.1f datum %% %s %3d %3d %5.3f \n",
		 $date, ($won-$lost)/2, 
		 $_, $won, $lost, $won/($won+$lost));
	}
      elsif (/([A-Z][a-z][a-z]) +([0-9]?[0-9]) -- (at )?([^,]*)(,.*)/)
	{
	  $z = ($3 eq "at ") ? "at " : "hosts ";
	  print "SPRTBASEBOS_RSOX $mon{$1}/$2/1996 ${symbol}_vs_$TeamSymbol{$4} $name $z$4$5\n";
	}
    }

#    print "endteam\n\n";

    close(S);
}

sub getaddress {
        local($host) = @_;
        local(@ary);
        @ary = gethostbyname($host);
        return(unpack("C4",$ary[4]));
}


sub Usage {
    close(LOG) if ($log_file);
die <<EOU;
Usage:  $prog [-s] [-l <logfile>] <query>
          -s don't strip the HTML markup codes.
          -l <logfile> dump HTTP status info to logfile

          Query is the string used to locate quotes in the SECAPL
          stock quote server. For example, if the symbol DTLN is
          supplied then the 15-minute delayed quote of Data
          Transmission Corporation is printed.
EOU
    ;
}
