#!/usr/bin/perl -w # # Make an attempt to "guess" at the latitude and longitude of this machine by # checking a few public SOAP services. # # 1) Look up the public facing IP address of this machine # 2) Get the zip code (and a not very good guess latitude and longitude) # 3) Look up the latitude and longitude for that zipcode # 4) Replace the default in the code (Cambridge, MA) with the new info # use SOAP::Lite; sub searchandreplace { # # Several files in the ephem package have the latitude and longitude # for Boston, MA hardcoded. This script changes that data. # my ($lat,$lon,$tz,$dir,$file) = @_; my $filename = substr($dir,length($dir)-1,1) eq "/" ? $dir.$file : $dir."/".$file; print STDERR "cannot find $filename" if ! -f $filename && return 0; print STDERR "illegal latitude: $lat" if $lat !~ /^\s*\-?\d+\.\d+\s*$/ && return 0; print STDERR "illegal longitude: $lon" if $lon !~ /^\s*\-?\d+\.\d+\s*$/ && return 0; rename($filename,$filename . '.bak'); open(FILENAMEOUT,">$filename"); open(FILENAMEIN,"<$filename".".bak"); while (my $line = ) { $line =~ s/42.377834/$lat/; $line =~ s/-71.106323/$lon/; $line =~ s/(timezone\s*=\s*)-5(\.0+)?/${1}${tz}/; $line =~ s/(TOFF\s*=\s*)-5(\.0+)?/${1}${tz}/; print(FILENAMEOUT $line); }; close FILENAMEOUT; close FILENAMEIN; # no need to delete, since this is removed after a clean install anyway # unlink($filename.".bak"); 1; }; my $DEBUG = 0; my @latitude = (); my @longitude = (); my $ip = SOAP::Lite -> service('http://www.showmyip.com/soap/server.php?wsdl') -> showmyip(); print STDERR "Found IP = " . $ip->{'IP'}."\n" if $DEBUG; my $loc = SOAP::Lite -> service('http://www.showmyip.com/soap/lookupserver.php?wsdl') -> showmyip_lookup($ip->{'IP'}); push(@latitude,$loc->{'LATITUDE'}); push(@longitude,$loc->{'LONGITUDE'}); my $timezone = -5.00; if ($loc->{'TIMEZONE'} =~ /^([-+]?)(\d+):(\d+)$/) { # Change timezone from HOURS:MINUTES to Decimal hours # my ($sign,$hours,$minutes) = ($1,$2,$3); $sign = "" if $sign eq "+"; $hours =~ s/^0+//; $minutes /= 60; my $num = ($sign.$hours) + $minutes; $timezone = sprintf("%4.2f",$num); }; if ($loc->{'COUNTRY_CODE3'} eq "USA") { print STDERR "Found zip code = ".$loc->{'POSTAL_ZIP_CODE'}."\n" if $DEBUG; my @q = [$loc->{'CITY'}." ".$loc->{'STATE'}, $loc->{'POSTAL_ZIP_CODE'}]; my $loc2 = SOAP::Lite -> service('http://www.ontok.com/geocode/soap?wsdl') -> geocode(SOAP::Data->name(key => ""), SOAP::Data->name(q => @q)); foreach my $idx (0..scalar(@$loc2)-1) { push(@latitude,@{$loc2}[$idx]->{'lat'}); push(@longitude,@{$loc2}[$idx]->{'long'}); }; }; print STDERR join(" ",@latitude)."\n" if $DEBUG; print STDERR join(" ",@longitude)."\n" if $DEBUG; # # # my $lat = pop(@latitude); my $lon = pop(@longitude); searchandreplace($lat,$lon,$timezone,$ARGV[0],"x10events.sh"); searchandreplace($lat,$lon,$timezone,$ARGV[0],"year.sh"); searchandreplace($lat,$lon,$timezone,$ARGV[0],"ephem.cc"); searchandreplace($lat,$lon,$timezone,$ARGV[0],"today.cc"); searchandreplace($lat,$lon,$timezone,$ARGV[0],"year.cc"); searchandreplace($lat,$lon,$timezone,$ARGV[0],"x10events.cc");