#!/usr/bin/perl

# make Fig file from HTML generated by kh2html.pl
# (Google satellite photos)

# Andrew Daviel, April 2005

$html = shift ;
$size = 15 * 256 ; # 15 * pixelwidth
$base = "http://kh.google.com" ;
$pdepth = 40 ;
print<<EOT;
#FIG 3.2
Landscape
Center
Inches
Letter
100.00
Single
-2
# created by $0
1200 2
EOT
open (IN,$html) or die "expected input filename" ;
while (<IN>) {
  chomp ;
#  if (/src="(.*&t=)([\w]+)"/) {
  while (/src="([^"]+&t=)(\w+)"/g) {
    $opt = $1 ; $file = $2 ; 
    $url = "$base$opt$file" ;
    if (/title="([\w\s\.]+)"/) {
      $title = $1 ; print "# $title\n" ;
    } 
#    print "get $url\n" ;
    $jpg = "$file.jpg" ;
    unless (-r $jpg) {
      system ("wget -O $jpg \"$url\"") ;
    }
    $x1 = $nc * $size ; $x2 = $x1 + $size ;
    $y1 = $nr * $size ; $y2 = $y1 + $size ;
    print "2 5 0 1 0 -1 $pdepth -1 -1 0.000 0 0 -1 0 0 5\n" ;
    print "\t0 $jpg\n" ;
    print "\t$x1 $y1 $x2 $y1 $x2 $y2 $x1 $y2 $x1 $y1\n" ;
    $nc++ ;
  }
  if (/<br>/i) { $nr++ ; $nc = 0 ;}
}

