This file is indexed.

/usr/bin/geo-code is in gpsdrive-scripts 2.10~pre4-6.dfsg-5ubuntu4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/bash

#
#	geo-code: Convert a street address into a latitude/longitude
#
#	Requires: curl; gpsbabel; bash or ksh;
#		  mysql (if using the gpsdrive.sql output option)
#
#	Donated to the public domain by Rick Richardson <rickr@mn.rr.com>
#		http://home.mn.rr.com/richardsons/sw/geo-code
#
#	Use at your own risk.  Not suitable for any purpose.  Not legal tender.
#
#

PROGNAME="$0"

usage() {
	cat <<EOF
Usage:
	`basename $PROGNAME` [options] address citystate_or_zip [country]

		Convert (geocode) a street address into a latitude/longitude.

	`basename $PROGNAME` [options] tele-phone-number

		Convert (geocode) a phone number into a latitude/longitude.

	In either case, the output can be formatted to any of the output
	file types that gpsbabel supports, or directly imported into the
	GpsDrive MySQL waypoint database.

Requires:
	curl		http://curl.haxx.se/
	gpsbabel	http://gpsbabel.sourceforge.net/

Options:
	-o format	Output format, -o? for possibilities [$OUTFMT]
			plus "gpsdrive.sql" for direct insertion into MySQL DB
			plus "latlon" for just Lat<tab>Long.
			plus "map" to display a map.
	-n name		The waypoint name, e.g. Bob's House.  The default
			is the street address.
	-s		Output shortened names (a gpsbabel option)
	-t type		The waypoint type, e.g. house, cache, bar [$CODETYPE]
	-q		Quiet. Do not output address confirmation on stderr.
	-S		Alias for -o gpsdrive.sql
	-a		For SQL, delete existing record only if it matches
			all fields.  Otherwise, delete it if it matches
			just the name and the type. 
	-D level	Debug level
	-U		Retrieve latest version of this script

Countries:
	us, ca, fr, de, it, es, uk

Examples:
	\$ geo-code "123 AnyStreet" 12345 
	123AnyStreet 42.81020 -73.95070 new

	\$ geo-code -t house "123 AnyStreet" 12345 
	123AnyStreet 42.81020 -73.95070 house

	\$ geo-code -n "Bob's House" -t house "123 AnyStreet" 12345 
	BobsHouse 42.81020 -73.95070 house

	\$ geo-code -S -n "Bob" -t house "123 AnyStreet" 12345 
	[waypoint is added to GpsDrive MySQL database]

	\$ geo-code 901-555-1212 
	123AnyStreet 42.81020 -73.95070 new

See Also:
	geo-nearest	http://home.mn.rr.com/richardsons/sw/geo-nearest
	geo-pg		http://home.mn.rr.com/richardsons/sw/geo-pg
EOF

	exit 1
}

#
#       Report an error and exit
#
error() {
	echo "`basename $PROGNAME`: $1" >&2
    remove_cruft
	exit 1
}

#
#       Set default options, can be overriden on command line or in rc file
#
DEBUG=0
OUTFMT=gpsdrive
COUNTRY=us	# us, ca, fr, de, it,es, uk
SQLUSER=gast	# For -o gpsdrive.sql
SQLPASS=gast	# For -o gpsdrive.sql
SQLDB=geoinfo	# For -o gpsdrive.sql
CODETYPE=new
UPDATEcodeURL=http://home.mn.rr.com/richardsons/sw/geo-code
UPDATEcodeFILE=geo-code.new

#
#	Read RC file, if there is one
#
if [ -f $HOME/.georc ]; then
	. $HOME/.georc
fi

