# Copyright 2001-2003 by Jay F. Davis, webtoad@earthlink.net
# For free use under terms of the Perl Artistic License which can be found at www.perl.com.
# Please make a reasonable effort to send changes and improvements to the author.

use Palm::PDB;
use Palm::Raw;
use strict;
use English;

my $pdb = new Palm::PDB;

$pdb->Load("./ScorePlayerDB.pdb");

print $pdb->{"name"} . "\n";
print $pdb->{"type"} . "\n";
print $pdb->{"creator"} . "\n";
#print $pdb->{"version"}."\n";

my $rec; my $newRec; 

# these are the fields from the 2002 Yahoo fantasy download file
my ($id, $team, $lname, $fname, $mname, $status, $pos, $bathand, $throwhand, $games,
	$bat, $run, $hit, $double, $triple, $homer, $rbi, $walk, $hit_by, $so,
	$steal, $caught,  $sac_hit, $sac_fly, $slugging, $on_base, $avg,
	$error, $passed, $inning, $h, $bb, $hit_bat, $k, $r, $er, $wp, $balk,
	$hr, $start, $complete, $shutout, $era, $win, $loss, $save); 


my $data; my $stat_data; my $i; my $offset; my $x;
my $y; my $roster_line; my $work; my $work2; 
my $number; my $u_lname; my $u_fname, my $lineup_line, my $bat_ord;

open STATS, "<./stats_regular_season.txt";

# skip first 3 lines of file:
my $j = 1;
while ($j <= 3) {
    $stat_data = readline(*STATS);
    $j++;
}

