#!/bin/sh
# Copyright 2003  Slackware Linux, Inc., Concord, CA, 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.

# Modified by Robby Workman  (http://rlworkman.net) on 20070410
# No additional license terms added.

# You will need the URI::Escape perl module installed to build libexo
# See http://slackbuilds.org/repository/libraries/uri-escape/

TMP=${TMP:-/tmp/build}
PKG=$TMP/package-xfce
CWD=$(pwd)
OUTPUT=${OUTPUT:-/tmp}		# Location of final package
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}
TAG=${TAG:-_rlw}

VERSION=4.4.1
TERMVERS=0.2.6			# Version of Terminal
THUNARVERS=0.8.0		# Version of Thunar
EXOVERS=0.3.2			# Version of libexo/exo
GXEVERS=2.4.1			# Version of gtk-xfce-engine
MPADVERS=0.2.12			# Version of mousepad
XDTVERS=4.4.0			# Version of xfce-dev-tools

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

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

for file in \
xfce4-dev-tools-$XDTVERS.tar.bz2 \
libxfce4util-$VERSION.tar.bz2 \
libxfcegui4-$VERSION.tar.bz2 \
libxfce4mcs-$VERSION.tar.bz2 \
xfce-mcs-manager-$VERSION.tar.bz2 \
exo-$EXOVERS.tar.bz2 \
xfce4-panel-$VERSION.tar.bz2 \
Thunar-$THUNARVERS.tar.bz2 \
xfdesktop-$VERSION.tar.bz2 \
xfce-mcs-plugins-$VERSION.tar.bz2 \
xfce-utils-$VERSION.tar.bz2 \
xfce4-icon-theme-$VERSION.tar.bz2 \
xfce4-session-$VERSION.tar.bz2 \
xfwm4-$VERSION.tar.bz2 \
gtk-xfce-engine-$GXEVERS.tar.bz2 \
xfprint-$VERSION.tar.bz2 \
orage-$VERSION.tar.bz2 \
xfwm4-themes-$VERSION.tar.bz2 \
xfce4-appfinder-$VERSION.tar.bz2 \
mousepad-$MPADVERS.tar.bz2 \
Terminal-$TERMVERS.tar.bz2 \
xfce4-mixer-$VERSION.tar.bz2 \
; do
  ( echo
    sleep 2
    echo Starting $file...
    echo
    cd $TMP
    tar -xjf $CWD/src/$file
    cd $(basename $file .tar.bz2)

    # Add experimental patch from Mike Massonnett to add a border to the panel
    # plugins - this should fix: http://bugzilla.xfce.org/show_bug.cgi?id=2267
    if [ "$file" = "xfce4-mixer-$VERSION.tar.bz2" ]; then
      zcat $CWD/xfce4-mixer-plugin.diff.gz | patch -p1 --verbose || exit 1
    fi

    chown -R root:root .
    chmod -R u+w,go+r-w,a-s .

    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    ./configure \
      --prefix=/usr \
      --sysconfdir=/etc/X11 \
      --enable-static=no \
      --enable-xinerama \
      --with-sound=alsa \
      --program-prefix="" \
      --program-suffix="" \
      --build=$ARCH-slackware-linux \
      --host=$ARCH-slackware-linux

    make -j3
    make install-strip DESTDIR=$PKG
    make install-strip

    mkdir -p $PKG/usr/doc/xfce-$VERSION/$(basename $file .tar.bz2)
    cp -a AUTHORS COPYING ChangeLog INSTALL NEWS README TODO \
      $PKG/usr/doc/xfce-$VERSION/$(basename $file .tar.bz2) 
    echo 
    echo $file done
    echo 
  )
done

for mandir in $(find . -name man -type d) ; do
  if [ -d $mandir ]; then
    ( cd $mandir
      find . -type f -name "*.?" -exec gzip -9 {} \;
    )
  fi
done

