#!/bin/bash

# Packager : Jean-Philippe Guillemin <jp.guillemin@free.fr>
# Sources for perl : http://www.perl.com
# Sources for modules : http://search.cpan.org


VERSION='0.05'
BUILD='1jp'
NAME='perl-Gtk2-Notify'

arch=${arch:-x86_64}
case "$arch" in
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
             SLKLDFLAGS=""
             LIBDIRSUFFIX=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib${LIBDIRSUFFIX}64"
             LIBDIRSUFFIX="64"
             ;;
esac

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export LDFLAGS="$SLKLDFLAGS"

perlversion="5.18.1"

PKGBUILD=$(pwd)
TMP="$PKGBUILD/tmp"
rm -rf $TMP
mkdir -p $TMP
PKG="/tmp/$NAME-$VERSION-$arch-$BUILD"
rm -f $PKG.tgz
rm -rf $PKG
mkdir -p $PKG

cd $PKGBUILD



# modules:
sources=" \
Gtk2-Notify "


for source in $sources ; do
  echo -n "Checking $source : "
  cd $PKGBUILD
  tarball="$(ls ${source}*tar* 2>/dev/null)"
  if [ "$tarball" == "" ] ; then
    echo "not found :("
    exit 1
  else
    echo "found $tarball :)"
  fi
done

cd $PKGBUILD
for source in $sources ; do

  cd $PKGBUILD
  tarball="$(ls ${source}*tar* )"

  rm -rf $TMP
  mkdir -p $TMP
  
  tar xvf $tarball -C $TMP
  
  SRC=$(ls -d $TMP/*/)

    cd $SRC
    [ "$source" == "Gtk2-Notify" ] && patch -p1 < $PKGBUILD/patch-Gtk2-Notify-0.7.diff

    chown -R root.root .
    perl Makefile.PL INSTALLDIRS=vendor -y
    
    make -j2 || exit 1
    make install DESTDIR=$PKG || exit 1

    mkdir -p $PKG/usr/doc/$(basename $SRC)
    cp -a \
      AUTHORS COPYING INSTALL NEWS README TODO ChangeLog doc \
      $PKG/usr/doc/$(basename $SRC)
	
	# perllocal is useless for in default distribution package
	find $PKG/usr/lib${LIBDIRSUFFIX}/perl5 -name perllocal.pod | xargs rm -f
	
	# Man pages
	( 
	  mkdir -p $PKG/usr/man
	  mv $PKG/usr/share/man/* $PKG/usr/man
	  rm -rf $PKG/usr/share/man
	  cd $PKG/usr/man
	  find . -name "*.?" | xargs gzip -f -9
	)
	
	
	########
	#( cd $PKG ; makepkg  -l y -c n $PKG.tgz )
	#removepkg $PKG.tgz	
	#installpkg $PKG.tgz
	#rm -f $PKG.tgz
	#########
  cd $PKGBUILD
done


# Strip everything:
( cd $PKG
  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
)


# Adjust owner/perms to standard values:
(
	cd $PKG
	chown -R root:root .
	find . -perm 555 -exec chmod 755 {} \;
	find . -perm 444 -exec chmod 644 {} \;
)
cd $PKGBUILD

# Slack desc

mkdir -p $PKG/install

cat << "EOF" > $PKG/install/slack-desc
perl-Gtk2-Notify:  perl-Gtk2-Notify (perl Gtk2 Notify bindings)
perl-Gtk2-Notify:
perl-Gtk2-Notify: This package contains perl-Gtk2-Notify
perl-Gtk2-Notify: 
perl-Gtk2-Notify: with patch http://pkgs.fedoraproject.org/cgit/
perl-Gtk2-Notify: perl-Gtk2-Notify.git/plain/libnotify.patch?id2=HEAD
perl-Gtk2-Notify:
perl-Gtk2-Notify:
perl-Gtk2-Notify:
perl-Gtk2-Notify:
perl-Gtk2-Notify:
EOF

# Welcome home

mkdir -p $PKG/usr/src/$NAME-$VERSION
cp $PKGBUILD/build-$NAME.sh $PKG/usr/src/$NAME-$VERSION/

# And finally, a package

cd $PKG
makepkg  -l y -c n $PKG.txz

# cleanup
rm -rf $PKG
rm -rf $TMP

