#!/bin/bash
# This is a script to build a Zenwalk package. Check the Zenwalk homepage: www.zenwalk.org
#
# SOFTWARE			: googleearth
# MAINTAINER       	: skywatcher (s.skywatcher|AT|gmail|DOT|org, simone antoniucci)
# LATEST REVISION	: 2011-01-15
# BUILD-ONLY DEPS	: -
# NOTES				: -

### Checking for unbound variables and exit on errors
set -u -e

### Repo
URL=http://zenwalk.pinguix.com/user-accounts/skywatcher

### Pkg variables
NAME="googleearth"
VERSION="6.0.1.2032"
ARCH="i686"
CPU="i686"
BUILD="70.1"
EXT="txz"
HOMEPAGE="http://earth.google.com"
SRCLNK="http://dl.google.com/earth/client/current/GoogleEarthLinux.bin"
DESCRIPTION="sat images, maps and google searches"
DOCS="README.linux"
ADDSRC=""
INCSRC=""
ADDDEP="curl,cxxlibs,cyrus-sasl,expat,fontconfig,freetype,gcc,glib2,libICE,libSM,libX11,libXau,libXdamage,libXdmcp,libXext,libXfixes,libXrender,libXxf86vm,libdrm,libidn,libpng,libxcb,mesa,openldap-client,openssl,util-linux-ng,zlib"
XCLDEP=""

### Script variables
CWD=`pwd`
PKG=$NAME-$VERSION-$ARCH-$BUILD
URL=$URL/$NAME/$VERSION
PKGDIR=$CWD/$NAME-install
SRCDIR=$CWD/$NAME-$VERSION
SRCPKG=`echo $SRCLNK | cut -d\  -f1 | rev | cut -d/ -f1 | rev`

### Flags and jobs
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
NJOBS="-j4"

### Cleaning
rm -rf $PKGDIR
rm -rf $SRCDIR

### Making default package dirs
install -dv $SRCDIR $PKGDIR/{install,usr/src/$NAME-$VERSION}

### Getting sources
for src in $SRCLNK; do \
	source=`echo $src | rev | cut -d/ -f1 | rev` 
	if [ -f $source ]; then
		echo "Source file $source is already present, no need to download it";
	else
		echo "Downloading $source..."
		wget -c -nv $SRCLNK
	fi
done

### Building ===========================================================

### Extracting bin file
sh $SRCPKG --noexec --target $SRCDIR
cd $SRCDIR
install -dv $PKGDIR/usr/share/$NAME
tar xfv $NAME-data.tar -C $PKGDIR/usr/share/$NAME
tar xfv $NAME-linux-x86.tar -C $PKGDIR/usr/share/$NAME

# Repackaging
mv -v googleearth-icon.png googleearth.xpm $PKGDIR/usr/share/$NAME 

# Exec
mv -v bin/$NAME $PKGDIR/usr/share/$NAME
install -dv $PKGDIR/usr/bin
ln -sv "/usr/share/googleearth/googleearth" $PKGDIR/usr/bin/$NAME

# Adding info on data folder for the executable to find it
# cat $TARGET/$NAME | sed '/# Set the home/a\GOOGLEEARTH_DATA_PATH=/usr/share/googleearth/' > $TARGET/$NAME || exit 1

# Icon
install -dv $PKGDIR/usr/share/icons/hicolor/48x48/apps
ln -sv "/usr/share/$NAME/$NAME-icon.png" $PKGDIR/usr/share/icons/hicolor/48x48/apps/$NAME.png

# Desktop entry
install -dv $PKGDIR/usr/share/applications
cat > $PKGDIR/usr/share/applications/$NAME.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

# Mime types
install -dv $PKGDIR/usr/share/mime
cat > $PKGDIR/usr/share/mime/$NAME-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

# Docs, man, info
cd $SRCDIR
install -dv $PKGDIR/usr/doc/$NAME-$VERSION
install -v $DOCS $PKGDIR/usr/doc/$NAME-$VERSION
mv -v $PKGDIR/usr/share/$NAME/gpl.txt $PKGDIR/usr/doc/$NAME-$VERSION

# Unnedeed stuff
rm -fv $PKGDIR/usr/share/$NAME/{preuninstall,setup,postinstall}.sh
rm -fv $PKGDIR/usr/share/$NAME/setup.data
rm -fv $PKGDIR/usr/share/$NAME/linux

### Slack-desc 
#    |-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc << EOF
$NAME: $NAME - $DESCRIPTION
$NAME:  
$NAME: Google Earth combines the power of Google Search with satellite 
$NAME: imagery, maps, terrain and 3D buildings to put the world's
$NAME: geographic information at your fingertips.
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: $HOMEPAGE
EOF

# check lsb compliance of the system

   #~ Google Earth 6 is "LSB compliant" meaning it was built on a LSB system. 
   #~ Zenwalk however does not have that symlink which is part of the LSB 3.0 
   #~ specification. We need to create the symlink:
   #~ 
   #~ 			ln -sf /lib/ld-linux.so.2 /lib/ld-lsb.so.3

# create link for lsb compliance of the system
install -dm755 $PKGDIR/lib
ln -sf "/lib/ld-linux.so.2" "$PKGDIR/lib/ld-lsb.so.3"
   
### Doinstall

# lsb & mime & desktop 
cat > $PKGDIR/install/doinst.sh << EOF
# Update mime and desktop database
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
EOF

# ======================================================================

### Managing and zipping man and info files
if [ -d $PKGDIR/usr/man ]; then
	find $PKGDIR/usr/man -type f -exec gzip -9 -v {} \;
fi
if [ -d $PKGDIR/usr/info ]; then
	find $PKGDIR/usr/info -type f -exec gzip -9 -v {} \;
fi

### Adding build-file and included stuff to the package
for file in $0 $INCSRC
	do cp -av $CWD/$file $PKGDIR/usr/src/$NAME-$VERSION
done
chmod a+x $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh

### Stripping binaries
cd $PKGDIR
set +e
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

### Ownerships and permissions
cd $PKGDIR
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 {} \;
set -e

### Making the package
makepkg -l y -c n $CWD/$PKG.$EXT
cd $CWD

### md5
md5sum $PKG.$EXT > $PKG.md5

### dep
ADD=$ADDDEP EXCLUDE=$XCLDEP requiredbuilder -z $PKG.$EXT > $PKG.dep

### slackdesc
cat $PKGDIR/install/slack-desc > $PKG.txt

### src
echo $SRCLNK > $PKG.src
for link in $0 $ADDSRC 
	do echo $URL/$link >> $PKG.src
done

### Cleaning
DEL_SRCDIR=${DEL_SRCDIR-1}
if [ "$DEL_SRCDIR" -eq "1" ]; then
	rm -rf $SRCDIR
	rm -rf $PKGDIR
fi

### Done. Ciao!
