#!/bin/sh
#
#
# Configures the build of Kiltdown.
#
# Copyright (c) 2001 Marc Wandschneider.  All Rights Reserved
#

########################
#
# These are our local variables for working.
#
HELP=
I_FLAGS=
L_FLAGS=
l_FLAGS=
R_FLAGS=
PLATFORM=
DEBUG=-O
CURDIR=`dirname $0`

########################
#
# These are the variables we're trying to determine values for.
#
QTINC=
QTLIB=
DBINC=
DBLIB=
KDEINC=
KDELIB=

echo

########################
#
# Process the command lines arguments
#
while [ -n "$1" ]; do

    case `echo $1 | sed -e 's/^-*//'` in
    
        qtdir)
            shift; QTDIR=$1;
            ;;
        qtdir=?*)
            QTDIR=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        qtinc)
            shift; QTINC=$1;
            ;;
        qtinc=?*)
            QTINC=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        qtlib)
            shift; QTLIB=$1;
            ;;
        qtlib=?*)
            QTLIB=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        dbdir)
            shift; DBDIR=$1;
            ;;
        dbdir=?*)
            DBDIR=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        dbinc)
            shift; DBINC=$1;
            ;;
        dbinc=?*)
            DBINC=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        dblib)
            shift; DBLIB=$1;
            ;;
        dblib=?*)
            DBLIB=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        platform)
            shift; PLATFORM=$1;
            ;;
        platform=?*)
            PLATFORM=`echo $1 | awk -F '=' '{print $2}'`;
            ;;
        kdedir)
	    shift; CONF_KDEDIR=$1;
	    ;;
        kdedir=?*)
	    CONF_KDEDIR=`echo $1 | awk -F '=' '{print $2}'`;
	    ;;
	kdeinc)
	    shift; KDEINC=$1;
	    ;;
	kdeinc=?*)
	    KDEINC=`echo $1 | awk -F '=' '{print $2}'`;
	    ;;
        kdelib)
	    shift; KDELIB=$1;
	    ;;
	kdelib=?*)
	    KDELIB=`echo $1 | awk -F '=' '{print $2}'`;
	    ;;
	debug)
            DEBUG='-g -DK_DEBUG'
            ;;
        release)
            DEBUG='-O'
            ;;
        I?*)
            I_FLAGS="$I_FLAGS $1"
            ;;
        L?*)
            L_FLAGS="$L_FLAGS $1"
            ;;
        R?*)
            R_FLAGS="$R_FLAGS $1"
            ;;
        l?*)
            l_FLAGS="$l_FLAGS $1"
            ;;
        I)
            shift
            I_FLAGS="$I_FLAGS -I$1"
            ;;
        L)
            shift
            L_FLAGS="$L_FLAGS -L$1"
            ;;
        R)
            shift
            R_FLAGS="$R_FLAGS -R$1"
            ;;
        l)
            shift
            l_FlAGS="$l_FLAGS -l$1"
            ;;
        help)
            HELP=yes
            ;;
        *)
            echo 'Unknown Argument: $1'
            HELP=yes
            ERROR=yes
            ;;
    esac
    shift
done


if [ "x$HELP" = "xyes" ]
then
    cat <<EOF

Usage: $0 [options]

The following options are available:

    -release          [default] Build a release version with optimized code.
    -debug            Build a debug version of Kiltdown with memory leak 
                        detection and assertions enabled.

    -platform <plat>  Use the specified configuration file in configs/ dir.
                        (auto-detected by default)

    -qtdir <dir>      Use the given directory as the location for Qt 2.2.3

    -qtinc <dir>      If you don't specify "qtdir", you can specify the
                        include dir separately.
    -qtlib <dir>      If you don't specify "qtlib", you can specify the
                        lib dir separately.

    -dbdir <dir>      Use the given directory as the location for DB libraries
                        (version 3.1.17 or 3.2.9)

    -dbinc <dir>      If you don't specify "dbdir", you can specify the
                        include dir separately.
    -dblib <dir>      If you don't specify "dblib", you can specify the
                        lib dir separately.

    -kdedir	      You can optionally have this application be a KDE 2.1
		        application and participate in KDE styles.  If you
			do not specify this, then Kiltdown will be a regular
			X windows Application.

    -kdeinc <dir>     If you don't specify "kdedir", you can specify the
		        include files for KDE 2.1 separately
    -kdelib <dir>     If you don't specify "kdedir", you can specify the
			library files separately.

    -help             Display this message.

    You can also specify extra command line arguments for the compiler,
    linker, and runtime searchpath using:
    
    -I<directory>   -L<directory>    -R<directory> -l<libName>
    
    
