#Maintainer: Maximus (maximuus[at]gmail[dot]com)
#Based on Slackbuild by Michiel van Wessem, and Zenwalk build
#script by 'skywatcher'

pkgname=googleearth
pkgver=5.1.3509.4636.1
pkgrel=1mb
arch=i486
source=(http://dl.google.com/earth/client/current/GoogleEarthLinux.bin)
sourcetemplate=http://people.salixos.org/maximuus/$pkgname/$pkgver/
#docs=("README.linux" "gpl.txt")
url=http://earth.google.com/index.html
#dotnew=()
#CFLAGS=
#CXXFLAGS=
#options=('noextract')

doinst() {
		if [ -x usr/bin/update-desktop-database ]; then
			usr/bin/update-desktop-database -q ./usr/share/applications >/dev/null 2>&1
		fi
		if [ -x usr/bin/update-mime-database ]; then
			usr/bin/update-mime-database ./usr/share/mime >/dev/null 2>&1
		fi		
}

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"$pkgname - a virtual globe, map and geographic information program"
"Google Earth lets you fly anywhere on Earth to view satellite imagery,"
"maps, terrain, 3D buildings, from galaxies in outer space to the"
"canyons of the ocean. You can explore rich geographical content, save"
"your toured places, and share with others."
"NOTE: This program may not be stable on machines with certain"
"combinations of hardware. It may crash itself and/or cause X to crash"
)


build() {
	# Copyright 2007-2009  Michiel van Wessem, Manchester, United Kingdom
	# All rights reserved.
	#
	# Redistribution and use of this script, with or without modification, is
	# permitted provided that the following conditions are met:
	#
	# 1. Redistributions of this script must retain the above copyright
	#    notice, this list of conditions and the following disclaimer.
	#
	# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
	# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
	# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
	# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
	# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
	# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
	# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
	# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	#
	# Thanks to rworkman for the additional code and script cleanups
	# and to Daniel de Kok and Alan_Hicks for their comments.
	
	#create directory structure
	mkdir -p $startdir/pkg/usr/{bin,doc,share}
	mkdir $startdir/pkg/usr/share/{applications,googleearth,mime}
	mkdir -p $startdir/pkg/usr/share/icons/hicolor/48x48/apps
	
	#extract source
	sh $startdir/src/GoogleEarthLinux.bin --noexec --target $startdir/pkg/usr/share/googleearth
	cd $startdir/pkg/usr/share/googleearth
	tar xf googleearth-data.tar
	tar xf googleearth-linux-x86.tar
	rm {googleearth-linux-x86.tar,googleearth-data.tar}
	
	#move docs
	mv {README.linux,gpl.txt} $startdir/pkg/usr/doc
	
	#move, modify and link executable
	cat bin/googleearth | sed '/# Set the home/a\GOOGLEEARTH_DATA_PATH=/usr/share/googleearth/' > googleearth || exit 1
	rm -rf bin 
	cd $startdir/pkg/usr/bin
	ln -s ../share/googleearth/googleearth googleearth || exit 1
	
	#move icon
	mv $startdir/pkg/usr/share/googleearth/googleearth-icon.png $startdir/pkg/usr/share/icons/hicolor/48x48/apps/googleearth.png
	
	#strip binaries
	cd $startdir/pkg
	find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	
	#create .desktop
cat > $startdir/pkg/usr/share/applications/googleearth.desktop << EOF
[Desktop Entry]
Encoding=UTF-8
Name=Google Earth
GenericName=3D planet viewer
Comment=Explore, search and discover the planet
Exec=googleearth %f
Terminal=false
MultipleArgs=false
Type=Application
Icon=googleearth
Categories=Application;Network
MimeType=application/vnd.google-earth.kml+xml;application/vnd.google-earth.kmz;application/earthviewer;application/keyhole
EOF

	#add mime info
cat > $startdir/pkg/usr/share/mime/googleearth-mimetypes.xml << EOF
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="application/vnd.google-earth.kml+xml">
    <comment>Keyhole Markup Language data</comment>
    <glob pattern="*.kml"/>
  </mime-type>

  <mime-type type="application/vnd.google-earth.kmz">
    <comment>Keyhole Markup Language archive</comment>
    <glob pattern="*.kmz"/>
  </mime-type>

  <mime-type type="application/keyhole">
    <comment>Keyhole Markup Language data</comment>
  </mime-type>

  <mime-type type="application/earthviewer">
    <comment>Keyhole Markup Language data</comment>
  </mime-type>
</mime-info>
EOF

	#verify permissions
	cd $startdir/pkg
	chown -R root:root .
	find . -perm 666 -exec chmod 644 {} \;
	find . -perm 664 -exec chmod 644 {} \;
	find . -perm 600 -exec chmod 644 {} \;
	find . -perm 444 -exec chmod 644 {} \;
	find . -perm 400 -exec chmod 644 {} \;
	find . -perm 440 -exec chmod 644 {} \;
	find . -perm 777 -exec chmod 755 {} \;
	find . -perm 775 -exec chmod 755 {} \;
	find . -perm 511 -exec chmod 755 {} \;
	find . -perm 711 -exec chmod 755 {} \;
	find . -perm 555 -exec chmod 755 {} \;
	find . \( -name "*.png" -o -name "*.kml" -o -name "*.xml" \) -exec chmod 644 {} \;


	#remove fluff
	rm -f $startdir/pkg/usr/share/googleearth/{preuninstall,setup,postinstall}.sh
	rm -rf $startdir/pkg/usr/share/googleearth/setup.data
	rm -rf $startdir/pkg/usr/share/googleearth/linux
}