#!/bin/sh
# Copyright 2003  Slackware Linux, Inc., Concord, CA, USA
# Copyright 2007, 2008  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Set initial variables:
CWD=$(pwd)
TMP=${TMP:-/tmp/xfce-build-dir}
rm -rf $TMP
mkdir -p $TMP
PKG=$TMP/package-xfce
rm -rf $PKG
mkdir -p $PKG

VERSION=4.4.2
PKGVER=4.4.2
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-2}
DISTRO=${DISTRO:-slamd64}

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

if [ $DISTRO = slamd64 ]; then
	LIBSUFFIX=${LIBSUFFIX:-64}
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

mkdir -p $PKG/etc/profile.d
cp -a $CWD/profile.d/* $PKG/etc/profile.d
chown root:root $PKG/etc/profile.d/*
chmod 755 $PKG/etc/profile.d/*

# Kludge factor ahead
mkdir -p $PKG/usr/man $PKG/usr/share $PKG/usr/doc
( cd $PKG/usr/share ; ln -sf ../man . ; ln -sf ../doc . )

for file in \
xfce4-dev-tools-4.4.0.1.tar.bz2 \
libxfce4util-4.4.2.tar.bz2 \
libxfcegui4-4.4.2.tar.bz2 \
libxfce4mcs-4.4.2.tar.bz2 \
xfce-mcs-manager-4.4.2.tar.bz2 \
exo-0.3.4.tar.bz2 \
xfce-mcs-plugins-4.4.2.tar.bz2 \
xfce4-panel-4.4.2.tar.bz2 \
Thunar-0.9.0.tar.bz2 \
xfce4-session-4.4.2.tar.bz2 \
xfwm4-4.4.2.tar.bz2 \
xfdesktop-4.4.2.tar.bz2 \
xfce-utils-4.4.2.tar.bz2 \
xfprint-4.4.2.tar.bz2 \
gtk-xfce-engine-2.4.2.tar.bz2 \
mousepad-0.2.13.tar.bz2 \
Terminal-0.2.8.tar.bz2 \
xfce4-appfinder-4.4.2.tar.bz2 \
xfce4-mixer-4.4.2.tar.bz2 \
xfce4-icon-theme-4.4.2.tar.bz2 \
xfwm4-themes-4.4.2.tar.bz2 \
orage-4.4.2.tar.bz2 \
; do
  ( cd $TMP
    tar xjf $CWD/src/$file || exit 1
    cd $(basename $file .tar.bz2)

    # Here's where we patch or set up any other package-specific stuff:
    if [ "$file" = "xfce4-mixer-$VERSION.tar.bz2" ]; then
      PACKAGE_SPECIFIC_OPTIONS="--with-sound=alsa"
    fi

    # Official patches from Thunar developer 
    if [ "$file" = "Thunar-0.9.0.tar.bz2" ]; then
      # http://foo-projects.org/pipermail/xfce4-dev/2007-December/023857.html
      zcat $CWD/thunar-vfs-volume-hal_missing-audio-cds-for-volman.patch.gz | patch -p0 --verbose || exit 1
      # http://bugzilla.xfce.org/show_bug.cgi?id=2983
      zcat $CWD/thunar_xfdesktop-zombie_process-fix.diff.gz | patch -p1 --verbose || exit 1
    fi

    # Official patch to fix memory leak in xfdesktop:
    if [ "$file" = "xfdesktop-4.4.2.tar.bz2" ]; then
      zcat $CWD/xfdesktop-desktop-menu-dentry.diff.gz | patch -p1 --verbose || exit 1
    fi

    chown -R root:root .
    find . \
     \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
     -exec chmod 755 {} \; -o \
     \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
     -exec chmod 644 {} \;

    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    ./configure --prefix=/usr \
			--libdir=/usr/lib$LIBSUFFIX \
      --sysconfdir=/etc/xfce \
      --enable-compositor \
      --enable-static=no \
      --enable-xinerama \
      $PACKAGE_SPECIFIC_OPTIONS \
      --build=$ARCH-$DISTRO-linux

    # Unset this now -- we're done with it
    unset PACKAGE_SPECIFIC_OPTIONS

    # I had a few things blow up with
    # -j > 1...
    make || exit 1
    make install
    make install DESTDIR=$PKG
    mkdir -p $PKG/usr/doc/$(basename $file .tar.bz2)
    # This will cause errors, but won't miss any docs:
    cp -a \
     AUTHORS BUGS COMPOSITOR COPYING COPYING.LIB COPYING_LIBS FAQ HACKING \
     INSTALL NEWS NOTES README README.Kiosk README.Plugins THANKS TODO \
     example.gtkrc-2.0 $PKG/usr/doc/$(basename $file .tar.bz2) 2> /dev/null \
     || true
  ) || exit 1
done || exit 1

( 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
)

# Le kludge r'mvoal
( cd $PKG/usr/share ; rm -f doc man )

# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

( mkdir -p $PKG/etc/X11/xinit
  cd $PKG/etc/X11/xinit
  ln -sf ../../xfce/xdg/xfce4/xinitrc xinitrc.xfce
  chmod 755 ../../xfce/xdg/xfce4/xinitrc
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n $TMP/xfce-$PKGVER-$PKGARCH-$BUILD.tgz

