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

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

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
ZLIB=yes
LIBPNG=yes
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
	;;
   -qt-zlib)
       ZLIB=yes
	;;
   -system-zlib)
       ZLIB=no
	;;
   -qt-libpng)
       LIBPNG=yes
	;;
   -system-libpng)
       LIBPNG=no
	;;
   -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] \\
        [-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 support. Please see qgif.h.
    -gif ............... Compile in GIF support. Please see qgif.h.

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

 *  -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.

    -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++ 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-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++
	PLATFORM_NOTES="
	    - WARNING: DG/ux not supported correctly in this version of Qt.
	"
	;;
     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-g++
	PLATFORM_NOTES="
	    - Also available for HP-UX: hpux-acc hpux-cc
	"
	;;
     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="
	    - 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 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 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="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$SM" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_SM_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} -lICE -lSM"
[ "x$ZLIB" = "xyes" ] && QT_ZLIB=' $(ZLIB_OBJECTS)'
[ "x$LIBPNG" = "xyes" ] && QT_PNG=' $(PNG_OBJECTS)'
[ "x$ZLIB" = "xno" ] && QT_LIBS="${QT_LIBS} -lz"
[ "x$LIBPNG" = "xno" ] && QT_LIBS="${QT_LIBS} -lpng"
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...

for a in `find . -name Makefile.in -print | sort` ; do
    N=`dirname $a`/Makefile
    M=${N}.in
    cat > $N <<EOF
#############################################################################
# Automatically generated from $M
# Build options from $1
#############################################################################

EOF
    cat .buildopts $CONFIG $M >> $N
    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
