#!/bin/bash
# maintainer        : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# package			: viewnior
# homepage			: http://xsisqox.github.com/Viewnior/
# source location	: http://cloud.github.com/downloads/xsisqox/Viewnior/viewnior-1.1.tar.gz
# dependency         : 

set -x -e -u

softname='viewnior'
softversion='1.1'
packageversion='66.1'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
src="$buildir/$softname-$softversion"
srcpkg="$buildir/$softname-$softversion.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://cloud.github.com/downloads/xsisqox/Viewnior/$softname-$softversion.tar.gz

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

# Slack-desc
cat <<EOF > $dest/install/slack-desc 
        |-----handy-ruler------------------------------------------------------|
viewnior: viewnior - An image viewer program
viewnior:
viewnior: Created to be simple, fast and elegant. It's minimalistic interface 
viewnior: provides more screenspace for your images.
viewnior: 
viewnior:
viewnior:
viewnior:
viewnior:
viewnior: 
viewnior:
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 xzvf $srcpkg
cd $src	

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

./configure \
	--program-prefix="" \
	--program-suffix="" \
	--prefix="/usr" \
	--sysconfdir="/etc" \
	--localstatedir="/var" \
	--mandir="/usr/man" \
	--infodir="/usr/info" \
	--localedir="/usr/share/locale" \
	--enable-static="no" \
	--enable-shared="yes" \
	--enable-wallpaper="yes" \
	--build=$arch-slackware-linux

make
make install DESTDIR=$dest

cp ../doinst.sh $dest/install

# add 'default' files
    cp -a \
      AUTHORS COPYING ChangeLog-20090517 INSTALL NEWS README TODO\
      $dest/usr/doc/$softname-$softversion
	chmod 644 $dest/usr/doc/*
	
# Strip Executables
( 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
)

# ZenBuild
cd $buildir
cp 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

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# dep file
cd $buildir
echo -e "atk,cairo,cxxlibs|gcc-g++,dbus,dbus-glib,expat,fontconfig,\
freetype,gcc,gconf,glib2,gtk+2,libX11,libXau,libXcomposite,libXcursor,\
libXdamage,libXdmcp,libXext,libXfixes,libXi,libXinerama,libXrandr,libXrender,\
libpng,libxcb,orbit2,pango,pixman,zlib" > $package.dep

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

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

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