#!/bin/bash

# Packager : Jean-Philippe Guillemin <jp.guillemin@free.fr>
# sources location : http://www.ca-us.xfce.org/archive/ 


softname='xfce4-panel'
#softversion=''
tarball=$(ls ${softname}*tar*);softversion=${tarball%.*};softversion=${softversion%.*}; softversion=${softversion##*-}
packageversion='72.1'
arch='i486'
cpu='i686'

PKGBUILD=$(pwd)

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

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

TMP="$PKGBUILD/tmp"

cd $PKGBUILD

sources="$softname"


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
  
  if [ "$(echo $tarball | grep ".tar.bz2" )" ]; then
      tar jxvf $tarball -C $TMP
  elif [ "$(echo $tarball | grep ".tar.gz" )" ]; then
      tar zxvf $tarball -C $TMP
  fi
  
  SRC=$(ls -d $TMP/*/)

  
    cd $SRC
EXTRACONFIG="--enable-maintainer-mode"
    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 {} \;

    export CFLAGS="-Os -march=$arch -mtune=$cpu"
    export ICEAUTH='/usr/bin/iceauth'
EXTRACONFIG="--enable-maintainer-mode"
    ./configure --prefix=/usr \
      --mandir=/usr/man \
      --docdir=/usr/doc/$(basename $SRC) \
      --sysconfdir=/etc \
      --enable-static=no \
      --enable-compositor \
      --enable-xinerama \
      --enable-final \
      --enable-debug=no \
      --with-sound=alsa \
      --disable-gtk-doc \
      $EXTRACONFIG
            
    make -j2 || exit 1
    # make install
    
    make install DESTDIR=$PKG

    mkdir -p $PKG/usr/doc/$(basename $SRC)
    cp -f 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 is part of the XFCE desktop
$softname:
$softname: XFce is a fast and lightweight desktop environment that is both
$softname: sex-appealing and easy to use.
$softname: 
$softname: For more information, see:  http://www.xfce.org
$softname:
$softname:
$softname:
$softname:
$softname:
END

mkdir -p $PKG/usr/src/$softname-$softversion
cp $PKGBUILD/build-$softname.sh $PKG/usr/src/$softname-$softversion/

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

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