#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-kdebase

# Set the config option variables if they are not already set:
if [ -r ../KDE.options ]; then
  . ../KDE.options
fi

# The global options may be overridden here (if needed):
if [ -r ./local.options ]; then
  . ./local.options
fi

rm -rf $PKG
mkdir -p $PKG/opt/kde

# Avoid a version number in .la files:
if [ -d /usr/lib/qt ]; then
  QTDIR=/usr/lib/qt
fi
cd $TMP
echo "Building kdebase-$VERSION.tar.bz2..."
tar xjvf $CWD/kdebase-$VERSION.tar.bz2
cd kdebase-$VERSION
chown -R root.root .

( cd konqueror ; cat $CWD/post-3.3.2-kdebase-htmlframes2.patch | patch -p1 -E --verbose )

CFLAGS=$CPUOPT \
CXXFLAGS=$CPUOPT \
./configure \
  --prefix=/opt/kde \
  --with-shadow \
  --with-xinerama \
  --disable-debug \
  --program-prefix="" \
  --program-suffix="" \
  $TARGET-slackware-linux
make $NUMJOBS
make install DESTDIR=$PKG

### !!! KLUDGE ALERT !!! ###
### CAUSES ARTS TO NOT USE REALTIME PRIORITY BY DEFAULT, WORKING AROUND A LOGOUT HANG ###
### THIS SHOULD BE RECONSIDERED WITH EACH KDEBASE RELEASE! ###
cat $CWD/kludge/kcmartsrc > $PKG/opt/kde/share/config/kcmartsrc
### !!! KLUDGE ALERT !!! ###

mkdir -p $PKG/usr/doc/kdebase-$VERSION
cp -a \
  AUTHORS COPYING INSTALL README kdebase.lsm \
  $PKG/usr/doc/kdebase-$VERSION
mkdir -p $PKG/etc/X11/xinit $PKG/etc/profile.d
cat $CWD/xinit/xinitrc.kde > $PKG/etc/X11/xinit/xinitrc.kde
cat $CWD/profile.d/kde.sh > $PKG/etc/profile.d/kde.sh
cat $CWD/profile.d/kde.csh > $PKG/etc/profile.d/kde.csh
chmod 755 $PKG/etc/X11/xinit/xinitrc.kde $PKG/etc/profile.d/kde.sh $PKG/etc/profile.d/kde.csh

# Change the konsole defaults to look like the real Linux console:
cat $CWD/config/konsolerc > $PKG/opt/kde/share/config/konsolerc

# Make konsole a login shell by default:
cat $CWD/config/konsole.desktop > $PKG/opt/kde/share/applications/kde/konsole.desktop

# Install our own special KDM Xsession file (better for non-bash users):
( cd $PKG/opt/kde/share/config/kdm
  cp -a Xsession Xsession.orig
  cat $CWD/config/Xsession > Xsession
)

# Save default kdmrc as an example as it has many useful comments:
mv $PKG/opt/kde/share/config/kdm/kdmrc $PKG/opt/kde/share/config/kdm/kdmrc.sample.default
# Add kdmrc with Slackware defaults:
cat $CWD/config/kdmrc > $PKG/opt/kde/share/config/kdm/kdmrc.new

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

( 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
)
if [ -d $PKG/opt/kde/man ]; then
  gzip -9 $PKG/opt/kde/man/man?/*
fi

cd $PKG
makepkg -l y -c n ../kdebase-$VERSION-$ARCH-$BUILD.tgz