#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# source location    : http://www.opbyte.it/grsync/
# last revised       : 25.1.2012

set -x -e -u

softname='grsync'
softversion='1.2.1'
packageversion='72.1'

arch='i486'
cpu='i686'

prefix=/usr

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.gz"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://www.opbyte.it/release/$softname-$softversion.tar.gz

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc/$softname-$softversion
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
      |-----handy-ruler------------------------------------------------------|
grsync: Rsync GUI
grsync: 
grsync: Grsync is a GUI (Graphical User Interface) for rsync, the commandline
grsync: directory synchronization tool. 
grsync: It makes use of  the  GTK  libraries  and  is  released under the GPL
grsync: license, so it is opensource. It is in beta stage and doesn't support
grsync: all of rsync features, but can  be effectively  used  to  synchronize
grsync: local directories. 
grsync: 
grsync: 
grsync: 
grsync:
EOF

# check if package is present
if [ -f $softname-$softversion.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar zxvf $srcpkg

# build
cd $src

    export CFLAGS="-O2 -march=$arch -mtune=$cpu"
    export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

    ./configure \
      --prefix=$prefix \
      --mandir=/usr/man \
      --program-prefix="" \
      --program-suffix="" \
      --enable-unity="no" \
      --build=$arch-slackware-linux || exit

    make
    make install DESTDIR=$dest

# add 'default' files
    cp -a \
       AUTHORS COPYING ChangeLog INSTALL NEWS README \
       $dest/usr/doc/$softname-$softversion
      
# Compress Man Pages
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# Strip
( cd $dest
  find . | xargs file | grep -e "executable" -e "shared object" | grep ELF | cut -s -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
)

# .desktop file "user"
cat << "EOF" > $dest/usr/share/applications/$softname.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Grsync - Backup tool
Name[fr]=Grsync - Outil de sauvegarde
Exec=grsync
Icon=grsync
Terminal=false
Type=Application
Categories=Application;System;FileTools;
StartupNotify=true
EOF

# .desktop file "root"
cat << "EOF" > $dest/usr/share/applications/$softname-root.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Grsync - Backup tool (root)
Name[fr]=Grsync - Outil de sauvegarde (root)
Exec=gksu grsync
Icon=grsync
Terminal=false
Type=Application
Categories=Application;System;FileTools;
StartupNotify=true
EOF

# Make the package freedesktop compliant
convert $dest/usr/share/pixmaps/$softname.png -resize 48x48 $dest/usr/share/icons/hicolor/48x48/apps/$softname.png

# ZenBuild
cp $buildir/build-$softname.sh $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root:root $dest

cd $dest
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 {} \;

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# md5sum signature
cd $buildir
md5sum $package.txz > $package.md5

# .dep
cd $buildir
echo -e "atk,cairo,expat,fontconfig,freetype,glib2,gtk+2,libX11,libXau,\
libXcomposite,libXcursor,libXdamage,libXdmcp,libXext,libXfixes,libXi,libXinerama,\
libXrandr,libXrender,libpng,libxcb,pango,pixman,xcb-util,zlib" > $package.dep

# src file
echo http://zenwalk.pinguix.com/user-accounts/bip/ISO/xap/grsync/source/build-grsync.sh > $package.src
echo $source >> $package.src

# erase
rm -rf $src
rm -rf $dest

# log ;)
) 2>&1 | tee "$softname-$softversion-build.log"
