#!/bin/bash

# Packager : Jean-Philippe Guillemin <jp.guillemin@free.fr>
# sources location : 


softversion='0.98.2'
packageversion='1jp'
softname='murrine-engine'
arch=${arch:-x86_64}

PKGBUILD=$(pwd)

package="$softname-$softversion-$arch-$packageversion"

PKG="/tmp/$package"
rm -rf $PKG
rm -f $PKG.tgz
mkdir -p $PKG

TMP="$PKGBUILD/tmp"

cd $PKGBUILD

sources="murrine"


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

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
    EXTRACONFIG=""
    chown -R root.root .
    find . -perm 777 -exec chmod 755 {} \;
    find . -perm 775 -exec chmod 755 {} \;
    find . -perm 666 -exec chmod 644 {} \;
    find . -perm 664 -exec chmod 644 {} \;

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

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

    ./configure --prefix=/usr \
      --libdir=/usr/lib${LIBDIRSUFFIX} \
      --mandir=/usr/man \
      --docdir=/usr/doc/$(basename $SRC) \
      --sysconfdir=/etc/X11 \
      --enable-static=no \
      --enable-final 

      $EXTRACONFIG
            
    make -j2 || exit 1
    # make install
    
    make install DESTDIR=$PKG

	install -d -m 755 -o root -g root $PKG/usr/doc/$(basename $SRC)
	install -m 644 -o root -g root AUTHORS INSTALL README TODO NEWS ChangeLog $PKG/usr/doc/$(basename $SRC)   



    # Striptease
    ( 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

	  for mandir in $(find ./usr/share/man -name man* -type d) ; do
		mkdir -p ./usr/man/$(basename $mandir)
        mv $mandir/*.? ./usr/man/$(basename $mandir)
      done	  
	  rm -rf ./usr/share/man
	  for mandir in $(find . -name man -type d) ; do
        if [ -d $mandir ]; then
          ( cd $mandir
            find . -type f -name "*.?" -exec gzip -9 {} \;
          )
        fi
      done
    )
done


cd $PKGBUILD


# redundant , if any
rm -rf $PKG/usr/share/doc


# slack-desc
mkdir -p $PKG/install
cat > $PKG/install/slack-desc << END
$softname: $softname - a Cairo GTK Engine
$softname:
$softname: Murrine is a Gtk+ engine, written in C language, using cairo 
$softname: vectorial graphics library. By default it comes with a modern 
$softname: glassy look, elegant and clean on the eyes. But it is also 
$softname: extremely customizable, and allows the user to achieve an 
$softname: incredible variety of styles.
$softname:
$softname:
$softname:
$softname:
END


install -D -m 644 -o root -g root $PKGBUILD/build-$softname.sh $PKG/usr/src/$softname-$softversion/build-$softname.sh

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

# Cleanup
cd $PKGBUILD
rm -rf $PKG
rm -rf $TMP
