#!/bin/sh
#
# Configures to build the Qt library
#
# Copyright 1999-2000 Troll Tech AS.  All rights reserved.
#

# To remove the need for the first test, do this:
# QTDIR=`pwd` export QTDIR

relpath=`dirname $0`
relpath=`(cd $relpath; pwd)`

touch .test.qt.
if [ '!' -f ${QTDIR}/.test.qt. ];
then
    echo
    echo
    echo '   The environment variable $QTDIR is not set correctly. It is currently'
    echo '   set to "'$QTDIR'", but it should be set to this directory,'
    echo '   which is "'`pwd`'".'
    echo
    echo '   Please read the INSTALL file for instructions on how to set $QTDIR'
    echo '   correctly. If you have set $QTDIR in your .profile or .login, you '
    echo '   will need to log out and log in again to make the setting effective.'
    echo
    echo
    exit 1
fi
rm .test.qt.


# Remove the build options file
[ -f .buildopts ] && rm .buildopts


# Defaults

DEBUG=no
SHARED=yes
GIF=no
SM=no
THREAD=no
ZLIB=yes
LIBPNG=yes
JPEG=no
NAS_SOUND=no
I_FLAGS=
L_FLAGS=
l_FLAGS=

# Parse the arguments, setting things to "yes" or "no".

while [ -n "$1" ]; do
   case $1 in
   -platform)
       shift; PLATFORM=$1
	;;
   -release)
       DEBUG=no
	;;
   -debug)
       DEBUG=yes
	;;
   -shared)
       SHARED=yes
	;;
   -static)
       SHARED=no
	;;
   -no-gif)
       GIF=no
	;;
   -gif)
       GIF=yes
	;;
   -no-sm)
       SM=no
	;;
   -sm)
       SM=yes
	;;
   -no-thread)
       THREAD=no
	;;
   -thread)
       THREAD=yes
	;;
   -qt-zlib)
       ZLIB=yes
	;;
   -system-zlib)
       ZLIB=no
	;;
   -qt-libpng)
       LIBPNG=yes
	;;
   -system-libpng)
       LIBPNG=no
	;;
   -no-jpeg)
       JPEG=no
	;;
   -system-jpeg)
       JPEG=yes
	;;
   -no-nas-sound)
       NAS_SOUND=no
	;;
   -system-nas-sound)
       NAS_SOUND=yes
	;;
   -h | -help | --help)
       HELP=yes
	;;
   -v)
       VERBOSE=yes
        ;;
   -I?*)
	I_FLAGS="$I_FLAGS $1"
	;;
   -L?*)
	L_FLAGS="$L_FLAGS $1"
	;;
   -l?*)
	l_FLAGS="$l_FLAGS $1"
	;;
   -I)
	shift
	I_FLAGS="$I_FLAGS -I$1"
	;;
   -L)
	shift
	L_FLAGS="$L_FLAGS -L$1"
	;;
   *)
       echo $1: unknown argument
       HELP=yes;
       ERROR=yes
	;;
   esac
   shift
done


# Next, emit a usage message if something failed.

if [ "$HELP" = "yes" ]; then
   [ "x$ERROR" = "xyes" ] && echo
   cat <<EOF
Usage: $0 [-debug] [-release] [-gif] [-no-gif] [-sm] [-no-sm] \\
        [-qt-zlib] [-system-zlib] [-qt-libpng] [-system-libpng] \\
        [-no-jpeg] [-system-jpeg] [-no-thread] [-thread] \\
        [-Istring] [-lstring] [-Lstring] \\

The defaults (*) are usually acceptable.  Here is a short explanation of
each option:

 *  -release ........... Compile and link Qt with debugging turned off.
    -debug ............. Compile and link Qt with debugging turned on.

 *  -shared ............ Create and use a shared Qt library (libqt.so)
    -static ............ Create and use a static Qt library (libqt.a)

 *  -no-gif ............ Do not compile in GIF reading support.
    -gif ............... Compile in GIF reading support. See src/kernel/qgif.h

 *  -no-sm ............. Do not support X Session Management.
    -sm ................ Support X Session Management, links in -lSM -lICE.

 *  -no-thread ......... Do not compile with Threading Support
    -thread ............ Compile with Threading Support(links in -lpthread)

 *  -qt-zlib ........... Use the zlib bundled with Qt.
    -system-zlib ....... Use a zlib from the operating system

 *  -qt-libpng ......... Use the libpng bundled with Qt.
    -system-libpng ..... Use a libpng from the operating system.

 *  -no-jpeg ........... Do not compile in JPEG I/O support.
    -system-jpeg ....... Use libjpeg from the operating system.

 *  -no-nas-sound ...... Do not compile in NAS sound support.
    -system-nas-sound .. Use NAS libaudio from the operating system.

    -platform target ... See the PLATFORMS file for a list of supported
                         operating systems and compilers.

    -Istring ........... Add an explicit include path.
    -Lstring ........... Add an explicit library path.
    -lstring ........... Add an explicit library.

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