#
#       Process the options
#
ADDRESS=
CSZ=
MODE=babel
SQL=0
NAME=
GURL=
QUIET=0
SQLMATCH=type_name
unset OPTIND
while getopts "an:o:qSs:t:D:Uh?" opt
do
	case $opt in
	n)	NAME="$OPTARG";;
	o)	OUTFMT="$OPTARG";;
	s)	BABELFLAGS="$BABELFLAGS -s";;
	S)	OUTFMT="gpsdrive.sql";;
	t)	CODETYPE="$OPTARG";;
	q)	QUIET=1;;
	a)	SQLMATCH=all;;
	D)	DEBUG="$OPTARG";;
	U)	echo "Getting latest version of this script..."
		destdir=`mktemp -d`
		curl -o$destdir/$UPDATEcodeFILE "$UPDATEcodeURL"
		echo "Latest version is in $destdir/$UPDATEcodeFILE"
		exit
		;;
	h|\?)	usage;;
	esac
done
shift `expr $OPTIND - 1`

case "$OUTFMT" in
map)
	MODE=map
	;;
latlon)
	MODE=latlon
	;;
gpsdrive)
	BABELFLAGS=-s
	;;
gpsdrive.sql)
	BABELFLAGS=-s
	OUTFMT=gpsdrive
	MODE=sql
	# DEBUG=1
	;;
\?)
	gpsbabel -? | sed '1,/File Types/d'
	echo	"	gpsdrive.sql         " \
		"GpsDrive direct MySQL database insertion"
	echo	"	latlon               " \
		"Just latitude and longitude, thank you"
	exit
	;;
esac

case "$#" in
3)
	#
	#	street_address citystate_or_zip country
	#
	ADDRESS=$1
	CSZ=$2
	COUNTRY=$3
	address=`echo $ADDRESS | tr ' ' '+'`
	csz=`echo $CSZ | tr ' ' '+'`
	country=`echo $COUNTRY | tr '[A-Z]' '[a-z]'`
	case "$country" in
	us|usa)		country=us;;
	ca)		country=ca;;
	fr)		country=fr;;
	de)		country=de;;
	it)		country=it;;
	es)		country=es;;
	uk)		country=uk;;
	*)		error "Unknown country '$3'";;
	esac
	;;
2)
	#
	#	street-address citystate_or_zip
	#
	ADDRESS=$1
	CSZ=$2
	address=`echo $ADDRESS | tr ' ' '+'`
	csz=`echo $CSZ | tr ' ' '+'`
	country=$COUNTRY
	;;
1)
	#
	# Google-able_phone_or_name
	#
	# first name (or first initial), last name, city (state is optional)
	# first name (or first initial), last name, state
	# first name (or first initial), last name, area code
	# first name (or first initial), last name, zip code
	# phone number, including area code
	# last name, city, state
	# last name, zip code
	#
	ADDRESS=$1
	GURL="http://www.google.com/search?q=$1"
	;;
*)
	usage
esac

if [ "$NAME" = "" ]; then
	NAME="$ADDRESS"
fi

#
#	procedure to make a gpsbabel style file
#
make_style() {
	cat <<EOF
FIELD_DELIMITER		TAB
RECORD_DELIMITER	NEWLINE
BADCHARS		TAB
IFIELD	LAT_DECIMAL, "", "%08.5f"
IFIELD	LON_DECIMAL, "", "%08.5f"
IFIELD	DESCRIPTION, "", "%s"
IFIELD	ICON_DESCR, "", "%s"
EOF
}

#
#	procedure to remove cruft files
#
remove_cruft() {
	for i in $STYLE $COORDS $OUTWAY $MAP $TMP
	do
		[ -f $i ] && rm -f $i
	done
}

#
#	Main Program
#
TMP=`mktemp -d`
STYLE=${TMP}/style
COORDS=${TMP}/coords
OUTWAY=${TMP}/way
MAP=${TMP}/gif
UA="Mozilla/5.0"

if [ "$GURL" != "" ]; then
	#
	# Treat a single command line argument as a phone number and use
	# Google to look up the Yahoo maps link
	#	
	if [ $DEBUG -gt 0 ]; then
		echo "curl $GURL"
	fi
	URL=`   curl -s -A "$UA" "$GURL" \
		| tee $COORDS \
		| sed -n 's#.*href=http://maps.yahoo.com/\([^>]*\)>.*#\1#p' \
		| head -n1 \
		`
	if [ "$URL" = "" ]; then
		error "Unable to lookup telephone number or name with Google"
	else
		URL="http://maps.yahoo.com/$URL"
	fi
