#!/bin/sh
#
# Qtopia 2.2 "single-package" configure script
#

export QPE_ROOT="$(/bin/pwd)"

relpath="$(dirname $0)"
relpath="$(cd "$relpath"; /bin/pwd)"

if [ "$QPE_ROOT" != "$relpath" ]; then
    echo "You must run configure from $relpath!"
    exit 1
fi

export QPEDIR=$QPE_ROOT/qtopia
export QTOPIA_DEPOT_PATH=$QPE_ROOT/qtopia_depot
export TMAKEDIR=$QPE_ROOT/tmake
export DQTDIR=$QPE_ROOT/dqt
export QT2DIR=$QPE_ROOT/qt2
export QTEDIR=$QPE_ROOT/qt2

if [ ! -d "$QTOPIA_DEPOT_PATH" ]; then
    # probably a source package... don't shadow build
    export QTOPIA_DEPOT_PATH=$QPEDIR
fi

# Now "follow" the symlinks
for var in QPEDIR QTOPIA_DEPOT_PATH TMAKEDIR DQTDIR QT2DIR QTEDIR; do
    val="$(eval echo '$'$var)"
    if [ ! -d $val ]; then
        echo "Missing $val ($var). Please check that you unpacked the package correctly."
        exit 1
    fi
    eval $var'="$(cd $val; /bin/pwd)"'
done

# Check the edition flags
grep 'phone' $QTOPIA_DEPOT_PATH/.configureoptions >/dev/null 2>&1 && phone=yes || phone=no
grep 'pda'   $QTOPIA_DEPOT_PATH/.configureoptions >/dev/null 2>&1 && pda=yes || pda=no

# Setup the Qt 2 and 3 include directories (done now so we don't need to ship packages with symlinks)
for dir in $DQTDIR $QT2DIR; do
    if [ -f "$dir/bin/syncqt" ]; then
        # don't affect the script's environment
        (
            export QTDIR=$dir
            cd $QTDIR
            $QTDIR/bin/syncqt >/dev/null 2>&1
        )
    fi
done

# Check the license
if [ -f LICENSE.GPL ]; then
    Product=Free
    LicenseFile=LICENSE.GPL
    DestLicenseFile=LICENSE.GPL
elif [ -f LICENSE.TROLL ]; then
    Product=Trolltech
    LicenseFile=LICENSE.TROLL
    DestLicenseFile=LICENSE.TROLL