EOF

    [ "x$ERROR" = "xyes" ] && exit 1
    exit 0;
fi    


########################
#
# Do some sanity checking on the values they gave us for incs and libs
#

#
# Qt 2.3
#
if [ -z "$QTINC" ]
then
    if [ -n "$QTDIR" ]
    then
        QTINC=$QTDIR/include
    fi
fi
if [ -z "$QTLIB" ]
then
    if [ -n "$QTDIR" ]
    then
        QTLIB=$QTDIR/lib
    fi
fi
if [ -n "$QTDIR" ]
then
    QTBIN=$QTDIR/bin/
fi

#
# DB 3
#
if [ -z "$DBINC" ]
then
    if [ -n "$DBDIR" ]
    then
        DBINC=$DBDIR/include
        DBLIB=$DBDIR/lib
    fi
fi
if [ -z "$DBLIB" ]
then
    if [ -n "$DBDIR" ]
    then
        DBLIB=$DBDIR/lib
    fi
fi

#
# KDE
#
if [ -z "$KDEINC" ]
then
    if [ -n "$CONF_KDEDIR" ]
    then
        KDEINC=$CONF_KDEDIR/include
        KDELIB=$CONF_KDEDIR/lib
    fi
fi
if [ -z "$KDELIB" ]
then
    if [ -n "$CONF_KDEDIR" ]
    then
        KDELIB=$CONF_KDEDIR/lib
    fi
fi




########################
#
# Verify the Qt Version
#
echo -n 'Verifying Qt version 2.2.3 ... '
SD="/usr/include /usr/X11R6/include /usr/local/include `echo $I_FLAGS | sed -e 's/-I//g'`"
if [ -z "$QTINC" ]
then
    SRCHDIRS=$SD
else
    if [ -n "$QTINC" ]
    then
        if [ -d "$QTINC" ]
        then
            SRCHDIRS=$QTINC
        else
            SRCHDIRS=$SD
        fi                
    else
        SRCHDIRS=$SD
    fi    
fi

for qd in $SRCHDIRS
do
    if [ -f $qd/qglobal.h ]
    then
        HAVEQGLOBAL=yes
        break
    fi    
done

if [ "x$HAVEQGLOBAL" != "xyes" ]
then
    echo 
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo "    Unable to determine what version of Qt you're"
    echo "    running.  Please verify the command line arguments"
    echo "    or the value of the $QTDIR environment variable"
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo
    
    exit 1;
fi


QTVERSION=`grep QT_VERSION $qd/qglobal.h | grep -v QT_VERSION_ | grep define | awk '{print $3}'`

if [ '!' 0$QTVERSION -eq 230 ]
then
    echo 
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo "You are not running a version of Qt that has been"
    echo "properly tested with Kiltdown.  Currently only"
    echo "Qt 2.2.3 has been verified to work acceptably."
    echo "(2.2.4 has been tested, and has too many bugs)."
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo
    
fi
echo ok.


########################
#
# Verify the DB Version
#
#
echo -n 'Verifying Berkeley DB version >= 3.1 ... '
SD="/usr/include /usr/local/include `echo $I_FLAGS | sed -e 's/-I//g'`"
if [ -z "$DBINC" ]
then
    SRCHDIRS=$SD
