#!/usr/bin/perl
# mb2gpsdrive 0.0.5	fetches mapblast directions and builds a gpsdrive
# 			waypoint file
# 			
# Copyleft 2002 by Sven Fichtner <sven.fichtner@flugfunk.de>
# 
# Some code is stolen from mb2route written by Doug Rinckes. Thanks Doug! ;)
# See http://opencast.mine.nu/mod.php?mod=userpage&menu=9&page_id=4 for
# further information about mb2route
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# changelog:
# who				when		what
# ############################################################################
# sven.fichtner@flugfunk.de	2002-05-07	- removed the comment line from
# 						  output file as it would be
# 						  recognized as a waypoint
# sven.fichtner@flugfunk.de	2002-05-12	- got some hints from
# 						  kevin stephens about how
# 						  to fetch the data. thanks!
# sven.fichtner@flugfunk.de	2002-05-14	- added -D -A -S
# 						- screwed up mapblast
# 						  several times.. ;)
# sven.fichtner@flugfunk.de	2002-05-15	- LC_ALL-patch applied
# 						- PATHCALC SRVR Connection
# 						  timed out will be reported
# 						  to the user when mapblast
# 						  ends up with this error.

# Fritz reported problems when LC_ALL is set to de_DE
system("set LC_ALL=en_US");

require "getopts.pl";

&Getopts('hn:o:v:d:s:a:e:D:A:S:');

# syntax checking section
if ($opt_h eq 1) {
	printf("mb2gpsdrive.pl:         uses mapblast directions to build\n");
	printf("                        a gpsdrive waypoint file\n\n");
	printf("  -h		        the help screen..you just found it.\n");
	printf("  -d <departure city>	Start your route here..\n");
	printf("  -D <departure street>	Street name of the first waypoint\n");
	printf("  -a <arrival city>	..and finish it there.\n");
	printf("  -A <arrival street>	Street name of the last waypoint\n");
	printf("  -s <country code>	See below and README.mb2gpsdrive\n");
	printf("  -S <country code>	If the city specified with -a is in a different\n");
	printf("			country than -s, enter it here.\n");
	printf("  -n <route name>	Prefix all waypoints with this value (prevents\n");
	printf("			waypoints overwriting each other)\n");
	printf("  -o <output file>	Writes the route data to this file\n");
	printf("\n");
	printf("If the -o option is not given, you will find the result \n");
	printf("in /var/tmp/way.txt\n\n");
	printf("Those are some possible country codes (case DOES matter)\n");
	printf("See README.mb2gpsdrive for all country codes.\n\n");
	printf("United States - US\n");
	printf("Germany - DEU\n");
	printf("Austria - AUT\n\n");
	exit;
}

# now assign all the variables from the parameters
if ($opt_n eq "") {
	die "You must supply a route name. (hint: -n)";
} else {
	$routenum=$opt_n;
}
if ($opt_d eq "") {
	die "You must supply the city where you want to start. (hint: -d)";
} else {
	$startcity=$opt_d;
}
if ($opt_a eq "") {
	die "Where do you want to go today? [tm] (hint: -a)";
} else {
	$endcity=$opt_a;
}
if ($opt_s eq "") {
	die "You must enter the state where you want to start";
} else {
	$startstate=$opt_s;
}
if ($opt_S eq "") {
	$endstate=$opt_s;
} else {
	$endstate=$opt_S;
}

$startstreet=$opt_D;
$endstreet=$opt_A;
$outfile=$opt_o;

# this is just for debugging.

printf("route name:	$routenum\n");
printf("start city:	$startcity\n");
printf("start street:	$startstreet\n");
printf("start state:	$startstate\n");
printf("end city:	$endcity\n");
printf("end street:	$endstreet\n");
printf("end state:	$endstate\n");


# fill out mapblasts form

open(WGET,"wget -O /dev/null -v \"http://www.mapblast.com/myblast/driveSilo.mb?remLoc_1=&AD4_1=$startstate&AD2_street_1=$startstreet&AD3_1=$startcity&apmenu_1=&apcode_1=&GAD1_1=&GAD2_1=&GAD3_1=&GAD4_1=&phone_1=&IC_1=&CT_1=&GMI_1=&remLoc_2=&AD4_2=$endstate&AD2_street_2=$endstreet&AD3_2=$endcity&apmenu_2=&apcode_2=&ndrive=1&GAD1_2=&GAD2_2=&GAD3_2=&GAD4_2=&phone_2=&IC_2=&CT_2=&GMI_2=&locBoxNum=&req_action=getdir&AD2=&AD3=&AD4=&formnum=2&forceAddr=&CT=&GMI=&multiIC=&locn_ckieVal=&serch=&skip=&CMD=FILL\" 2>&1 |")  or die;

while (<WGET>) {
	next unless (/^Location/);
	$_ =~ /\s(http[^\s]+)\s/;
	$URL = $1;
}

close(WGET);
$URL .= '&showman=1';

# get the page showing step-by-step maps
system("wget -q -O /var/tmp/formfilled.html \"$URL\"");

# check we can open the files
open(HTML,"/var/tmp/formfilled.html") || die "open formfilled.html failed";
if ($outfile eq "") {
	open(ROUTE,">/var/tmp/way.txt") || die "open temporary file $route failed";
} else {
	open(ROUTE,">$outfile") || die "open outfile $outfile failed";
}

$waypoint=0;
# for each line in the html output...
while (<HTML>) {
	chop($_);
	# if it is a waypoint...
	if (/<a href="\/myblast\/map.mb\?\&CT=.*IC=([+-]?\d+\.\d+)\:([+-]?\d+\.\d+)\:\d+\:\d+\:([+-]?\d+\.\d+)\:([+-]?\d+\.\d+)/) {
		# get the position of this waypoint ($1 and $2) and the next one ($3 and $4)
		$north = $1; $east = $2; $north1 = $3; $east1 = $4;
		# put this waypoint into the route file
		printf(ROUTE "$routenum\_$waypoint $north $east\n");
		$waypoint ++;
	} elsif (/PATHCALC SRVR Connection timed out/) {
		printf("\nMapBlast! reported an error:\n");
		printf("PATHCALC SRVR Connection timed out\n\n");
		$mberror=1;
	}
}
# put the last waypoint into the route file
printf(ROUTE "$routenum\_$waypoint $north1 $east1\n");
close(HTML);
close(ROUTE);

if ($mberror == 1) {
	print("Please rerun mb2gpsdrive");
	exit;
}

if ($waypoint == 0) {
	die "No waypoints found in HTML file";
}

printf("$waypoint waypoints written\n");

if ($outfile eq "") {
	print("you will find the waypoints in /var/tmp/way.txt\n");
}

