#!/usr/bin/perl # Emulate -c option of gpsdrive to jump to the waypoint @NEW_ARGS = ("-G", "1200x580", "-p", "-B", ""); # Set defaults here. # -B '' to stop launching gpsd $RC_DIR = "$ENV{HOME}/.gpsdrive"; $RC_PATH = "$RC_DIR/gpsdriverc"; $WP_PATH = "$RC_DIR/way.txt"; sub coord_from_waypoint { my($wp_name) = @_; open(IN, $WP_PATH) or die "$WP_PATH: $!"; while () { my($n, $lat, $lon) = split; $n eq $wp_name and return ($lat, $lon); } close(IN); (); # not found } sub embed_coord { my($lat, $lon) = @_; rename($RC_PATH, "$RC_PATH~") or die "Renaming $RC_PATH: $!"; open(IN, "$RC_PATH~") or die "$RC_PATH~: $!"; open(OUT, ">$RC_PATH") or die "$RC_PATH: $!"; while () { s/^lastlong = ([0-9.])*/lastlong = $lon/; s/^lastlat = ([0-9.])*/lastlat = $lat/; print OUT; } close(OUT); close(IN); } sub main { while (my $arg = shift(@ARGV)) { if ($arg eq "-c") { my $wp_name = shift(@ARGV); my($lat, $lon) = coord_from_waypoint($wp_name) or die "$wp_name not found in $WP_PATH"; embed_coord($lat, $lon); } else { push(@NEW_ARGS, $arg); } } chdir($RC_DIR); # to see way.txt exec("/usr/bin/gpsdrive", @NEW_ARGS); } main;