#!/bin/sh

# Copyright 2003  Slackware Linux, Inc., Concord, CA, USA
# Copyright 2007, 2008, 2009, 2010, 2011  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.

VERSION=4.8
BUILD=${BUILD:-19vl70}

# The individual tarballs often have different versions; handle them here
XDT_VERS=4.8.0		# xfce4-dev-tools version
LIBUTIL_VERS=4.8.2	# libxfce4util version
XFCONF_VERS=4.8.0	# xfconf version
XFCEUI_VERS=4.8.0	# libxfce4ui version
XFCEGUI4_VERS=4.8.1	# libxfcegui4 version
EXO_VERS=0.6.2		# exo version
GARCVER=0.1.9		# garcon version
TUMBVER=0.1.22		# tumbler version
THUNAR_VERS=1.2.3	# thunar version
PANEL_VERS=4.8.6	# xfce4-panel version
SETTINGS_VERS=4.8.3	# xfce4-settings version
SESSION_VERS=4.8.2	# xfce4-session version
XFDESKTOP_VERS=4.8.3	# xfdesktop version
XFWM_VERS=4.8.2		# xfwm4 version
UTILS_VERS=4.8.3	# xfce-utils version
APPFINDER_VERS=4.8.0	# xfce4-appfinder version
GXE_VERS=2.8.1		# gtk-xfce-engine version
XFWMTHEME_VERS=4.6.0	# xfwm4-themes version

NUMJOBS=${NUMJOBS:--j2}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i586 ;;
    arm*) export ARCH=arm ;;
       *) export ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/xfce-build-dir}
PKG=$TMP/package-xfce

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP || exit 1

COMPRESSION=xz
COMPRESSUTIL=xz

for file in \
 xfce4-dev-tools-$XDT_VERS.tar.$COMPRESSION \
 libxfce4util-$LIBUTIL_VERS.tar.$COMPRESSION \
 xfconf-$XFCONF_VERS.tar.$COMPRESSION \
 libxfce4ui-$XFCEUI_VERS.tar.$COMPRESSION \
 libxfcegui4-$XFCEGUI4_VERS.tar.$COMPRESSION \
 exo-$EXO_VERS.tar.$COMPRESSION \
 garcon-$GARCVER.tar.$COMPRESSION \
 tumbler-$TUMBVER.tar.$COMPRESSION \
 Thunar-$THUNAR_VERS.tar.$COMPRESSION \
 xfce4-panel-$PANEL_VERS.tar.$COMPRESSION \
 xfce4-settings-$SETTINGS_VERS.tar.$COMPRESSION \
 xfce4-session-$SESSION_VERS.tar.$COMPRESSION \
 xfdesktop-$XFDESKTOP_VERS.tar.$COMPRESSION \
 xfwm4-$XFWM_VERS.tar.$COMPRESSION \
 xfce-utils-$UTILS_VERS.tar.$COMPRESSION \
 xfce4-appfinder-$APPFINDER_VERS.tar.$COMPRESSION \
 gtk-xfce-engine-$GXE_VERS.tar.$COMPRESSION \
 xfwm4-themes-$XFWMTHEME_VERS.tar.$COMPRESSION ;
do
  $COMPRESSUTIL -dc $CWD/src/$file | tar xvf - || exit 1
  cd $(basename $file .tar.$COMPRESSION) || exit 1
    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 {} \;
  
    # Here's where we patch or set up any other package-specific stuff:
      # Disable hal support in exo and xfce4-session
      if [ "$file" = "exo-$EXO_VERS.tar.$COMPRESSION" -o "$file" = "xfce4-session-$SESSION_VERS.tar.xz" ]; then
        PACKAGE_SPECIFIC_OPTIONS="--disable-hal"
      fi
      # Enable gnome keyring support in xfce4-session
      if [ "$file" = "xfce4-session-$SESSION_VERS.tar.$COMPRESSION" ]; then
        PACKAGE_SPECIFIC_OPTIONS="--enable-libgnome-keyring"
      fi
      if [ "$file" = "xfce-utils-$UTILS_VERS.tar.$COMPRESSION" ]; then
        # Launch xfce through consolekit so that ck enabled stuff works in xfce
        zcat $CWD/patches/xfce-utils.xinitrc.consolekit.diff.gz | patch -p1 || exit 1
        # Don't start ssh-agent or gpg-agent automatically; some users 
        # (including rworkman) want to use gnome-keyring for those
        zcat $CWD/patches/do_not_start_ssh_and_gpg_agents.diff.gz | patch -p1 || exit 1
        # Load /etc/X11/xinit/.Xmodmap before $HOME/.Xmodmap
        zcat $CWD/patches/load_systemwide_Xmodmap_file.diff.gz | patch -p1 || exit 1
        # Fix the "Help" button in the start menu
        zcat $CWD/patches/fixup-help-function-in-utils.diff.gz | patch -p1 || exit 1
        # Set the Vendor information; we can now drop some plain text about 
        # us into a /usr/share/xfce4/Slackware file if desired
        PACKAGE_SPECIFIC_OPTIONS="--with-vendor-info=Slackware"
      fi
    # End patches/package-specific stuff

    if [ -e autogen.sh ]; then
      do_configure="./autogen.sh"
    else
      do_configure="./configure"
    fi

    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    $do_configure \
      --prefix=/usr \
      --libdir=/usr/lib${LIBDIRSUFFIX} \
      --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 \
      --disable-debug \
      --build=$ARCH-slackware-linux \
      $PACKAGE_SPECIFIC_OPTIONS
  
    # Unset this now -- we're done with it
    unset PACKAGE_SPECIFIC_OPTIONS
  
    make $NUMJOBS || make || exit 1
    #make install || exit 1
    make install DESTDIR=$PKG
  
    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

    # This will (hopefully) fix the included help function in xfce-utils
    # (coupled with the small patch we applied)
    ( cd $PKG/usr/doc/xfce-$VERSION
      ln -s $(basename $file .tar.$COMPRESSION) $(echo $(basename $file .tar.$COMPRESSION) | rev | cut -d- -f2- | rev)
    )
  cd - ;
done

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# Add vendor info in /usr/share/xfce4/Slackware
echo "http://slackware.com/" > $PKG/usr/share/xfce4/Slackware

# 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.x.   In the meantime, we'll default to gnome.
sed -i s/Rodent/gnome/ \
  $PKG/etc/xfce/xdg/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml

# We're going to borrow the "applications-education" icons (six of them)
# from the kde 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 {} \;

# 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/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
)

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

cd $PKG
/sbin/makepkg -l y -c n $CWD/xfce-$VERSION-$ARCH-$BUILD.txz

