#!/bin/bash

# xfce.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter 
# 08-May-2005
#
# 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac 

# A few tarballs have different versions; handle them here
XDT_VERS=4.7.2          # xfce4-dev-tools version
# We're going to ship xdt from git (targeting xfce-4.8) since it's not used
# for building release tarballs anyway, but it's handy to have around for
# building from git, and some folks might want to do that later...
EXO_VERS=0.3.106        # exo version
THUNAR_VERS=1.0.1       # thunar version
GXE_VERS=2.6.0          # gtk-xfce-engine version
TERM_VERS=0.4.4         # terminal version
MPAD_VERS=0.2.16        # mousepad version
XFWMTHEME_VERS=4.6.0    # xfwm4-themes version
SETTINGS_VERS=4.6.4     # xfce4-settings version
PANEL_VERS=4.6.3        # xfce4-panel version
XFCEGUI_VERS=4.6.3      # libxfcegui4 version

COMPRESSION=xz
COMPRESSUTIL=xz

# The Rodent theme isn't xdg-compliant, so they're not shipping it
# xfce4-icon-theme-$VERSION.tar.$COMPRESSION
# We'll set Tango as the system default instead...

for file in \
 xfce4-dev-tools-$XDT_VERS.tar.$COMPRESSION \
 libxfce4util-$VERSION.tar.$COMPRESSION \
 xfconf-$VERSION.tar.$COMPRESSION \
 libxfcegui4-$XFCEGUI_VERS.tar.$COMPRESSION \
 libxfce4menu-$VERSION.tar.$COMPRESSION \
 exo-$EXO_VERS.tar.$COMPRESSION \
 xfce4-panel-$PANEL_VERS.tar.$COMPRESSION \
 Thunar-$THUNAR_VERS.tar.$COMPRESSION \
 xfce4-settings-$SETTINGS_VERS.tar.$COMPRESSION \
 xfce4-session-$VERSION.tar.$COMPRESSION \
 xfdesktop-$VERSION.tar.$COMPRESSION \
 xfwm4-$VERSION.tar.$COMPRESSION \
 xfce-utils-$VERSION.tar.$COMPRESSION \
 xfce4-mixer-$VERSION.tar.$COMPRESSION \
 xfprint-$VERSION.tar.$COMPRESSION \
 xfce4-appfinder-$VERSION.tar.$COMPRESSION \
 gtk-xfce-engine-$GXE_VERS.tar.$COMPRESSION \
 Terminal-$TERM_VERS.tar.$COMPRESSION \
 xfwm4-themes-$XFWMTHEME_VERS.tar.$COMPRESSION \
 mousepad-$MPAD_VERS.tar.$COMPRESSION \
 orage-$VERSION.tar.$COMPRESSION ;
do
  cd $TMPBUILD
  tar xvvf $CWD/src/$file || exit 1
  cd $(basename $file .tar.$COMPRESSION) || exit 1
  slackhousekeeping

   # Here's where we patch or set up any other package-specific stuff:
      # Patch to only show xfcalendar in XFce (otherwise it shows up in
      # KDE's Lost & Found and does not work):
      if [ "$file" = "orage-$VERSION.tar.$COMPRESSION" ]; then
        zcat $CWD/patches/xfcalendar.desktop.in.diff.gz | patch -p1 || exit 1
      fi
      # Fix Terminal's title bar and preferences problems with GTK+ 2.18:
      if [ "$file" = "Terminal-$TERM_VERS.tar.$COMPRESSION" ]; then
        zcat $CWD/patches/terminal.gtk.2.18.fix.diff.gz | patch -p1 || exit 1
      fi

      # Make Thunar respect --docdir
      if [ "$file" = "Thunar-$THUNAR_VERS.tar.$COMPRESSION" ]; then
        zcat $CWD/patches/thunar-1.0.1-fixup_docdir.diff.gz | patch -p1 || exit 1
        zcat $CWD/patches/thunar-fix_umask_properly.diff.gz | patch -p1 || exit 1
        # Port to PNG 1.4.0:
        zcat $CWD/patches/thunar-1.0.1-png14.diff.gz | patch -p1 || exit 1
      fi
      # Fix the missing education icon in the desktop menu
      if [ "$file" = "xfdesktop-$VERSION.tar.$COMPRESSION" ]; then
        zcat $CWD/patches/xfdesktop-fix_education_icon.diff.gz | patch -p1 || exit 1
      fi
      # Port xfce4-settings to libxklavier-5.x:
      if [ "$file" = "xfce4-settings-$SETTINGS_VERS.tar.$COMPRESSION" ]; then
        zcat $CWD/patches/xfce4-settings.libxklavier5.diff.gz | patch -p1 || exit 1
      fi
      # Launch xfce through consolekit so that ck enabled stuff works in xfce:
      if [ "$file" = "xfce-utils-$VERSION.tar.$COMPRESSION" ]; then
        zcat $CWD/patches/xfce-utils.xinitrc.consolekit.diff.gz | patch -p1 || exit 1
      fi
    # End patches/package-specific stuff
 
    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    ./configure \
      --prefix=/usr \
      --sysconfdir=/etc/xfce \
      --mandir=/usr/man \
      --docdir=/usr/doc/xfce-$VERSION/$(basename $file .tar.$COMPRESSION) \
      --htmldir=/usr/doc/xfce-$VERSION/$(basename $file .tar.$COMPRESSION)/html \
      --disable-static \
      --build=$ARCH-slackware-linux-gnueabi \
      $PACKAGE_SPECIFIC_OPTIONS || failconfig

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

    make $NUMJOBS || make || failmake
    make install || failinstall
    make install DESTDIR=$PKG || failinstall

    mkdir -p $PKG/usr/doc/xfce-$VERSION/$(basename $file .tar.$COMPRESSION)
    # This will cause errors, but won't miss any docs:
    cp -a \
      AUTHORS BUGS COMPOSITOR COPYING* FAQ HACKING INSTALL \
      NEWS NOTES README* THANKS TODO example.gtkrc-2.0 \
      $PKG/usr/doc/xfce-$VERSION/$(basename $file .tar.$COMPRESSION) \
     2> /dev/null || true

    # If there's a ChangeLog, installing at least part of the recent history
    # is useful, but don't let it get totally out of control:
    if [ -r ChangeLog ]; then
      DOCSDIR=$(echo $PKG/usr/doc/xfce-$VERSION/$(basename $file .tar.$COMPRESSION))
      cat ChangeLog | head -n 1000 > $DOCSDIR/ChangeLog
      touch -r ChangeLog $DOCSDIR/ChangeLog
    fi

done

# The Rodent icon theme isn't xdg compliant, so it spews errors quite often
# and just flat breaks some things here and there, so the Xfce team elected
# not to ship it with 4.6.0.   In the meantime, we'll default to Tango.
sed -i s/Rodent/Tango/ \
  $PKG/etc/xfce/xdg/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml

# We're going to borrow the "applications-education" icons (six of them)
# from kde's oxygen icon theme (it's LGPL)
tar xf $CWD/icons.tar.xz -C $PKG/usr/share

# We don't want any of those icon caches
find $PKG/usr/share/icons -type f -name "index-theme.cache" -exec rm -f {} \;

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/*

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
)

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