else
	#
	#	Fetch a web page which geocode's the lat/lon
	#
	#	<input type=hidden name=slt value="44.496478">
	#	<input type=hidden name=sln value="-93.941013">
	#
	URL="http://maps.yahoo.com/py/maps.py"
	URL="$URL?BFCat="
	URL="$URL&Pyt=Tmap"
	URL="$URL&addr=$address"
	URL="$URL&csz=$csz"
	URL="$URL&country=$country"	# us, ca, fr, de, it,es, uk
	URL="$URL&Get&nbsp;Map=Get+Map"
fi

if [ $DEBUG -gt 0 ]; then
	echo "curl $URL"
fi

if [ $DEBUG -gt 0 ]; then
    filter="tee `mktemp`"
else
    filter=cat
fi
curl -L -s -A "$UA" "$URL" \
| $filter \
| sed -n \
	-e 's/<title>Yahoo! Maps - \([^<]*\)<.*/\1/p' \
	-e 's/.*slt=\([^%]*\).*sln=\([^%]*\).*Create.*/\1 \2/p' \
> $COORDS

#if [ $DEBUG -gt 0 ]; then
#	cp -d $COORDS /tmp/geo.coords
#fi

#
#	Convert the coords, address, and type to the desired
#	output format.
#
get_latlon() {
	read title
	read lat lon
}

get_latlon < $COORDS;

if [ "$title" != ""  -a "$QUIET" != 1 ]; then
	echo "$title" >&2
fi
if [ "$lat" = "" -o "$lon" = "" ]; then
	error "Cannot determine coordinates of that address"
	remove_cruft
	exit
fi

case "$MODE" in
map)
	echo "$lat	$lon"
	scale=10000
	URL="http://www.vicinity.com/gif"
	URL="$URL?&CT=$lat:$lon:$scale&IC=&W=1280&H=1024&FAM=myblast&LB="
	curl -L -s -A "$UA" "$URL" > $MAP
	xv $MAP
	remove_cruft
	exit
	;;
latlon)
	echo "$lat	$lon"
	remove_cruft
	exit
	;;
esac

make_style > $STYLE

echo "$lat	$lon	$NAME	$CODETYPE" \
| gpsbabel $BABELFLAGS -i xcsv,style=$STYLE -f /dev/fd/0 -o $OUTFMT -F $OUTWAY

#
#	Output the data or add it to the MySQL database
#
gpsdrive_add() {
	delcmd="delete from waypoints"
	addcmd="insert into waypoints (name,lat,lon,type,comment)"

	read _name _lat _lon _type

	COMMENT="$ADDRESS"
	if [ "$CSZ" != "" ]; then
		COMMENT="$COMMENT, $CSZ"
	fi

	echo "use $SQLDB;"
	case "$SQLMATCH" in
	all)
		# Must be a complete match to delete existing record
		echo "$delcmd where name='$_name' and type='$_type'"
		echo "and lat='$_lat' and lon='$_lon';"
		;;
	*)
		# Must match only name and type
		echo "$delcmd where name='$_name' and type='$_type';"
		;;
	esac
	echo "$addcmd values ('$_name','$_lat','$_lon','$_type',"
	echo "'$COMMENT');"
}

if [ -f $OUTWAY ]; then
	case "$MODE" in
	sql)
		#
		# add it via mysql
		#
		if [ $QUIET != 1 ]; then
			echo "$NAME $lat $lon $CODETYPE" >&2
		fi
		if [ $DEBUG -gt 0 ]; then
			gpsdrive_add <$OUTWAY
		else
			gpsdrive_add <$OUTWAY | mysql -u$SQLUSER -p$SQLPASS
		fi
		;;
	*)
		#
		# output to stdout
		#
		cat $OUTWAY
		;;
	esac
fi

remove_cruft