foreach $rec (@{$pdb->{records}}) {
#    print $rec->{id} . "\n";

	$y = "y";
        
    while ($y eq "y") {
		$stat_data = readline(*STATS);
	
		($id, $team, $lname, $fname, $mname, $status, $pos, $bathand, $throwhand, $games,
		$bat, $run, $hit, $double, $triple, $homer, $rbi, $walk, $hit_by, $so,
		$steal, $caught,  $sac_hit, $sac_fly, $slugging, $on_base, $avg,
		$error, $passed, $inning, $h, $bb, $hit_bat, $k, $r, $er, $wp, $balk,
		$hr, $start, $complete, $shutout, $era, $win, $loss, $save) = split ',', $stat_data;
		
		if ($status eq "A") {
			$y = "n";
		}
		if (eof STATS) {
			$y = "n";
		}
    }
	
    if (eof STATS) {
		last;
    }

    # strip off eol character from last field:
    $save = substr($save, 0, length($save) - 1);
    
    if ($status eq "I") {
		$status = "Injured";
    }
    elsif ($status eq "A") {
		$status = "Active";
    }
    elsif ($status eq "D") {
		$status = "Deactivated";
    }
    elsif ($status eq "M") {
		$status = "Minor League";
    }
    
    $number = 0;
    open ROSTER, "<./roster.txt";
    while (<ROSTER>) {
		$roster_line = $ARG;
		if (substr($roster_line, 0, 3) eq $team) {
			# get lname
			$work = $roster_line;
			$work =~ s/[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^ ]* ([^. ]*).*/\1/;
			$work =~ tr/\'//d;
			$work =~ tr/,//d;
			$work =~ tr/\xF1//d; # dec 241 (n circumflex)
			$work =~ tr/a-z/A-Z/; 
			$work =~ s/(.*) [A-Z]\. (.*)/\1 \2/; # get rid of middle initial
			chomp $work;
			# get fname
			$work2 = $roster_line;
			$work2 =~ s/[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t([^ ]*) .*/\1/;
			$work2 =~ s/\.//g;
			$work2 =~ tr/a-z/A-Z/;
			chomp $work2;
			#print "work: " . $work . ", lname: " . $lname . "\n";
			$u_lname = $lname;
			$u_lname =~ tr/a-z/A-Z/;
			$u_fname = $fname;
			$u_fname =~ tr/a-z/A-Z/;
			if ($u_lname eq $work && substr($u_fname, 0, 1) eq substr($work2, 0, 1)) {
				$number = $roster_line;
				$number =~ s/[^\t]*\t\s?([0-9]*).*/\1/;
				chomp $number;
				$pos = $roster_line;
				$pos =~ s/.*\[([A-Z0-9]*)\]/\1/;
				chomp $pos;
				#$bathand = $roster_line;
				#$bathand =~ s/[^\t]*\t[^\t]*\t([SRL]).*/\1/;
				#chomp $bathand;
				#$throwhand = $roster_line;
				#$throwhand =~ s/[^\t]*\t[^\t]*\t[SRL]\/([RL]).*/\1/;
				#chomp $throwhand;
				last;
			}
		}
    }
    close ROSTER;

	$bat_ord = 0;
    open LINEUP, "<./lineups.txt";
    while (<LINEUP>) {
		$lineup_line = $ARG;
		if (substr($lineup_line, 0, 3) eq $team) {
			# team is good
			if (substr($lineup_line, 4, 1) eq substr($fname, 0, 1)) {
				# first letter of first name is good
				$work = $lname;
				$work =~ tr/a-z/A-Z/;
				if (substr($lineup_line, 6, length($lname)) eq $work) {
					# last name matches:
					$bat_ord = substr($lineup_line, (7 + length($lname)), 1);
				}
			}
		}
    }
    close LINEUP;

    if ($pos eq "RP" || $pos eq "SP" || $pos eq "P") {
	$pos = "1";
    }
    elsif ($pos eq "C") {
	$pos = "2";
    }
    elsif ($pos eq "1B") {
	$pos = "3";
    }
    elsif ($pos eq "2B") {
	$pos = "4";
    }
    elsif ($pos eq "3B") {
	$pos = "5";
    }
    elsif ($pos eq "SS") {
	$pos = "6";
    }
    elsif ($pos eq "OF" || $pos eq "LF") {
	$pos = "7";
    }
    elsif ($pos eq "CF") {
	$pos = "8";
    }
    elsif ($pos eq "RF") {
	$pos = "9";
    }
    elsif ($pos eq "DH") {
	$pos = "10";
    }
    elsif ($pos eq "NA") {
	$pos = "0";
    }
    
    $offset = length($lname) + length($fname) + (length($pos) - 1) + 5;

#    print "id: " . $id . "\n";
    print "team: " . $team . "   ";
    print "lname: " . $lname . "   ";
    print "position: " . $pos . "   ";
    print "number: " . $number . "   ";
    print "bathand: " . $bathand . "   ";
    print "throwhand: " . $throwhand . "   ";
    print "bat order: " . $bat_ord. "   ";
#    print "ab: " . $bat . "\n";
#    print "hits: " . $hit . "\n";
#    print "hand: " . $hand . "\n";
    print "\n";
    

    # add the data:
    $data = "\x00\x00\x00\x00\x00"; # the 5 hex nulls
    if ($pos eq "1") {
		$data .= "\x03"; # flag 1: pitch and bat stats
    }
    else {
		$data .= "\x01"; # flag 1: bat stats only
    }
    $data .= "\xF0\x7F" . chr($offset);    # flags (2-3: field flags, 4: offset to team name)
    $data .= $lname . "\x00" . $fname . "\x00" . $pos . "\x00" . $team . "\x00";
    $data .= $bat_ord . "\x00" . $bathand . "\x00" . $throwhand . "\x00";
    #$data .= $status . "\x00";
    # . " \x00" . " \x00" . " \x00" . " \x00"; # 2-5: (ph addr city zip): a blank
    $data .= $bat . "\x00" . $number . "\x00"; # 2: Put zero in for player's number
    $data .= $hit . " " . $double . " " . $triple . " " . $run . " ";
    $data .= $games . " " . $start . " " . "0 " . "0 " . "0\x00"; # 3-4: 2 extra zeros really belong?
    $data .= $homer . "\x00" . $rbi . " " . $walk . " ";
    $data .= "0 " . $so . " " . "0 " . $steal . " " . $caught . " "; # 1: IBB
    $data .= "0 " . $hit_by . " " . $sac_fly . " " . $sac_hit . " " . $error . " "; # 1: GIDP
    $data .= "0 " . "0 " . "0 " . $passed . " " . "0 " . "0 " . "0 " . "0\x00";
    if ($pos eq "1") {
	# add pitcher stats:
	$data .= $win . " " . $loss . " " . $games . " " . $start . " " . $complete . " ";
	$data .= "0 " . $shutout . " " . $save . " " . "0 " . "0 " . $inning . " ";
	$data .= "0 " . "0 " . $h . " " . $r . " " . $er . " " . $hr . " " . $hit_bat . " ";
	$data .= $bb . " 0 " . $k . " " . "0 " . $wp . " " . $balk . " " . "0 " . "0\x00";  # 2: IBB
    }
#    if ($INPUT_LINE_NUMBER > 3) {
#	last;
#    }
    $rec->{data} = $data;
    $rec->{attributes}{expunged} = 0;
    $rec->{attributes}{dirty} = 1;
    $rec->{category} = 1;
    $rec->{id} = 0;

#    print $i . "\n";
    $i++;
}

#print $pdb->{appinfo};

$pdb->Write("./new_pdb.pdb");

exit;