# DISABLED: If gif hasn't been set explicitly, ask.
#while [ "x$GIF" = "x" ]; do
#   echo 'Compile in GIF support? (yes/no) ' | tr -d '
#'
#    read GIF;
#    case $GIF in
#    y* | Y*)
#        GIF=yes;;
#    n* | N*)
#        GIF=no;;
#    *)
#        GIF="";;
#    esac
#done

UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown
UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown

PLATFORM_NOTES=""

if [ -z "$PLATFORM" ]
then
    ##### Generate?  Look in configs/?
    PLATFORMS="aix-g++ aix-xlc bsdi-g++ dgux-g++ freebsd-g++ gnu-g++ hpux-acc
	hpux-cc hpux-g++ hpux-n64 hpux-o64 irix-64 irix-dcc irix-g++ irix-n32
	irix-o32 linux-g++ netbsd-g++ openbsd-g++ osf1-cxx osf1-g++ qnx-g++
	sco-g++ solaris-cc-gcc solaris-cc solaris-64 solaris-g++ sunos-g++
	ultrix-g++ unixware-g++ unixware7-cc unixware7-g++"

    case "$UNAME_SYSTEM:$UNAME_RELEASE" in
     AIX*)
        #PLATFORM=aix-g++
	PLATFORM=aix-xlc
	PLATFORM_NOTES="
	    - Also available: aix-g++
	"
	;;
     QNX:*)
	PLATFORM=qnx-g++
	;;
     GNU:*)
	PLATFORM=gnu-g++
	;;
     BSD/386:*)
	PLATFORM=bsdi-g++
	;;
     dgux:*)
	PLATFORM=dgux-g++
	;;
     ULTRIX:*)
	PLATFORM=ultrix-g++
	;;
     FreeBSD:*)
	PLATFORM=freebsd-g++
	;;
     OpenBSD:*)
	PLATFORM=openbsd-g++
	;;
     NetBSD:*)
	PLATFORM=netbsd-g++
	;;
     IRIX64:*)
	PLATFORM=irix-64
	;;
     IRIX:*)
	#PLATFORM=irix-dcc
	#PLATFORM=irix-g++
	PLATFORM=irix-n32
	#PLATFORM=irix-o32
	PLATFORM_NOTES="
	    - Also available for IRIX: irix-dcc irix-g++ irix-o32
	"
	;;
     HP-UX:*)
	#PLATFORM=hpux-acc
	#PLATFORM=hpux-cc
	#PLATFORM=hpux-n64
	#PLATFORM=hpux-o64
	PLATFORM=hpux-g++
	PLATFORM_NOTES="
	    - Also available for HP-UX: hpux-acc hpux-cc hpux-n64 hpux-o64
	"
	;;
     OSF1:*)
	#PLATFORM=osf1-cxx
	PLATFORM=osf1-g++
	PLATFORM_NOTES="
	    - Also available for OSF1: osf1-cxx
	"
	;;
     Linux:*)
	PLATFORM=linux-g++
	;;
     SunOS:5*)
	#PLATFORM=solaris-g++
	PLATFORM=solaris-cc
	PLATFORM_NOTES="
	    - Also available for SunOS:5: solaris-64 solaris-g++
	    - To mix GNU C and SunPRO C++, add -platform solaris-cc-gcc
	"
	;;
     SunOS:4*)
	PLATFORM=sunos-g++
	PLATFORM_NOTES="
	    - Mixing GNU C and SunPRO C++ is not supported
	"
	;;
     UNIX_SV:4.2*)
	PLATFORM=unixware-g++
	#PLATFORM=unixware7-cc
	#PLATFORM=unixware7-g++
	PLATFORM_NOTES="
	    - Also available for UNIX_SV:4.2: unixware7-cc unixware7-g++
	"
	;;
     *:3.2)
	PLATFORM=sco-g++
	;;
     *)
	echo
	for p in $PLATFORMS
	do
	    echo "    $0 $* -platform $p"
	done
	echo
	echo "The build script does not currently recognize all platforms"
	echo "supported by Qt."
	echo "Rerun this script with a -platform option listed to"
	echo "set the operating system / compiler combination you use."
	echo
	exit 2
    esac
fi


# Check if the platform + static + debug exists

TARGET=$PLATFORM
if [ "x$SHARED" = "xyes" ]; then
    TARGET="${TARGET}-shared"
    if [ '!' -f $relpath/configs/$TARGET ]; then
        echo
        echo
        echo '    No shared library support for platform/compiler ' $TARGET
        echo '    Please see the PLATFORMS file for more information.'
        echo
        echo
        exit 2
    fi
