#!/usr/bin/perl
#
# トラックログでお絵描き
# Nowral
# 98.12.17
#
($width,$height)  = (800,600); #モニタ解像度

@filename = @ARGV;
foreach $oldargv (@filename) {
open(ARGVIN, "<$oldargv");

&MacPerl'DoAppleScript(<<END_SCRIPT1);
tell application "KeyQuencer Engine"
   DoScript "Wait 3 seconds"
end tell
END_SCRIPT1

($lat1,$lat2,$lon1,$lon2) = (0,90,180,0);
while (<ARGVIN>) {
    if(/\t(\d+)。(\d+)'(\d+\.\d)"\t(\d+)。(\d+)'(\d+\.\d)"/) {
        $lat = $1 +$2/60 +$3/3600;
        $lon = $4 +$5/60 +$6/3600;
        if($lat > $lat1) { $lat1 = $lat; }
        if($lat < $lat2) { $lat2 = $lat; }
        if($lon < $lon1) { $lon1 = $lon; }
        if($lon > $lon2) { $lon2 = $lon; }
    }
}

$mag  = $width  * 0.8 / ($lon2-$lon1);
$mag1 = $height * 0.8 / ($lat1-$lat2);
if($mag > $mag1) { $mag = $mag1; }

$ox = ($width  - $mag*($lon2-$lon1)) / 2;
$oy = ($height - $mag*($lat1-$lat2)) / 2;

print $lat1,",",$lon1,",",$mag,",",$ox,",",$oy,"\n";

#重ね書き用、パラメタ固定
#($lat1,$lon1,$mag,$ox,$oy) = (43.9136944444444,144.156638888889,23971.4625446155,120,193.7931034481);
#print $lat1,",",$lon1,",",$mag,",",$ox,",",$oy,"\n";

$trn = -1;
open(ARGVIN, "<$oldargv");
llp:while (<ARGVIN>) {
    if(/^(\w+)\t(\d+)\t.*\t(\d+)。(\d+)'(\d+\.\d)"\t(\d+)。(\d+)'(\d+\.\d)"/) {

        $lat = $3 +$4/60 +$5/3600;
        $lon = $6 +$7/60 +$8/3600;
        $x2 = int( ($lon - $lon1) * $mag + $ox +0.5 );
        $y2 = int( ($lat1 - $lat) * $mag + $oy +0.5 );

        if($1 eq "Wx") { ($x1,$y1) = ($x2,$y2); }

        if($trn == $2 || $1 ne "T") {
        print $x1,",",$y1,",",$x2,",",$y2,"\n";

&MacPerl'DoAppleScript(<<END_SCRIPT2);
tell application "KeyQuencer Engine"
   DoScript "Drag from $x1 $y1 to $x2 $y2 slower"
end tell
END_SCRIPT2

        }
        else { $trn = $2; }

        ($x1,$y1) = ($x2,$y2);
    }
}

&MacPerl'DoAppleScript(<<END_SCRIPT3);
tell application "KeyQuencer Engine"
   DoScript "MouseJump 0 0 center"
  DoScript "Play all"
end tell
END_SCRIPT3

}
&MacPerl'Quit(2);
die "The Unhappy End";