else
    DestLicenseFile=LICENSE.Qtopia
    # read in the license file
    if [ -f $HOME/.qt-license ]; then
        . $HOME/.qt-license >/dev/null 2>&1
        if [ -z "$LicenseKeyExt" ]; then
            echo
            echo "You are using an old license file."
            echo
            echo "Please install the license file supplied by Trolltech,"
            echo "or install the Qt Open Source Edition if you intend to"
            echo "develop free software."
            exit 1
        fi
    if [ -z "$Licensee" ]; then
        echo
        echo "Invalid license key. Please check the license key."
        exit 1
    fi
    else
        echo
        echo "Missing $HOME/.qt-license. Please install your license file to proceed"
        exit 1
    fi

    # Check that Qt/Embedded can be built properly
    echo $Products | grep 'qt-enterprise' >/dev/null 2>&1 \
        && ProductsValid=yes \
        || ProductsValid=no
    if [ "$ProductsValid" != "yes" ]; then
        echo
        echo "Your license file does not permit building Qt/Embedded 2.3 Enterprise Edition."
        echo "Please check your license file."
        exit 1
    fi

    # Key verification
    echo $LicenseKeyExt | grep ".....*-....*-....*-....*-.....*-.....*-...." >/dev/null 2>&1 \
        && LicenseValid="yes" \
        || LicenseValid="no"
    if [ "$LicenseValid" != "yes" ]; then
        echo
        echo "Invalid license key. Please check the license key."
        exit 1
    fi
    ProductCode=`echo $LicenseKeyExt | cut -f 1 -d - | cut -b 1`
    PlatformCode=`echo $LicenseKeyExt | cut -f 2 -d - | cut -b 1`
    LicenseTypeCode=`echo $LicenseKeyExt | cut -f 3 -d -`
    LicenseFeatureCode=`echo $LicenseKeyExt | cut -f 4 -d - | cut -b 1`

    # determine which edition we are licensed to use
    case $LicenseTypeCode in
    F4M)
        LicenseType="Commercial"
        ;;
    Z4M|R4M|Q4M)
        LicenseType="Evaluation"
        ;;
    esac
    if [ -z "$LicenseType" ]; then
        echo
        echo "Invalid license key. Please check the license key."
        exit 1
    fi

    case $LicenseFeatureCode in
    G)
        # US
        case "$LicenseType" in
        Commercial)
            cp -f $QPE_ROOT/.LICENSE-US $QPE_ROOT/LICENSE
            ;;
        Evaluation)
            cp -f $QPE_ROOT/.LICENSE-EVALUATION-US $QPE_ROOT/LICENSE
            ;;
        esac
        ;;
    2)
        # non-US
        case "$LicenseType" in
        Commercial)
            cp -f $QPE_ROOT/.LICENSE $QPE_ROOT/LICENSE
            ;;
        Evaluation)
            cp -f $QPE_ROOT/.LICENSE-EVALUATION $QPE_ROOT/LICENSE
            ;;
        esac
        ;;
    *)
        echo
        echo "Invalid license key. Please check the license key."
        exit 1
        ;;
    esac
    if [ '!' -f $QPE_ROOT/LICENSE ]; then
        echo "The LICENSE or LICENSE.GPL file shipped with"
        echo "this software has disappeared."
        echo
        echo "Sorry, you are not licensed to use this software."
        echo "Try re-installing."
        echo
        exit 1
    fi
    Product="$LicenseType"
    [ $pda = yes -a $phone = yes ] && Product="$Product Refboard"
    [ $pda = yes -a $phone = no ] && Product="$Product PDA"
    [ $pda = no -a $phone = yes ] && Product="$Product Phone"
    [ $pda = no -a $phone = no ] && Product="$Product Core"
    LicenseFile=LICENSE
fi

# Setup the license files for the Qt packages
for location in $DQTDIR $QT2DIR $QTEDIR $QTOPIA_DEPOT_PATH $QPEDIR; do
    # Remove any existing license files
    rm -f $location/LICENSE* $location/.LICENSE*
    # Put a single Qtopia license file there
    cp -f $PWD/$LicenseFile $location/$DestLicenseFile
done

# default configuration variables
DEFAULT_QT2_CFG="-no-opengl -no-xft -no-sm"
DEFAULT_DQT_CFG="-thread -qt-gif -fast"

DEFAULT_QTE_KEYPAD_CFG="-embedded -no-xft -qconfig qpe -qvfb -depths 4,8,16,32 -keypad-mode -system-jpeg -gif"
DEFAULT_QTE_NOKEYPAD_CFG="-embedded -no-xft -qconfig qpe -qvfb -depths 4,8,16,32 -system-jpeg -gif -D QT_TRANSFORM_VFB"
DEFAULT_QTE_ARM_KEYPAD_CFG="-embedded -no-xft -xplatform linux-sharp-g++ -qconfig qpe -keypad-mode -depths 16,32 -no-qvfb -system-jpeg -gif"
DEFAULT_QTE_ARM_NOKEYPAD_CFG="-embedded -no-xft -xplatform linux-sharp-g++ -qconfig qpe -depths 16,32 -no-qvfb -system-jpeg -gif"
if [ $phone = yes ]; then
    DEFAULT_QTE_CFG="$DEFAULT_QTE_KEYPAD_CFG"
else
    DEFAULT_QTE_CFG="$DEFAULT_QTE_NOKEYPAD_CFG"
fi

DEFAULT_QPE_PHONE_CFG="-edition phone -displaysize 176x208"
DEFAULT_QPE_PDA_CFG="-edition pda -displaysize 240x320"
DEFAULT_QPE_CORE_CFG="-edition core -displaysize 240x320"
DEFAULT_QPE_ARM_PHONE_CFG="-xplatform linux-sharp-g++ -edition phone -displaysize 176x208"
DEFAULT_QPE_ARM_PDA_CFG="-xplatform linux-sharp-g++ -edition pda -displaysize 240x320"
DEFAULT_QPE_ARM_CORE_CFG="-xplatform linux-sharp-g++ -edition core -displaysize 240x320"
if [ $phone = yes ]; then
    DEFAULT_QPE_CFG="$DEFAULT_QPE_PHONE_CFG"