else
    if [ -n "$DBINC" ]
    then
        if [ -d "$DBINC" ]
        then
            SRCHDIRS=$DBINC
        else
            SRCHDIRS=$SD
        fi
    else
        SRCHDIRS=$SD
    fi                    
fi

for di in $SRCHDIRS
do
    if [ -f $di/db.h ]
    then
        HAVEDGLOBAL=yes
        break
    fi    
done

if [ "x$HAVEDGLOBAL" != "xyes" ]
then
    echo 
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo "    Unable to determine what version of DB you're"
    echo "    running.  Please verify the command line arguments"
    echo "    or the value of the $DBDIR environment variable"
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo
    
    exit 1;
fi

DBVM=`grep 'DB_VERSION_MAJOR' $di/db.h | awk '{print $3}'`
DBVN=`grep 'DB_VERSION_MINOR' $di/db.h | awk '{print $3}'`

if [ '!' 0$DBVM -eq 3 ]
then
    DBVGOOD=no
fi
if [ '!' 0$DBVM -ge 1 ]
then
    DBVGOOD=no
fi

if [ "x$DBVGOOD" = "xno" ]
then
    echo 
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo
    echo "* You are not running a version of Berkely DB that has been"
    echo "  properly tested with Kiltdown.  Currently only"
    echo '  3.x (x >= 1) have been tested and found to work acceptably'
    echo "  well"
    echo
    echo '* Please note that many systems have <db.h> in /usr/include'
    echo "  but this is often not the right version.  You will need to"
    echo "  download db 3.1.17 or db 3.2.9 from www.sleepycat.com and"
    echo "  specify that directory on the command line for $0"
    echo
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo
    
    exit 1;
fi
echo ok.







########################
#
# Verify the KDE Version
#
#
if [ "x$KDEINC" != "x" ]
then

    echo -n 'Verifying KDE version >= 2 ... '
    SD="/usr/include /usr/local/include /usr/include/kde /usr/local/kde/include `echo $I_FLAGS | sed -e 's/-I//g'`"
    if [ -z "$KDEINC" ]
    then
        SRCHDIRS=$SD
    else
        if [ -n "$KDEINC" ]
        then
            if [ -d "$KDEINC" ]
            then
                SRCHDIRS=$KDEINC
            else
                SRCHDIRS=$SD
            fi
        else
            SRCHDIRS=$SD
        fi                    
    fi

    for di in $SRCHDIRS
    do
        if [ -f $di/kapp.h ]
        then
            HAVEDGLOBAL=yes
            break
        fi    
    done

    if [ "x$HAVEDGLOBAL" != "xyes" ]
    then
        echo 
        echo
        echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
        echo
        echo "    Unable to determine what version of KDE you're"
        echo "    running.  Please verify the command line arguments."
        echo
        echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
        echo
        echo
    
        exit 1;
    fi

    KDEVM=`grep 'KDE_VERSION_MAJOR' $di/kapp.h | awk '{print $3}'`

    if [ '!' 0$KDEVM -eq 2 ]
    then
        KDEVGOOD=no
    fi

    if [ "x$KDEVGOOD" = "xno" ]
    then
        echo 
        echo
        echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
        echo
        echo
        echo "* You are not running a version of KDE that will work with"
        echo "  Kiltdown.  Currently only 2.x versions will work."
        echo
        echo '* If you believe you are receiving this message in error, please'
        echo '  note that many systems have <kapp.h> in /usr/include/kde'
        echo "  but this is often not the right version.  You will need to"
        echo "  point this configure script at the KDE 2.x header files."
        echo
        echo
        echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
        echo
        echo
    
        exit 1;
    fi

    #
    # Great -- it worked.  Now set up the environment variables for this!
    #
    MWBASE=KMainWindow
    KDEFLAGS=-D_KILTDOWN_USEKDE
    I_KDEINC=-I$KDEINC
    if [ -n "$KDELIB" ]
    then
        L_KDELIB=-L$KDELIB
    fi
    l_KDELIB="-lkdeui -lkdecore"
    echo ok.