# Terminal and Thunar install docs to /usr/share/doc/$APPNAME,
# and since /usr/share/doc is symlinked to /usr/doc, I'd rather not have
# two doc directories for these (one with -$VERSION appended and one without)
# and some internal xfce stuff looks in the /usr/share location.  It seems
# possible to change that, but so far as I'm concerned, this is easier :)
mv $PKG/usr/doc/xfce-$VERSION/Terminal-$TERMVERS/* \
  $PKG/usr/share/doc/Terminal/
mv $PKG/usr/doc/xfce-$VERSION/Thunar-$THUNARVERS/* \
  $PKG/usr/share/doc/Thunar/
rmdir $PKG/usr/doc/xfce-$VERSION/Terminal-$TERMVERS
rmdir $PKG/usr/doc/xfce-$VERSION/Thunar-$THUNARVERS

# Now lets make them look like the rest 
cd $PKG/usr/doc/xfce-$VERSION
  rm -rf Terminal-$TERMVERS Thunar-THUNARVERS
  ln -s ../../share/doc/Terminal Terminal-$TERMVERS
  ln -s ../../share/doc/Thunar Thunar-$THUNARVERS

  # Xfce installs its own documentation to /usr/share/xfce/doc
  # We'll add a symlink to there in our Slackware-specific docs
  ln -s ../../share/xfce4/doc doc

  # Several of the xfce libraries install gtk-docs
  # We'll add a symlink to there in our Slackware-specific docs
  ln -s ../../../share/gtk-doc/html/libxfce4mcs libxfce4mcs-$VERSION/html
  ln -s ../../../share/gtk-doc/html/libxfcegui4 libxfcegui4-$VERSION/html
  ln -s ../../../share/gtk-doc/html/libxfce4panel xfce4-panel-$VERSION/html
  ln -s ../../../share/gtk-doc/html/libxfce4util libxfce4util-$VERSION/html
  ln -s ../../../share/gtk-doc/html/libxfprint xfprint-$VERSION/html
cd -

# Add the SlackBuild script to the package's doc directory
cat $CWD/xfce.SlackBuild > $PKG/usr/doc/xfce-$VERSION/xfce.SlackBuild

# Patch the /usr/bin/startxfce4 script to include /opt/kde/share in its
# search path (so it will find the kde app icons)
# This is (I think) due to a change from 4.2.x --> 4.4 in which there is
# no longer an internal icon theme handler; we now use the gtk handler
cd $PKG
zcat $CWD/startxfce4.diff.gz | patch -p0 || exit 1

# If you want to customize the behavior of the 'startxfce4' script used to
# start xfce, copy /etc/X11/xdg/xfce4/xinitrc to $HOME/.config/xfce4/xinitrc 
# and edit the copy, as this file is looked at (if it exists) instead of the 
# system default in /etc/X11/xdg/xfce4. 
# The user's $HOME/.xinitrc is simply a copy of the /usr/bin/startxfce4 
# script.
mkdir -p $PKG/etc/X11/xinit
cd $PKG/etc/X11/xinit
  ln -s ../../../usr/bin/startxfce4 $PKG/etc/X11/xinit/xinitrc.xfce
cd -

# Install the xfce4.desktop file so that kdm and gdm see it as an available 
# window manager session.
# Note that in /opt/kde/share/config/kdm/kdmrc, the following is present:
#  # The directories containing session type definitions in .desktop format.
#  # Default is "/opt/kde/share/apps/kdm/sessions"
#  # SessionsDirs=/etc/X11/sessions,/usr/share/xsessions
# In other words, kdm will not look at our file by default, but the desktop 
# file installed in /opt/kde/share/apps/kdm/sessions as part of the kdebase 
# package will work fine.
mkdir -p $PKG/etc/X11/sessions
cat $CWD/xfce4.desktop > $PKG/etc/X11/sessions/xfce4.desktop

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

cd $PKG
/sbin/makepkg -p -l y -c n $OUTPUT/xfce-$VERSION-$ARCH-$BUILD$TAG.tgz