elif [ $pda = yes ]; then
    DEFAULT_QPE_CFG="$DEFAULT_QPE_PDA_CFG"
else
    DEFAULT_QPE_CFG="$DEFAULT_QPE_CORE_CFG"
fi

quote()
{
    if [ $# -eq 1 ]; then
        echo "$1"
    else
        ret=
        for word in "$@"; do
            [ -n "$ret" ] && ret="${ret} "
            ret="${ret}'$word'"
        done
        echo $ret
    fi
}

EXTRA_QTOPIA_LIB_PATH=
HELP=no
USE_CACHE=no
RECONF=no
PREFIX=
DPREFIX=
DEBUG=no
QTOPIADESKTOP=no

# Read in the user-supplied defaults
[ -f config.defaults ] && . ./config.defaults

# Parse the arguments
while [ -n "$1" ]; do
    case "$1" in
    -h | -help | --help)
        HELP=yes
        ;;
    -qte)
        shift
        QTE_CFG="$(eval quote $1)"
        ;;
    -qpe)
        shift
        QPE_CFG="$(eval quote $1)"
        ;;
    -qt2)
        shift
        QT2_CFG="$(eval quote $1)"
        ;;
    -dqt)
        shift
        DQT_CFG="$(eval quote $1)"
        ;;
    -cache)
        USE_CACHE=yes
        ;;
    -libpath)
        shift
        EXTRA_QTOPIA_LIB_PATH="$1"
        ;;
    -reconf)
        RECONF=yes
        ;;
    -prefix)
        shift
        PREFIX="$1"
        ;;
    -dprefix)
        shift
        DPREFIX="$1"
        ;;
    -debug)
        DEBUG=yes
        ;;
    -qtopiadesktop)
        QTOPIADESKTOP=yes
        ;;
    *)
        echo $1: unknown argument
        HELP=yes
        ;;
    esac
    shift
done

if [ $RECONF = yes ]; then
    . ./config.cache