else
    TARGET="${TARGET}-static"
    if [ '!' -f $relpath/configs/$TARGET ]; then
        echo
        echo
        echo '     No static library support for platform/compiler ' $TARGET
        echo '    Please see the PLATFORMS file for more information.'
        echo
        echo
        exit 2
    fi
fi

if [ "x$DEBUG" = "xyes" ]; then
    TARGET="${TARGET}-debug"
fi

CONFIG="$relpath/configs/${TARGET}"
if [ -z "$TARGET" -o '!' -f "$CONFIG" ];
then
    echo
    echo
    echo '   The specified platform/compiler not supported: ' $TARGET
    echo '   Please see the PLATFORMS file for a complete list.'
    echo
    echo
    exit 2
fi


# Build up the variables for output

QT_CXX=""
[ "x$GIF" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_BUILTIN_GIF_READER"
[ "x$JPEG" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_JPEG_SUPPORT"
[ "x$NAS_SOUND" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_NAS_SUPPORT"
[ "x$SM" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_SM_SUPPORT"
[ "x$THREAD" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_QT_THREAD_SUPPORT"
[ "x$ZLIB" = "xyes" ] && I_FLAGS="${I_FLAGS} -I\$(QTDIR)/src/3rdparty/zlib"
[ "x$LIBPNG" = "xyes" ] && I_FLAGS="${I_FLAGS} -I\$(QTDIR)/src/3rdparty/libpng"

QT_LIBS=""
QT_PNG=""
QT_ZLIB=""
[ "x$SM" = "xyes" ] && QT_LIBS="${QT_LIBS} -lSM -lICE"
[ "x$THREAD" = "xyes" ] && QT_LIBS="${QT_LIBS} -lpthread"
[ "x$LIBPNG" = "xyes" ] && QT_PNG=' $(PNG_OBJECTS)'
[ "x$ZLIB" = "xyes" ] && QT_ZLIB=' $(ZLIB_OBJECTS)'
[ "x$LIBPNG" = "xno" ] && QT_LIBS="${QT_LIBS} -lpng"
[ "x$ZLIB" = "xno" ] && QT_LIBS="${QT_LIBS} -lz"
[ "x$JPEG" = "xyes" ] && QT_LIBS="${QT_LIBS} -ljpeg"
[ "x$NAS_SOUND" = "xyes" ] && QT_LIBS="${QT_LIBS} -laudio -lXt" # Xt junk in audio library
QT_LIBS="$L_FLAGS $QT_LIBS $l_FLAGS"


# Create the .buildopts file

cat > .buildopts <<EOF
QT_CXXFLAGS_OPT =$QT_CXX $I_FLAGS
QT_CFLAGS_OPT   =$I_FLAGS
QT_LIBS_OPT	=$QT_LIBS
QT_PNG_OBJ      =$QT_PNG
QT_ZLIB_OBJ	=$QT_ZLIB

EOF


QTDIR='$(QTDIR)'


# Give feedback...

echo
echo "Build type:    $TARGET"
if [ -n "$PLATFORM_NOTES" ]
then
    echo "Platform notes:"
    echo "$PLATFORM_NOTES"
else
    echo
fi
echo "Compile flags: $QT_CXX $I_FLAGS"
echo "Link flags:    $QT_LIBS"
echo "GIF supports:  $GIF"
echo
echo Creating makefiles...

sepath=`echo $relpath | sed -e 's/\\./\\\\./g'`

for a in `find $relpath/. -name Makefile.in -print | sort` ; do
    buildpath=`dirname $a | sed -e "s;$sepath;.;g"`
    test -d $buildpath || mkdir $buildpath
    N=$buildpath/Makefile
    M=$a
    cat > $N <<EOF
#############################################################################
# Automatically generated from $M
# Build options from $1
#############################################################################

EOF

    if [ $relpath != `pwd` ] ; then
        echo VPATH=$relpath/$buildpath >> $N
	cat .buildopts $CONFIG >> $N
	sed -e's/	\$(MOC) [^ ]* -o .*/	$(MOC) $< -o $@/g' \
	    -e's/\(	\$(CXX) -c \$(CXXFLAGS) \$(INCPATH) -o\) [^ ]* [^ ]*/\1 $@ $</g' $M >> $N
    else
	cat .buildopts $CONFIG $M >> $N
    fi
    if [ "$VERBOSE" = "yes" ]; then
       echo '  Created' $N
    fi
done

echo
echo Qt is now configured for building. Just run make.
echo To reconfigure, run make clean and configure.
echo
