#!/bin/sh
# (C) Raphael Langerhorst, August 2004
# see file LICENSE.BSD for the license

checkreturn()
{
retvalue=$1
if [ ! $retvalue -eq 0 ]
then
  echo ""
  echo "Operation failed with error code $retvalue,"
  echo "please take a look at the output to see what went wrong."
  echo "If you don't know what to do about it, please contact"
  echo "the developers. See http://www.g-system.at"
  echo "Using the bug mailing list is recommended."
  echo ""
  exit $retvalue
fi
}

echo ""
echo "This script does a clean build of the G System."
echo "Thus it might take longer than a normal rebuild cycle."
echo "It is recommended to use this script if you are tracking"
echo "the latest version of the subversion repository and"
echo "a normal rebuild cycle does not work or leads to"
echo "unexpected crashes of the application."
echo "To do a normal rebuild cycle, just type \"./scripts/compile\"."

echo ""
echo "Checking environment variables..."
echo ""

if [ -z $QTDIR ]
then
  echo "You have to set QTDIR to your Qt installation directory."
  exit 1
else
  echo "QTDIR is set to $QTDIR"
fi

# let's see if we can set KDEDIR with kde-config --prefix
if [ -z $KDEDIR ]
then
  echo "KDEDIR environment variable not set, using kde-config."
  which kde-config > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    KDEDIR=`kde-config --prefix`
  else
    echo "kde-config could not be found!"
  fi
fi

# is KDEDIR still empty??
if [ -z $KDEDIR ]
then
  sleep 1
  echo "You have to set KDEDIR to your KDE installation directory"
  echo "in order to enable KDE integration."
  sleep 2
else
  echo "KDEDIR is now set to $KDEDIR"
fi

if [ -z $QMAKESPEC ]
then
  echo "QMAKESPEC is not set, this is required for qmake to work."
  echo "Take a look at http://doc.trolltech.com/3.3/qmake-manual-2.html"
  echo "for the details."
  exit 3
else
  echo "QMAKESPEC is set to $QMAKESPEC"
fi

export QTDIR
export KDEDIR
export QMAKESPEC

sleep 5

echo ""
echo "Removing binaries and libraries from ./bin and ./lib"
echo ""

rm -f ./bin/*
rm -f ./lib/*

echo ""
echo "Checking for a Makefile..."
echo ""

if [ -e Makefile ]
then
  echo "Makefile exists, cleaning build environment with \"make distclean\"."
  make distclean
else
  echo "No Makefile present, proceeding with Makefile generation..."
fi

echo ""
echo "Generating makefiles with \"qmake\"..."
echo ""

topdir=$PWD

echo "current directory: $topdir"

echo "executing \"qmake\" in $PWD"
qmake
checkreturn $?
echo "executing \"make clean\" in $PWD"
make clean

cd src
echo "executing \"make clean\" in $PWD"
make clean
echo "executing \"qmake\" in $PWD"
qmake
echo "executing \"make clean\" in $PWD"
make clean
checkreturn $?

for dir in *
do
  if [ -d $dir ]
  then
    lastdir=$PWD
    cd $dir
    echo "executing \"make clean\" in $PWD"
    make clean
    echo "executing \"qmake\" in $PWD"
    qmake
    checkreturn $?
    echo "executing \"make clean\" in $PWD"
    make clean
    cd $lastdir
  fi
done

cd $topdir
echo ""
echo "current directory is $PWD"

sleep 2

echo ""
echo "Cleaning build environment..."
echo ""

make clean

echo ""
echo "Starting build process with \"make\"."
echo ""

make
checkreturn $?

echo ""
echo "Compilation finished!"
echo "In order to install G System, type \"./scripts/makeinstall\"."
echo "You might need super user privileges for installation,"
echo "type \"su\" to switch to the root user."
echo "Don't forget to exit from the root shell after installing."
echo ""
echo "Please report bugs or comments to the mailing list or forum."
echo ""
echo "Take a look at the README file to find out how to use the G System."
echo ""
echo "ENJOY!!"
echo ""


syntax highlighted by Code2HTML, v. 0.9.1