#!/bin/bash

# kdebase.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 08-Aug-2005

# 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

# 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

# Avoid a version number in .la files:
if [ -d /usr/lib/qt ]; then
   QTDIR=/usr/lib/qt
fi

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

# Create some package framework:
mkdir -p $PKG/etc/X11/xinit $PKG/etc/profile.d
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION

# Extract source:
tar xvvf $CWD/../src/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

if [ -r $CWD/apply-patches.sh ]; then
   . $CWD/apply-patches.sh
fi

# Configure:
CFLAGS=$CPUOPT \
CXXFLAGS=$CPUOPT \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc/kde \
   --with-shadow \
   --with-xinerama \
   --disable-debug \
   --program-prefix="" \
   --program-suffix="" \
   --build=$TARGET-slackware-linux || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# Move the KDM files someplace FHS compliant:
mkdir -p $PKG/etc/kde
mv $PKG/usr/share/config/kdm $PKG/etc/kde
( cd $PKG/usr/share/config ; ln -sf ../../../etc/kde/kdm . )

### !!! 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/usr/share/config/kcmartsrc
### !!! KLUDGE ALERT !!! ###

# Copy docs:
cp -a AUTHORS COPYING INSTALL README kdebase.lsm \
      $PKG/usr/doc/$PKGNAM-$VERSION
install -m755 $CWD/xinit/xinitrc.kde $PKG/etc/X11/xinit/
install -m755 $CWD/profile.d/kde.sh  $PKG/etc/profile.d/
install -m755 $CWD/profile.d/kde.csh $PKG/etc/profile.d/

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

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

# set sane defaults for antialiasing:
if [ ! -e $PKG/usr/share/config/kdeglobals ]; then
  cat $CWD/config/kdeglobals > $PKG/usr/share/config/kdeglobals
  chmod 644 $PKG/usr/share/config/kdeglobals
else
  # This might be fixed or unsafe.  Better check it out...
  echo
  echo "BAILOUT: kdeglobals already exists.  Check to see if anti-aliased"
  echo "         fonts work out-of-the-box now..."
  echo
  exit 1
fi

# Setup config files for KDM:
$PKG/usr/bin/genkdmconf --no-old --no-old-scripts --no-backup --in $PKG/usr/share/config/kdm
# Not allowing root to login at the console is just plain silly.  If they have physical access to
# the machine, what's the point of trying to stop this?  Even the kdmrc file says this is supposed
# to be the default.
cat $PKG/usr/share/config/kdm/kdmrc | perl -pi -e 's|AllowRootLogin=false|AllowRootLogin=true|' > $PKG/usr/share/config/kdm/kdmrc.new
rm -f $PKG/usr/share/config/kdm/kdmrc
cp -a $PKG/usr/share/config/kdm/Xsession $PKG/usr/share/config/kdm/Xsession.orig
cat $CWD/config/Xsession > $PKG/usr/share/config/kdm/Xsession
mv $PKG/usr/share/config/kdm/backgroundrc $PKG/usr/share/config/kdm/backgroundrc.new

# 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
