#!/bin/bash

# License: Do whatever you want at your own risk. Do not remove the credits.
# Warning: No warranties, if you don't want to assume the risks, just delete 
# this file.
#
# Author: Federico Voges <fvoges@intrasoft.com.ar>
# This script was made using instructions/tips/advice from the Linux SxS
# (linux-users@linux-sxs.org) mainly from:
# Douglas J. Hunley, Tim Wunder and Keith Antoine (thanks guys!)
#
# SIMPLE INSTRUCTIONS
#
# First: I've used objprelink (http://leon.bottou.com/objprelink/) and 
# checkinstall (http://proyectos.glo.org.mx/checkinstall/)
# Go get them, or change this script and build options
#
# Get the whole ftp://ftp.kde.org/pub/kde/stable/3.0/src/ directory. 
#
# NOTE: koffice is a symlink, so be sure to download the file and place it in
# this dir (unless you don't want koffice) it's located at 
# ftp.kde.org/pub/kde/stable/koffice-1.1.1-kde3/src
#
# I've place all the files in /home/ftp/pub/kde-3.0/src/
# make a directory to extract all sources (in my case build inside src)
# extract all sources. eg. (from /home/ftp/pub/kde-3.0/src/build):
# for f in ../*.tar.bz2; do bzcat $f| tar xvf -;done
# then, run this script and take the day off :)
#
# NOTE: There's a kdevelop-2.1_for_KDE_2.2 directory which I deleted 
# before building KDE 3.0
#
# For OpenLinux 3.1.1, I had to download/install:
# pcre - The version installed (in /opt/kde2) wasn't found by ./configure
# libxml - Too old
# libxslt - Too old

QTDIR="/usr/lib/qt3"
KDEDIR="/opt/kde3"
CXXFLAGS="-O3 -march=i686 -mcpu=i686"
CPPFLAGS="-O3 -march=i686 -mcpu=i686"
CFLAGS="-O3 -march=i686 -mcpu=i686"
KDECOMMON="--prefix=$KDEDIR --disable-debug --enable-final --enable-shared --enable-objprelink --with-xinerama --with-qt-dir=$QTDIR"
#KDECOMMON="--prefix=$KDEDIR --disable-debug --enable-final --enable-shared --with-xinerama --with-qt-dir=$QTDIR"
MAKEOPTS="-j $[`grep -c processor /proc/cpuinfo`+1]"

OLDPATH="$PATH"
PATH2="`echo $PATH|sed 's/kde2/kde3/g'`"
PATH="$PATH2"
export QTDIR KDEDIR CXXFLAGS CPPFLAGS CFLAGS KDECOMMON OLDPATH PATH MAKEOPTS

#cd /home/ftp/pub/kde-3.0/src/build
cd /home/src

if [ ! -f "$QTDIR/BEEN_HERE_DONE_THAT" ]; then 
  cp -a qt-copy-3.0.3 "$QTDIR"
  cd "$QTDIR"
  (
    echo yes | ./configure -prefix /usr/lib/qt3 -release -shared -qt-gif -system-zlib -no-g++-exceptions -thread -system-libpng -system-libjpeg -system-libmng -no-nas-sound -sm -xinerama -xrender -xft -no-tablet -xkb -platform linux-g++-objprelink 2>&1 || exit 1
    make $MAKEOPS 2>&1 || exit 1
    touch BEEN_HERE_DONE_THAT
  ) 2>&1 | tee qt-copy-3.0.3.log
  echo -en "\nPress [ENTER] to continue, or Ctrl-C to abort..."
  read

  cd -
fi

PKGS="pcre* libxml* libxslt* arts* kdelibs* kdebase* `ls -1 --color=no|egrep -v "(kdesupport|arts|kdelibs|kdebase|kdeaddons|qt-copy)"` kdeaddons*"

for f in $PKGS; do
  if [ ! -d $f ]; then
    continue
  fi
  echo "Package: $f"
  case "$f" in
    pcre*)
      OPTIONS="--prefix=/usr"
      ;;
    libxml*)
      OPTIONS="--prefix=/usr --with-threads --with-zlib"
      ;;
    libxslt*)
      OPTIONS="--prefix=/usr"
      ;;
    kdelibs*)
      OPTIONS="$KDECOMMON --with-ssl-dir=/usr/ssl/ --enable-mitshm --enable-dnotify"
      ;;
    kdebase*)
      OPTIONS="$KDECOMMON --with-pam=yes --with-shadow --with-ssl-dir=/usr/ssl/ --with-x"
      ;;
    kdeadmin*)
      OPTIONS="$KDECOMMON --with-pam=yes --with-shadow --with-rpm"
      ;;
    kdemultimedia*)
      OPTIONS="$KDECOMMON --with-x"
      ;;
    kdeartwork*)
      OPTIONS="$KDECOMMON --without-dpms"
      ;;
    kdebindings*)
      OPTIONS="$KDECOMMON --without-java"
      ;;
    *)
      OPTIONS="$KDECOMMON"
      ;;
  esac
  
  cd $f
  # check if it's allready done
  if [ ! -f BEEN_HERE_DONE_THAT ]; then 
    (
    ./configure $OPTIONS || exit 1
    make $MAKEOPTS || exit 1
    checkinstall -R -y || exit 1
    ldconfig
    touch BEEN_HERE_DONE_THAT
    ) 2>&1 | tee $f.log
    # Mark this PKG as installed
  else
    echo "Skipping (already installed)"
  fi
  cd -

  # If you want to pause between each PKG, uncommnet the following lines
  echo -en "\nPress [ENTER] to continue, or Ctrl-C to abort..."
  read
done

PATH="$OLDPATH"
exit 0

