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

softname='grsync'
softversion='0.6.3'
packageversion='1fb'

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
dest="$buildir/$package"
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: 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 source 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="" \

    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 "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
)

# .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
NotShowIn=KDE
EOF

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

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

# SalixBuild
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,zlib" > $package.dep

# .src 
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/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"
