#!/bin/bash
# usage:
#   jpg2kmlalbum *.jpg > doc.kml
#
# read out all EXIF coordinates from JPEG pictures
# and create Google Earth photo album
# frank, 7.4.2007

if [ $# -eq 0 ]; then
  echo "usage: jpg2kmlalbum *.jpg > doc.kml"
  exit
fi

imgwidth=800
albumname=Album

# header
cat << EOF
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Folder><name>$albumname</name>
EOF

# elements

for name in "$@"; do 
	coordinates=`jpg2gps "$name"`
	cx=`echo $coordinates | cut -d ',' -f 1`
	cy=`echo $coordinates | cut -d ',' -f 2`
#	res=`jhead "$name" | grep Resolution`
#	sx1=`echo "$res" | cut -b 16- | cut -d x -f 1`
#	sy1=`echo "$res" | cut -b 16- | cut -d x -f 2 | cut -b 2-`
#	sx=`echo $sx1`
#	sy=`echo $sy1`

cat << EOF
	<Placemark>
		<name>$name</name>
		<description><![CDATA[<p><img src="files/$name" width="$imgwidth"></p>]]></description>
		<LookAt><longitude>$cx</longitude><latitude>$cy</latitude></LookAt>
		<Style><IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/pal4/icon46.png</href></Icon></IconStyle></Style>
		<Point><coordinates>$cx,$cy,0</coordinates></Point>
	</Placemark>
EOF

done

#		<description><![CDATA[<p>$name</p><p><a href="$name"><img src="files/$name" width="800"><br>Show image in browser window</a></p>]]></description>


# 		<description><![CDATA[<p>$name</p><p><a href="$name"><img src="files/$name" width="$sx" height="$sy"><br>Show image in browser window</a></p>]]></description>

# trailer
echo '</Folder></kml>'
