#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage           : 
# source location    : http://cpan.fluoline.net/authors/id/P/PA/PARDUS/File-DesktopEntry-0.04.tar.gz
# last revised       : 28.9.2009
# dep                : 

softname='perl-File-DesktopEntry'
softnamesrc='File-DesktopEntry'
softversion='0.04'
packageversion='1fb'

arch='noarch'
cpu='i686'

prefix=/usr

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
src="$buildir/$softnamesrc-$softversion"
srcpkg="$buildir/$softnamesrc-$softversion.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://cpan.fluoline.net/authors/id/P/PA/PARDUS/$softnamesrc-$softversion.tar.gz

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc 
                      |-----handy-ruler------------------------------------------------------|
perl-File-DesktopEntry: perl-File-DesktopEntry (Object to handle .desktop files)
perl-File-DesktopEntry: 
perl-File-DesktopEntry: This module is used to work with .desktop files. The format of these
perl-File-DesktopEntry: files is specified by the freedesktop Desktop
perl-File-DesktopEntry: Entry specification.
perl-File-DesktopEntry: This module can parse these files but also knows how to run the
perl-File-DesktopEntry: applciations defined by these files.
perl-File-DesktopEntry: For this module version 1.0 of the specification was used.
perl-File-DesktopEntry: This module was written to support File::MimeInfo::Applications.
perl-File-DesktopEntry: 
perl-File-DesktopEntry: 
EOF

# check if source is present
if [ -f $softnamesrc-$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 
make
make install DESTDIR=$dest
 
# add 'default' files
	cd $src
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      Changes MANIFEST README \
      $dest/usr/doc/$softname-$softversion
	
#  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
)

# compress man pages
( mv $dest/usr/share/man $dest/usr/man
  cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
  rm -rf $dest/usr/share
)

# SalixBuild
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
cd $buildir
echo -e "perl,perl-File-BaseDir" > $package.dep

# .src
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/l/perl-file-desktopentry/build-perl-File-DesktopEntry.sh > $package.src
echo $source >> $package.src

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

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