#!/bin/bash

# small script to extract GPS coordinates from an image and
# call firefox with google maps url if the GPS info exists
#
# 2015-2023 Andy Spiegl - GPL
# ORIGINAL IDEA (python version): 2012 Thomas Wiegner - GPL

# my own program name
self=$(basename $0)

# defaults
#BROWSER="firefox"
BROWSER="sensible-browser"
#MAPSURL="http://maps.google.de/maps?q="
MAPSURL="https://www.google.de/maps/place/"

# syntax help
Usage()
{
  echo "Usage: $self [--browser <BROWSER>] [--mapsurl <URLtoMAPSapp>] IMAGE_WITH_GPS_EXIFDATA"
  echo "          [-h|--help]"
  echo "Extracts GPS data from image and calls Google maps in webbrowser."
  exit $1
}

# process params
while [ $# -gt 0 ]
do
  case $1 in
    --browser)     shift; BROWSER="$1";  shift; continue;;
    --mapsurl)     shift; MAPSURL="$1";  shift; continue;;
    -h | --help)  Usage 0;;
    -*) Usage 1;;
    *) imgfile="$1"; shift; continue;;
  esac
  break
done

# run exiftool to extract GPS coords
COORDS=$( exiftool -t -GPSPosition -c "%d°%d'%.3f''" "$imgfile" | sed -e"s|.*	||" -e"s| ||g" )
#echo GPS coords in "$imgfile": $COORDS
echo GPS coords: $COORDS

# check for errors
errstat=$?
if [ $errstat != 0 ]; then
  echo "Error $errstat while calling exiftool and sed: $!"
fi

# send it to the webbrowser (avoid glib error messages)
$BROWSER "${MAPSURL}${COORDS}" 2> /dev/null

exit