else
    # Try to guess the embedded configuration to use based on the Qtopia configuration selected
    if [ -z "$QTE_CFG" ]; then
        if [ "$QPE_CFG" = "phone" ]; then
            QTE_CFG=keypad
        elif [ "$QPE_CFG" = "pda" ]; then
            QTE_CFG=no-keypad
        elif [ "$QPE_CFG" = "core" ]; then
            QTE_CFG=no-keypad
        elif [ "$QPE_CFG" = "arm-phone" ]; then
            QTE_CFG=arm-keypad
        elif [ "$QPE_CFG" = "arm-pda" ]; then
            QTE_CFG=arm-no-keypad
        elif [ "$QPE_CFG" = "arm-core" ]; then
            QTE_CFG=arm-no-keypad
        fi
    fi

    [ -z "$QPE_CFG" ] && QPE_CFG="$DEFAULT_QPE_CFG"
    [ -z "$QTE_CFG" ] && QTE_CFG="$DEFAULT_QTE_CFG"
    [ -z "$QT2_CFG" ] && QT2_CFG="$DEFAULT_QT2_CFG"
    [ -z "$DQT_CFG" ] && DQT_CFG="$DEFAULT_DQT_CFG"

    # check if a shortcut is specified as the QTE config
    if [ "$QTE_CFG" = "keypad" ]; then
        QTE_CFG="$DEFAULT_QTE_KEYPAD_CFG"
    elif [ "$QTE_CFG" = "no-keypad" ]; then
        QTE_CFG="$DEFAULT_QTE_NOKEYPAD_CFG"
    elif [ "$QTE_CFG" = "arm-keypad" ]; then
        QTE_CFG="$DEFAULT_QTE_ARM_KEYPAD_CFG"
    elif [ "$QTE_CFG" = "arm-no-keypad" ]; then
        QTE_CFG="$DEFAULT_QTE_ARM_NOKEYPAD_CFG"
    fi

    # check if a shortcut is specified as the QPE config
    if [ "$QPE_CFG" = "phone" ]; then
        QPE_CFG="$DEFAULT_QPE_PHONE_CFG"
    elif [ "$QPE_CFG" = "pda" ]; then
        QPE_CFG="$DEFAULT_QPE_PDA_CFG"
    elif [ "$QPE_CFG" = "core" ]; then
        QPE_CFG="$DEFAULT_QPE_CORE_CFG"
    elif [ "$QPE_CFG" = "arm-phone" ]; then
        QPE_CFG="$DEFAULT_QPE_ARM_PHONE_CFG"
    elif [ "$QPE_CFG" = "arm-pda" ]; then
        QPE_CFG="$DEFAULT_QPE_ARM_PDA_CFG"
    elif [ "$QPE_CFG" = "arm-core" ]; then
        QPE_CFG="$DEFAULT_QPE_ARM_CORE_CFG"
    fi

    # Allow -qpe [-no]-qtopiadesktop to override our option
    echo "$QPE_CFG" | grep -- '-no-qtopiadesktop' >/dev/null 2>&1 && QTOPIADESKTOP=no
    # don't match against -no-qtopiadesktop!
    echo "$QPE_CFG" | grep -- '^-qtopiadesktop' >/dev/null 2>&1 && QTOPIADESKTOP=yes
    echo "$QPE_CFG" | grep -- '[^o]-qtopiadesktop' >/dev/null 2>&1 && QTOPIADESKTOP=yes

    QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-no-qtopiadesktop["'"'"']*[[:space:]]*/ /')"
    QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-qtopiadesktop["'"'"']*[[:space:]]*/ /')"
    if [ $QTOPIADESKTOP = yes ]; then
        QPE_CFG="$QPE_CFG -qtopiadesktop"
    else
        QPE_CFG="$QPE_CFG -no-qtopiadesktop"
    fi

    if [ -n "$EXTRA_QTOPIA_LIB_PATH" ]; then
        QPE_CFG="$QPE_CFG -L '$EXTRA_QTOPIA_LIB_PATH' -R '$EXTRA_QTOPIA_LIB_PATH'"
    fi

    # Allow -qpe -prefix to override our option
    echo "$QPE_CFG" | grep -- '-prefix' >/dev/null 2>&1 && prefix=yes || prefix=no
    if [ $prefix = yes ]; then
        PREFIX="$(echo "$QPE_CFG" | sed 's/.*-prefix[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
    fi
    if [ -n "$PREFIX" ]; then
        QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-prefix[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
        QPE_CFG="$QPE_CFG -prefix '$PREFIX'"
    fi

    # Allow -qpe -dprefix to override our option
    echo "$QPE_CFG" | grep -- '-dprefix' >/dev/null 2>&1 && dprefix=yes || dprefix=no
    if [ $dprefix = yes ]; then
        DPREFIX="$(echo "$QPE_CFG" | sed 's/.*-dprefix[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
    fi
    if [ -n "$DPREFIX" ]; then
        QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-dprefix[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
        QPE_CFG="$QPE_CFG -dprefix '$DPREFIX'"
    fi

    QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-debug["'"'"']*[[:space:]]*/ /')"
    QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-release["'"'"']*[[:space:]]*/ /')"
    QTE_CFG="$(echo "$QTE_CFG" | sed 's/[[:space:]]*["'"'"']*-debug["'"'"']*[[:space:]]*/ /')"
    QTE_CFG="$(echo "$QTE_CFG" | sed 's/[[:space:]]*["'"'"']*-release["'"'"']*[[:space:]]*/ /')"
    if [ $QTOPIADESKTOP = yes ]; then
        DQT_CFG="$(echo "$DQT_CFG" | sed 's/[[:space:]]*["'"'"']*-debug["'"'"']*[[:space:]]*/ /')"
        DQT_CFG="$(echo "$DQT_CFG" | sed 's/[[:space:]]*["'"'"']*-release["'"'"']*[[:space:]]*/ /')"
    fi
    if [ $DEBUG = yes ]; then
        QPE_CFG="$QPE_CFG -debug"
        QTE_CFG="$QTE_CFG -debug"
        if [ $QTOPIADESKTOP = yes ]; then
            DQT_CFG="$DQT_CFG -debug"
        fi
    else
        QPE_CFG="$QPE_CFG -release"
        QTE_CFG="$QTE_CFG -release"
        if [ $QTOPIADESKTOP = yes ]; then
            DQT_CFG="$DQT_CFG -release"
        fi
    fi
fi

if [ "$HELP" = "yes" ]; then
    cat <<END
Usage:  configure [-qte 'cfg'] [-qpe 'cfg'] [-libpath path] [-prefix path]
                  [-debug] [-qtopiadesktop] [-dprefix path]
                  [-qt2 'cfg'] [-dqt 'cfg']

    -qte cfg ....... Specifies the configuration to use for building
                     Qt/Embedded. For a full description of all possible Qt
                     configuration options type:
                     $QT2DIR/configure -help
                     or read the getting started section in the online
                     reference manual which can be found at:
                     $QPEDIR/doc/html/index.html

                     For your convenience you can also use one of the following
                     shortcuts as the cfg value which will then be replaced
                     with the full configuration listed after the shortcut:
              keypad '$DEFAULT_QTE_KEYPAD_CFG'
          arm-keypad '$DEFAULT_QTE_ARM_KEYPAD_CFG'
           no-keypad '$DEFAULT_QTE_NOKEYPAD_CFG'
       arm-no-keypad '$DEFAULT_QTE_ARM_NOKEYPAD_CFG'

    -qpe cfg ....... Specifies the configuration to use for building Qtopia.
                     For a full description of all possible Qtopia
                     configuration options type:
                     $QPEDIR/configure -help
                     or read the getting started section in the online
                     reference manual which can be found at:
                     $QPEDIR/doc/html/index.html

                     For your convenience you can also use one of the following
                     shortcuts as the cfg value which will then be replaced
                     with the full configuration listed after the shortcut:
               phone '$DEFAULT_QPE_PHONE_CFG'
           arm-phone '$DEFAULT_QPE_ARM_PHONE_CFG'
                 pda '$DEFAULT_QPE_PDA_CFG'
             arm-pda '$DEFAULT_QPE_ARM_PDA_CFG'
                core '$DEFAULT_QPE_CORE_CFG'
            arm-core '$DEFAULT_QPE_ARM_CORE_CFG'

    -libpath path .. Adds an extra path to the Qtopia configuration from which
                     extra libraries may be included. For example, if
                     path=foobar then the following will be appended to the qpe
                     configuration: -L 'foobar' -R 'foobar'

    -prefix path ... Install Qtopia into path instead of
                     $QPEDIR/image/opt/Qtopia. (1)

    -debug ......... Build in debug mode. This affects Qt/Embedded, Qtopia and
                     (if Qtopia Desktop is being built) Qt 3. (2)

    -qtopiadesktop . Enable Qtopia Desktop (the desktop PC companion program
                     that allows you to synchronise data). (1)

    -dprefix path .. Install Qtopia Desktop into path instead of
                     $QPEDIR/dimage/opt/Qtopia. (1)

    -qt2 cfg ....... Specifies the configuration to use when building Qt 2
                     (tools package).

    -dqt cfg ....... Specifies the configuration to use for building Qt 3
                     (tools package).

    IMPORTANT: Please ensure that you quote configuration arguments to -qte,
               -qpe, -qt2 and -dqt so that your shell does not split the
               arguments.

    NOTE: Depending on the source package not all listed configurations may be
          possible.

(1) NOTE: The presence of these options in a -qpe parameter overrides the
          configure arguments.

(2) NOTE: Any -debug or -release arguments in qte/qte (and dqt if Qtopia
          Desktop is enabled) arguments will be ignored and this value used
          instead.

END
    exit 2
fi

# Check license stuff
echo
echo "This is the Qtopia $Product Edition."
echo
if [ "$LicenseFile" = LICENSE ]; then
    if [ -n "$ExpiryDate" ]; then
        ExpiryDate=`echo $ExpiryDate | sed -e "s,-,,g" | tr -d "\n\r"`
        [ -z "$ExpiryDate" ] && ExpiryDate="0"
        Today=`date +%Y%m%d`
        if [ "$Today" -gt "$ExpiryDate" ]; then
            echo
            echo "WARNING  WARNING  WARNING  WARNING"
            echo
            echo "  Your support and upgrade period has expired."
            echo
            echo "  You may continue to use your last licensed release"
            echo "  of Qt under the terms of your existing license"
            echo "  agreement. But you are not entitled to technical"
            echo "  support, nor are you entitled to use any more recent"
            echo "  Qt releases."
            echo
            echo "  Please contact sales@trolltech.com to renew your"
            echo "  support and upgrades for this license."
            echo
            echo "WARNING  WARNING  WARNING  WARNING"
            echo
            sleep 3
        fi
    fi
fi
if [ "$LicenseFile" != LICENSE.TROLL ]; then
    TheLicense=`head -n 1 $LicenseFile`
    while true; do
        if [ $RECONF = yes ]; then
            echo "You have already accepted the terms of the $TheLicense."
            acceptance=yes
        else
            echo "You are licensed to use this software under the terms of"
            echo "the $TheLicense."
            echo
            echo "Type '?' to view the $TheLicense."
            echo "Type 'yes' to accept this license offer."
            echo "Type 'no' to decline this license offer."
            echo
            if echo '\c' | grep '\c' >/dev/null; then
                echo -n "Do you accept the terms of the $TheLicense? "
            else
                echo "Do you accept the terms of the $TheLicense? \c"
            fi
            read acceptance
        fi
        echo
        if [ "$acceptance" = "yes" ]; then
            break
        elif [ "$acceptance" = "no" ]; then
            echo "You are not licensed to use this software."
            echo
            exit 1
        else [ "$acceptance" = "?" ]
            more $QPE_ROOT/$LicenseFile
        fi
    done
fi

# Platform wrangling (detect x86_64 machines and use an appropriate spec)
echo "$QT2_CFG" | grep -- '-platform' >/dev/null 2>&1 && qt2_platform=yes || qt2_platform=no
echo "$DQT_CFG" | grep -- '-platform' >/dev/null 2>&1 && dqt_platform=yes || dqt_platform=no
echo "$QTE_CFG" | grep -- '-platform' >/dev/null 2>&1 && qte_platform=yes || qte_platform=no
echo "$QTE_CFG" | grep -- '-xplatform' >/dev/null 2>&1 && qte_xplatform=yes || qte_xplatform=no
echo "$QPE_CFG" | grep -- '-platform' >/dev/null 2>&1 && qpe_platform=yes || qpe_platform=no
echo "$QPE_CFG" | grep -- '-xplatform' >/dev/null 2>&1 && qpe_xplatform=yes || qpe_xplatform=no

if [ $qt2_platform = no ]; then
    qt2_platform="linux-g++"
else
    qt2_platform="$(echo "$QT2_CFG" | sed 's/.*-platform[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
fi
if [ $dqt_platform = no ]; then
    dqt_platform="linux-g++"
else
    dqt_platform="$(echo "$DQT_CFG" | sed 's/.*-platform[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
fi
if [ $qte_platform = no ]; then
    qte_platform="linux-g++"
else
    qte_platform="$(echo "$QTE_CFG" | sed 's/.*-platform[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
fi
if [ $qte_xplatform = no ]; then
    qte_xplatform="linux-generic-g++"
else
    qte_xplatform="$(echo "$QTE_CFG" | sed 's/.*-xplatform[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
fi
if [ $qpe_platform = no ]; then
    qpe_platform="linux-g++"
else
    qpe_platform="$(echo "$QPE_CFG" | sed 's/.*-platform[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
fi
if [ $qpe_xplatform = no ]; then
    qpe_xplatform="linux-generic-g++"
else
    qpe_xplatform="$(echo "$QPE_CFG" | sed 's/.*-xplatform[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
fi

machine="$(uname -m 2>/dev/null)" || machine=unknown
if [ "$machine" = "x86_64" ]; then
    echo "WARNING: Qtopia does not support x86_64. Checking your -platform and -xplatform arguments."
    if [ "$qt2_platform" = "linux-g++" ]; then
        qt2_platform="linux-32-g++"
    fi
    if [ "$dqt_platform" = "linux-g++" ]; then
        dqt_platform="linux-g++-32"
    fi
    if [ "$qte_platform" = "linux-g++" ]; then
        qte_platform="linux-32-g++"
    fi
    if [ "$qte_xplatform" = "linux-generic-g++" ]; then
        qte_xplatform="linux-generic32-g++"
    fi
    if [ "$qpe_platform" = "linux-g++" ]; then
        qpe_platform="linux-g++-32"
    fi
    if [ "$qpe_xplatform" = "linux-generic-g++" ]; then
        qpe_xplatform="linux-generic-g++-32"
    fi

    echo "Qt 2   is using -platform  $qt2_platform"
    echo "Qt 3   is using -platform  $dqt_platform"
    echo "Qt/E   is using -platform  $qte_platform"
    echo "Qt/E   is using -xplatform $qte_xplatform"
    echo "Qtopia is using -platform  $qpe_platform"
    echo "Qtopia is using -xplatform $qpe_xplatform"
    echo
fi

QT2_CFG="$(echo "$QT2_CFG" | sed 's/[[:space:]]*["'"'"']*-platform[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
QT2_CFG="$QT2_CFG -platform '$qt2_platform'"
DQT_CFG="$(echo "$DQT_CFG" | sed 's/[[:space:]]*["'"'"']*-platform[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
DQT_CFG="$DQT_CFG -platform '$dqt_platform'"
QTE_CFG="$(echo "$QTE_CFG" | sed 's/[[:space:]]*["'"'"']*-platform[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
QTE_CFG="$QTE_CFG -platform '$qte_platform'"
QTE_CFG="$(echo "$QTE_CFG" | sed 's/[[:space:]]*["'"'"']*-xplatform[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
QTE_CFG="$QTE_CFG -xplatform '$qte_xplatform'"
QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-platform[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
QPE_CFG="$QPE_CFG -platform '$qpe_platform'"
QPE_CFG="$(echo "$QPE_CFG" | sed 's/[[:space:]]*["'"'"']*-xplatform[=[:space:]"'"'"']*[^[:space:]"'"'"']*["'"'"']*[[:space:]]*/ /')"
QPE_CFG="$QPE_CFG -xplatform '$qpe_xplatform'"

# Save the commandlines for the build scripts
[ -f config.cache ] && rm config.cache
echo "QPEDIR=$QPEDIR" >>config.cache
echo "QTOPIA_DEPOT_PATH=$QTOPIA_DEPOT_PATH" >>config.cache
echo "TMAKEDIR=$TMAKEDIR" >>config.cache
echo "DQTDIR=$DQTDIR" >>config.cache
echo "QT2DIR=$QT2DIR" >>config.cache
echo "QTEDIR=$QTEDIR" >>config.cache
echo "Using the following configurations:"
echo "QPE_CFG=\"$QPE_CFG\"" | tee -a config.cache
echo "QTE_CFG=\"$QTE_CFG\"" | tee -a config.cache
echo "QT2_CFG=\"$QT2_CFG\"" | tee -a config.cache
echo "DQT_CFG=\"$DQT_CFG\"" | tee -a config.cache
echo "USE_CACHE=\"$USE_CACHE\"" >>config.cache

# Check that Qt/Embedded's -keypad-mode is present for Qtopia Phone Edition builds
echo "$QTE_CFG" | grep -- '-keypad-mode' >/dev/null 2>&1 && keypad=yes || keypad=no
echo "$QPE_CFG" | grep -- '-edition' >/dev/null 2>&1 && edition=yes || edition=no
if [ $edition = yes ]; then
    edition="$(echo "$QPE_CFG" | sed 's/.*[[:space:]]*["'"'"']*-edition[=[:space:]"'"'"']*\([^[:space:]"'"'"']*\).*/\1/')"
else
    if [ $phone = yes ]; then
        edition=phone
    else
        edition=pda
    fi
fi
if [ $edition = phone -a $keypad = no ]; then
    cat <<END

******************************************************************************
WARNING: It appears that you are building Qtopia Phone Edition but you did not
         pass -keypad-mode to Qt/Embedded. This combination is not supported.
         Touchscreen phones must still use -keypad-mode.
******************************************************************************
END
elif [ $edition != phone -a $keypad = yes ]; then
    cat <<END

******************************************************************************
WARNING: It appears that you are not building Qtopia Phone Edition but you
         passed -keypad-mode to Qt/Embedded. This combination is not
         supported and will result in binary-incompatibility.
******************************************************************************
END
fi

# environment variables
[ -f setQt2Env ] && rm setQt2Env
echo "export QTDIR=$QT2DIR" >>setQt2Env
echo "export TMAKEDIR=$TMAKEDIR" >>setQt2Env
echo "export TMAKEPATH=\$TMAKEDIR/lib/$qt2_platform" >>setQt2Env
echo "export PATH=\$QTDIR/bin:\$PATH" >>setQt2Env
echo "export LD_LIBRARY_PATH=\$QTDIR/lib:\$LD_LIBRARY_PATH" >>setQt2Env

[ -f setDqtEnv ] && rm setDqtEnv
echo "export QTDIR=$DQTDIR" >>setDqtEnv
echo "export PATH=\$QTDIR/bin:\$PATH" >>setDqtEnv
echo "export LD_LIBRARY_PATH=\$QTDIR/lib:\$LD_LIBRARY_PATH" >>setDqtEnv

[ -f setQteEnv ] && rm setQteEnv
echo "export QPEDIR=$QPEDIR" >>setQteEnv
echo "export QTDIR=$QTEDIR" >>setQteEnv
echo "export TMAKEDIR=$TMAKEDIR" >>setQteEnv
echo "export TMAKEPATH=\$TMAKEDIR/lib/qws/$qte_xplatform" >>setQteEnv
echo "export PATH=\$QTDIR/bin:\$PATH" >>setQteEnv
echo "export LD_LIBRARY_PATH=\$QTDIR/lib:\$LD_LIBRARY_PATH" >>setQteEnv

[ -f setQpeEnv ] && rm setQpeEnv
echo "export QPEDIR=$QPEDIR" >>setQpeEnv
echo "export QTOPIA_DEPOT_PATH=$QTOPIA_DEPOT_PATH" >>setQpeEnv
echo "export QTDIR=$QTEDIR" >>setQpeEnv
echo "export DQTDIR=$DQTDIR" >>setQpeEnv
echo "export TMAKEDIR=$TMAKEDIR" >>setQpeEnv
echo "export TMAKEPATH=\$TMAKEDIR/lib/qws/$qte_xplatform" >>setQpeEnv
echo "export PATH=\$QPEDIR/bin:\$QTDIR/bin:\$DQTDIR/bin:\$PATH" >>setQpeEnv
echo "export LD_LIBRARY_PATH=\$QPEDIR/lib:\$QTDIR/lib:\$DQTDIR/lib:\$LD_LIBRARY_PATH" >>setQpeEnv

# Prepare the Makefile
sed -e 's,\(QPEDIR=\).*,\1'$QPEDIR',' \
    -e 's,\(QTOPIA_DEPOT_PATH=\).*,\1'$QTOPIA_DEPOT_PATH',' \
    -e 's,\(QTEDIR=\).*,\1'$QTEDIR',' \
    -e 's,\(QT2DIR=\).*,\1'$QT2DIR',' \
    -e 's,\(DQTDIR=\).*,\1'$DQTDIR',' Makefile.in >Makefile

# reconfigure script
[ -f config.status ] && rm config.status
echo "#!/bin/sh" >>config.status
echo "./configure -reconf" >>config.status
chmod 755 config.status

echo
echo "Qtopia is now configured."
echo
echo "Type \"make\"              to build the qtopia bundle (and the tools, if required)."
echo "Type \"make install\"      to install Qtopia."
echo "Type \"make cleaninstall\" to install Qtopia after removing the image first (avoid stale files in the image)."
echo "Type \"make clean\"        to clean the qtopia bundle."
echo
echo "Type \"make tools\"        to build the tools bundle."
echo "Type \"make cleantools\"   to clean the tools bundle."
echo
echo "To manually build a particular component (eg. because it failed to build)"
echo "source the set...Env script. eg. . ./setQpeEnv; cd \$QPEDIR; make"
echo
exit 0