else

    #
    # No KDE
    #
    MWBASE=QMainWindow
    KDEFLAGS=
fi




########################
#
# Figure out the platform information
#
#
echo -n "Verifying platform ... "
OS_NAME=`uname -s`
OS_VERSION=`uname -r`


if [ -z "$PLATFORM" ]
then
    case "$OS_NAME:$OS_VERSION" in

      Linux:*)
          PLATFORM=linux-generic
          ;;
        FreeBSD:*)
            PLATFORM=bsd-generic
            ;;
        OpenBSD:*)
            PLATFORM=bsd-generic
            ;;
        NetBSD:*)
            PLATFORM=bsd-generic
            ;;
        SunOS:5.*)
            PLATFORM=solaris-gcc
            WARNINGS="
            
    Currently, on Solaris, we only support building with GNU
    utilities -- in particular gcc, (g)make, and libstdc++.
    For the latter, you will need to make sure that libstdc++
    is both in your LD_LIBRARY_PATH to BOTH BUILD *AND* RUN 
    Kiltdown.
    
    i.e. (csh)  # setenv LD_LIBRARY_PATH /usr/local/lib
         (sh)   # LD_LIBRARY_PATH=/usr/local/lib
                # export LD_LIBRARY_PATH

    Your system administrator should really ensure that this 
    library is always in the search path if it is installed
    (thus making the above unnecessary).

"
            ;;
        *)
            echo Fatal Error.
            echo
            echo "   $0 was unable to find a platform suitably matched to your"
            echo "   computer.  You can try to run $0 with \"-platform generic\""
            echo "   on the command line.  This might require some tweaking of"
            echo "   Makefile variables, but should be very close."
            echo
            echo "   Either way, please send mail to mw@kiltdown.org so he can try"
            echo "   and build a configuration file for your OS."
            echo
            echo
            
            exit 1;
    esac
fi

CONFIG=$CURDIR/configs/$PLATFORM

if [ '!' -f $CONFIG  ]
then
    echo
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo "   The config file \"$PLATFORM\" is not in $CURDIR/configs. Something"
    echo "   is wrong with the source package."
    echo
    echo 'DANGER WILL ROBINSON!!  DANGER WILL ROBINSON!!  DANGER!!! DANGER !!!'
    echo
    echo
    
    exit 1;
fi

echo done


########################
#
# Okay, now create the Makefiles from Makefile.in.
#
for mkf in `find $CURDIR -name Makefile.in -print`
do
    mkfb=`echo $mkf | sed -e 's/.in$//'`
    echo -n "Generating $mkfb ... "
    rm -f $mkfb

    cat >> $mkfb <<EOF
#
##################################################
# This is a generated Makefile.  It was generated
# from $0 and Makefile.in.
##################################################
#

QTINC=$QTINC
QTLIB=$QTLIB
DBINC=$DBINC
DBLIB=$DBLIB
QTBIN=$QTBIN

USER_INC=$I_FLAGS $I_KDEINC
USER_LPATH=$L_FLAGS $L_KDELIB
USER_LIBS=$l_FLAGS $l_KDELIB
USER_RPATH=$R_FLAGS
JPEGLIB=-ljpeg

OPT_DEFINES=$DEBUG $KDEFLAGS
MWBASE=$MWBASE

EOF

    cat $CONFIG >> $mkfb
    cat $mkf >> $mkfb    

    echo "done"
done

########################
#
# Print out any warnings we may have generated along the way.
#
if [ -n "$WARNINGS" ]
then
   echo $WARNINGS
fi

########################
#
# finito
#

echo
echo
echo 'Please be sure, if you have configured Qt 2.3.0 with the'
echo '-xft option, that you have added -lXft to this configuration'
echo 'command (type "make distclean" and then "configure ..." to run'
echo 'again).  If not, you will see a bunch of link errors when'
echo 'compiling.'
echo
echo Configuration Complete
echo




syntax highlighted by Code2HTML, v. 0.9.1