#!/bin/sh
# Copyright 2003  Slackware Linux, Inc., Concord, CA, USA
# Copyright 2007  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.1
PKGVER=4.4.1
ARCH=${ARCH:-i486}
BUILD=5

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
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.tar.bz2 \
libxfce4util-4.4.1.tar.bz2 \
libxfcegui4-4.4.1.tar.bz2 \
libxfce4mcs-4.4.1.tar.bz2 \
xfce-mcs-manager-4.4.1.tar.bz2 \
exo-0.3.2.tar.bz2 \
xfce-mcs-plugins-4.4.1.tar.bz2 \
xfce4-panel-4.4.1.tar.bz2 \
Thunar-0.8.0.tar.bz2 \
xfce4-session-4.4.1.tar.bz2 \
xfwm4-4.4.1.tar.bz2 \
xfdesktop-4.4.1.tar.bz2 \
xfce-utils-4.4.1.tar.bz2 \
xfprint-4.4.1.tar.bz2 \
gtk-xfce-engine-2.4.1.tar.bz2 \
mousepad-0.2.12.tar.bz2 \
Terminal-0.2.6.tar.bz2 \
xfce4-appfinder-4.4.1.tar.bz2 \
xfce4-mixer-4.4.1.tar.bz2 \
xfce4-icon-theme-4.4.1.tar.bz2 \
xfwm4-themes-4.4.1.tar.bz2 \
orage-4.4.1.tar.bz2 \
; do
  ( cd $TMP
    tar xjf $CWD/$file || exit 1
    cd `basename $file .tar.bz2`

    # Here's where we patch or set up any other package-specific stuff:
    if [ "$file" = "xfce-utils-$VERSION.tar.bz2" ]; then
      zcat $CWD/xfce-utils-startxfce4.in.diff.gz | patch -p1 --verbose || exit 1
    fi
    if [ "$file" = "xfce4-mixer-$VERSION.tar.bz2" ]; then
      PACKAGE_SPECIFIC_OPTIONS="--with-sound=alsa"
    fi

    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 {} \;

    # This option produces no difference that I can see
    # --disable-hal

    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    ./configure --prefix=/usr \
      --sysconfdir=/etc/xfce \
      --enable-compositor \
      --enable-static=no \
      --enable-xinerama \
      $PACKAGE_SPECIFIC_OPTIONS \
      --build=$ARCH-slackware-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
    ( 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
    )
  )
done || exit 1

# 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-$ARCH-$BUILD.tgz

