#!/bin/sh
#
# Configures the libmeegotouch build
#
# Copyright (C) 2010 Nokia Corporation.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

#-------------------------------------------------------------------------------
# script initialization
#-------------------------------------------------------------------------------

# the name of this script
relconf=`basename "$0"`
# the directory of this script is the "source tree"
relpath=`dirname "$0"`
relpath=`(cd "$relpath"; /bin/pwd)`
# the current directory is the "build tree" or "object tree"
outpath=`/bin/pwd`

#-------------------------------------------------------------------------------
# operating system detection
#-------------------------------------------------------------------------------

# need that throughout the script
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

#-----------------------------------------------------------------------------
# M version detection
#-----------------------------------------------------------------------------
cd "$relpath"
M_MAJOR_VERSION=`grep -m1 ML_MAJOR_VERSION mkspecs/features/mlocale_defines.prf.in |cut -f2 -d'='|sed 's/ //g'`
M_MINOR_VERSION=`grep -m1 ML_MINOR_VERSION mkspecs/features/mlocale_defines.prf.in |cut -f2 -d'='|sed 's/ //g'`
M_PATCH_VERSION=`grep -m1 ML_PATCH_VERSION mkspecs/features/mlocale_defines.prf.in |cut -f2 -d'='|sed 's/ //g'`
M_VERSION=$M_MAJOR_VERSION.$M_MINOR_VERSION.$M_PATCH_VERSION

if [ -z "$M_MAJOR_VERSION" ]; then
   echo "Cannot process version from src/meegotouch_defines.prf: $M_VERSION"
   echo "Cannot proceed."
   exit 1
fi

# create version string for doxygen doc
M_DOC_VERSION_STRING=`sh doc/versionString.sh .`

#-------------------------------------------------------------------------------
# initalize variables
#-------------------------------------------------------------------------------

# initalize internal variables
CFG_DEBUG=yes
CFG_RELEASE=no
CFG_TESTABLE=no
CFG_COVERAGE=no
CFG_BUILD_TESTS=no
CFG_BUILD_BENCHMARKS=no

M_DEFAULT_BUILD_PARTS="libs"
CFG_BUILD_PARTS=""
CFG_ICU=auto

HAVE_LINUX=no
HAVE_ICU=no

OPT_SHADOW=maybe

IS_ARMEL=no
   
# initalize variables used for installation
M_INSTALL_PREFIX=/usr/local
M_INSTALL_SYSCONFDIR=/etc
M_INSTALL_LIBDIR= # Will be $M_INSTALL_PREFIX/lib unless overriden

#-------------------------------------------------------------------------------
# parse command line arguments
#-------------------------------------------------------------------------------

# parse the arguments, setting things to "yes" or "no"
while [ "$#" -gt 0 ]; do
    CURRENT_OPT="$1"
    UNKNOWN_ARG=no
    case "$1" in
    #Autoconf style options
    --enable-*)
        VAR=`echo $1 | sed "s,^--enable-\(.*\),\1,"`
        VAL=yes
        ;;
    --disable-*)
        VAR=`echo $1 | sed "s,^--disable-\(.*\),\1,"`
        VAL=no
        ;;
    --*=*)
        VAR=`echo $1 | sed "s,^--\(.*\)=.*,\1,"`
        VAL=`echo $1 | sed "s,^--.*=\(.*\),\1,"`
        ;;
    --no-*)
        VAR=`echo $1 | sed "s,^--no-\(.*\),\1,"`
        VAL=no
        ;;
    --*)
        VAR=`echo $1 | sed "s,^--\(.*\),\1,"`
        VAL=yes
        ;;
    #Qt style no options
    -no-*)
        VAR=`echo $1 | sed "s,^-no-\(.*\),\1,"`
        VAL=no
        ;;
    #Qt style yes options
        -h|-help|-v|-verbose|-debug|-release|-testable|-coverage|-timestamps|-dev|-minimal|-icu|-gconf|-meegographicssystem|-shortcuts)
        VAR=`echo $1 | sed "s,^-\(.*\),\1,"`
        VAL=yes
        ;;
    #Qt style options that pass an argument
    -prefix|-make|-nomake|-sysconfdir|-libdir)
        VAR=`echo $1 | sed "s,^-\(.*\),\1,"`
        shift
        VAL="$1"
        ;;
    #Qt style complex options in one command
    -enable-*|-disable-*)
        VAR=`echo $1 | sed "s,^-\([^-]*\)-.*,\1,"`
        VAL=`echo $1 | sed "s,^-[^-]*-\(.*\),\1,"`
        ;;
    #Qt Builtin/System style options
    -no-*)
        VAR=`echo $1 | sed "s,^-[^-]*-\(.*\),\1,"`
        VAL=`echo $1 | sed "s,^-\([^-]*\)-.*,\1,"`
        ;;
    #Options that cannot be generalized
    -*)
        VAR=`echo $1 | sed "s,^-\(.*\),\1,"`
        VAL="unknown"
        ;;
    *)
        UNKNOWN_ARG=yes
        ;;
    esac
    if [ "$UNKNOWN_ARG" = "yes" ]; then
        echo "$1: unknown argument"
        OPT_HELP=yes
        ERROR=yes
        shift
        continue
     fi
    shift

    UNKNOWN_OPT=no
    case "$VAR" in
    prefix)
        M_INSTALL_PREFIX="$VAL"
        ;;
    sysconfdir)
        M_INSTALL_SYSCONFDIR="$VAL"
        ;;
    libdir)
        M_INSTALL_LIBDIR="$VAL"
        ;;
    nomake)
	CFG_NOBUILD_PARTS="$CFG_NOBUILD_PARTS $VAL"
        ;;
    make)
	CFG_BUILD_PARTS="$CFG_BUILD_PARTS $VAL"
        ;;
    release)
        CFG_RELEASE="$VAL"
	;;
    debug)
        CFG_DEBUG="$VAL"
        ;;
    testable)
        CFG_TESTABLE="$VAL"
        ;;
    no-werror)
        CFG_NO_WERROR="yes" 
        ;;
    coverage)
        CFG_COVERAGE="$VAL"
        ;;
    tests)
        CFG_BUILD_TESTS="yes"
        ;;
    benchmarks)
        CFG_BUILD_BENCHMARKS="yes"
        ;;
    icu)
        if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
            CFG_ICU="$VAL"
        else
            UNKNOWN_OPT=yes
        fi
        ;;
    h|help)
        if [ "$VAL" = "yes" ]; then
            OPT_HELP="$VAL"
        else
            UNKNOWN_OPT=yes
        fi
        ;;
    v|verbose)
        if [ "$VAL" = "yes" ]; then
            if [ "$OPT_VERBOSE" = "$VAL" ]; then            # takes two verboses to turn on qmake debugs
                QMAKE_SWITCHES="$QMAKE_SWITCHES -d"
            else
                OPT_VERBOSE=yes
            fi
        elif [ "$VAL" = "no" ]; then
            if [ "$OPT_VERBOSE" = "$VAL" ] && echo "$QMAKE_SWITCHES" | grep ' -d' >/dev/null 2>&1; then
                QMAKE_SWITCHES=`echo $QMAKE_SWITCHES | sed "s, -d,,"`
            else
                OPT_VERBOSE=no
            fi
        else
            UNKNOWN_OPT=yes
        fi
        ;;
    silent)
        CFG_SILENT="$VAL"
        ;;
    esac
    if [ "$UNKNOWN_OPT" = "yes" ]; then
        echo "${CURRENT_OPT}: invalid command-line switch"
        OPT_HELP=yes
        ERROR=yes
    fi
done

if [ "$CFG_SILENT" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG silent"
fi

#-------------------------------------------------------------------------------
# tests
#-------------------------------------------------------------------------------

# Make default LIBDIR relative to prefix unless overriden
if [ -z "$M_INSTALL_LIBDIR" ]; then
    M_INSTALL_LIBDIR=$M_INSTALL_PREFIX/lib
fi

#setup the build parts
CFG_BUILD_PARTS="$CFG_BUILD_PARTS $M_DEFAULT_BUILD_PARTS"

for nobuild in $CFG_NOBUILD_PARTS; do
    CFG_BUILD_PARTS=`echo "$CFG_BUILD_PARTS" | sed "s, $nobuild,,g"`
done

if echo $CFG_BUILD_PARTS | grep -v libs >/dev/null 2>&1; then
    echo
    echo "WARNING: libs is a required part of the build."
    echo
    CFG_BUILD_PARTS="$CFG_BUILD_PARTS libs"
fi

if [ "$CFG_BUILD_TESTS" = "yes" ]; then
    CFG_BUILD_PARTS="$CFG_BUILD_PARTS tests"
else
  if echo $CFG_BUILD_PARTS | grep tests >/dev/null 2>&1; then
    CFG_BUILD_TESTS="yes"
  fi
fi

if [ "$CFG_BUILD_BENCHMARKS" = "yes" ]; then
    CFG_BUILD_PARTS="$CFG_BUILD_PARTS benchmarks"
else
  if echo $CFG_BUILD_PARTS | grep benchmarks >/dev/null 2>&1; then
    CFG_BUILD_BENCHMARKS="yes"
  fi
fi

# is this a shadow build?
if [ "$OPT_SHADOW" = "maybe" ]; then
    OPT_SHADOW=no
    if [ "$relpath" != "$outpath" ] && [ '!' -f "$outpath/configure" ]; then
        if [ -h "$outpath" ]; then
            [ "$relpath" -ef "$outpath" ] || OPT_SHADOW=yes
        else
            OPT_SHADOW=yes
        fi
    fi
fi

# is this a Linux build ?
if [ ! -z "`uname -s | grep Linux`" ]; then
    HAVE_LINUX=yes
fi

# Test for ICU
if [ "$CFG_ICU" != "no" ]; then
    which icu-config > /dev/null
    if [ $? -eq 0 ]; then
        icu-config --exists
        if [ $? -eq 0 ]; then
            HAVE_ICU=yes
        fi
    fi
    if [ "$CFG_ICU" = "yes" -a "$HAVE_ICU" = "no" ]; then
        echo "You specified  -icu  but icu was not found."
        echo "Please install the libicu-dev package."
        exit 1
    fi
fi

#-------------------------------------------------------------------------------
# help - interactive parts of the script _after_ this section please
#-------------------------------------------------------------------------------

# next, emit a usage message if something failed.
if [ "$OPT_HELP" = "yes" ]; then
    [ "x$ERROR" = "xyes" ] && echo
    cat <<EOF
Usage:  $relconf [-prefix <dir>] [-release] [-debug] [-make <part>]
        [-no-make <part>]

Installation options:

    -prefix <dir> ...... This will install everything relative to <dir>
                         (default $M_INSTALL_PREFIX)
    -sysconfdir <dir> .. This will put configuration files in <dir>
                         (default $M_INSTALL_SYSCONFDIR)
    -libdir <dir> ...... This will put libraries in <dir>
                         (default $M_INSTALL_LIBDIR)

Configure options:

 The defaults (*) are usually acceptable. A plus (+) denotes a default value
 that needs to be evaluated. If the evaluation succeeds, the feature is
 included. Here is a short explanation of each option:

    -release ................. Compile and link libmeegotouch in release mode
 *  -debug ................... Compile and link libmeegotouch with debugging turned on

    -testable ................ Enable the testability plugin interface in libmeegotouch
    -coverage ................ Enable code coverage calculation
    -no-werror ............... Disable warnings as errors gcc flag

Additional options:

    -make <part> ............. Add part to the list of parts to be built at make time
                               (*libs *demos tests benchmarks)
    -nomake <part> ........... Exclude part from the list of parts to be built

    -tests ................... Build the tests.
    -benchmarks .............. Build the benchmarks.

    -no-icu .................. Do not compile with ICU support.
 +  -icu ..................... Compile with ICU support.

EOF

exit 0

fi

#-------------------------------------------------------------------------------
# save configuration into mlocaleconfig.pri
#-------------------------------------------------------------------------------

if [ "$OPT_SHADOW" = "yes" ]; then
    if [ -f "$relpath/mkspecs/mlocaleconfig.pri" ]; then
        echo >&2 "You cannot make a shadow build from a source tree containing a previous build."
        echo >&2 "Cannot proceed."
        echo >&2 ""
        echo >&2 "Please clean your source directory by executing this command:"
        echo >&2 ""
        echo >&2 "make distclean"
        echo >&2 ""
        exit 1
    fi

    # create mkspecs/features dir in build tree
    mkdir -p "$outpath/mkspecs/features"
fi


MLOCALECONFIG="$outpath/mkspecs/mlocaleconfig.pri"
[ -f "$MLOCALECONFIG.tmp" ] && rm -f "$MLOCALECONFIG.tmp"

if [ "$CFG_DEBUG" = "yes" ]; then
    MLOCALECONFIG_CONFIG="$MLOCALECONFIG_CONFIG debug"
fi

if [ "$CFG_RELEASE" = "yes" ]; then
    MLOCALECONFIG_CONFIG="$MLOCALECONFIG_CONFIG release"
fi


if [ "$CFG_TESTABLE" = "yes" ]; then
    MEEGOTOUCHCONFIG_FEATURES="$MEEGOTOUCHCONFIG_FEATURES testable"
fi

if [ "$CFG_COVERAGE" = "yes" ]; then
    which bcov > /dev/null
    if [ $? -ne 0 ]; then
        echo "You specified  -coverage but bcov was not found."
        echo "Please install the bcov and lcov packages."
        exit 1
    fi
    which lcov > /dev/null
    if [ $? -ne 0 ]; then
        echo "You specified  -coverage but lcov was not found."
        echo "Please install the bcov and lcov packages."
        exit 1
    fi
    if [ "$SBOX_USE_CCACHE" != "no" ]; then
        echo "You specified  -coverage so the SBOX_USE_CCACHE has to be set to \"no\"."
        exit 1
    fi

    MLOCALECONFIG_CONFIG="$MLOCALECONFIG_CONFIG coverage"
fi


if [ "$HAVE_ICU" = "yes" ]; then
    MLOCALECONFIG_DEPS="$MLOCALECONFIG_DEPS HAVE_ICU"
fi

if [ "$IS_ARMEL" = "yes" ]; then
    MLOCALECONFIG_DEPS="$MLOCALECONFIG_DEPS IS_ARMEL"
fi                               


cat >>"$MLOCALECONFIG.tmp" <<EOF
# Autogenerated by configure script

#cleaning in CONFIG to get right configuration
CONFIG -=release
CONFIG -=debug

#build configuration
CONFIG += $MLOCALECONFIG_CONFIG

ML_BUILD_PARTS += $CFG_BUILD_PARTS

ML_BUILD_FEATURES += $MEEGOTOUCHCONFIG_FEATURES

#versioning
ML_VERSION = $M_VERSION
ML_MAJOR_VERSION = $M_MAJOR_VERSION
ML_MINOR_VERSION = $M_MINOR_VERSION
ML_PATCH_VERSION = $M_PATCH_VERSION

ML_DOC_VERSION_STRING = $M_DOC_VERSION_STRING

#paths
ML_INSTALL_PREFIX = $M_INSTALL_PREFIX
ML_INSTALL_SYSCONFDIR = $M_INSTALL_SYSCONFDIR
ML_INSTALL_LIBDIR = $M_INSTALL_LIBDIR

#dependencies
DEFINES += $MLOCALECONFIG_DEPS

EOF

# replace meegotouchconfig.pri if it differs from the newly created temp file
if cmp -s "$MLOCALECONFIG.tmp" "$MLOCALECONFIG"; then
    rm -f "$MLOCALECONFIG.tmp"
else
    mv -f "$MLOCALECONFIG.tmp" "$MLOCALECONFIG"
fi

# now try to create a .qmake.cache file to be able to find the build root
M_QMAKE_CACHE="$outpath/.qmake.cache"
[ -f "$M_QMAKE_CACHE.tmp" ] && rm -f "$M_QMAKE_CACHE.tmp"

cat >>"$M_QMAKE_CACHE.tmp" <<EOF
# Autogenerated by configure script
M_BUILD_TREE = \$\$quote($outpath)
M_SOURCE_TREE = \$\$quote($relpath)

EOF

# replace .qmake.cache if it differs from the newly created temp file
if cmp -s "$M_QMAKE_CACHE.tmp" "$M_QMAKE_CACHE"; then
    rm -f "$M_QMAKE_CACHE.tmp"
else
    mv -f "$M_QMAKE_CACHE.tmp" "$M_QMAKE_CACHE"
fi

MLOCALE_DEFINES_PRF="$outpath/mkspecs/features/mlocale_defines.prf"

# and now we generate a mlocale_defines.prf that will contain the right
# install prefix
cat "$relpath/mkspecs/features/mlocale_defines.prf.in" \
| sed -e "s,@@ML_UNIX_INSTALL_PREFIX@@,$M_INSTALL_PREFIX,g" \
| sed -e "s,@@ML_UNIX_INSTALL_SYSCONFDIR@@,$M_INSTALL_SYSCONFDIR,g" \
| sed -e "s,@@ML_UNIX_INSTALL_LIBDIR@@,$M_INSTALL_LIBDIR,g" \
| sed -e "s,@@MLOCALECONFIG_DEPS@@,$MLOCALECONFIG_DEPS,g" \
> "$MLOCALE_DEFINES_PRF.tmp"

# replace meegotouch_defines.prf if it differs from the newly created temp file
if cmp -s "$MLOCALE_DEFINES_PRF.tmp" "$MLOCALE_DEFINES_PRF"; then
    rm -f "$MLOCALE_DEFINES_PRF.tmp"
else
    mv -f "$MLOCALE_DEFINES_PRF.tmp" "$MLOCALE_DEFINES_PRF"
fi

# substitute prefix in .pc files
MEEGOTOUCH_PC_DIR="$outpath/src/data"
MEEGOTOUCH_PC_FILES="mlocale5"

# make sure the directory exists
if [ ! -d $MEEGOTOUCH_PC_DIR ]; then
    mkdir -p $MEEGOTOUCH_PC_DIR
fi

for i in $MEEGOTOUCH_PC_FILES
do
   pc_file="$MEEGOTOUCH_PC_DIR/$i.pc"
   pc_in="$relpath/src/data/$i.pc.in"

   cat "$pc_in" \
   | sed -e "s,@@M_UNIX_INSTALL_PREFIX@@,$M_INSTALL_PREFIX,g" \
   | sed "s/@@M_VERSION@@/$M_VERSION/g" \
   > "$pc_file.tmp"

  # replace meegotouch_defines.prf if it differs from the newly created temp file
  if cmp -s "$pc_file.tmp" "$pc_file"; then
      rm -f "$pc_file.tmp"
  else
      mv -f "$pc_file.tmp" "$pc_file"
  fi

done

#-------------------------------------------------------------------------------
# build makefiles based on the configuration
#-------------------------------------------------------------------------------

if [ -z "$QTDIR" ]; then
    which qmake > /dev/null
    if [ $? -eq 0 ]; then
        QMAKE_BIN=`which qmake`
    else
        which qmake-qt5 > /dev/null
        if [ $? -eq 0 ]; then
            QMAKE_BIN=`which qmake-qt5`
        else
            echo "qmake was not found in your path\n"
        fi
    fi
elif [ -f "$QTDIR/bin/qmake" ]; then
    QMAKE_BIN="$QTDIR/bin/qmake"
else
    echo "QTDIR variable was set but could not find $QTDIR/bin/qmake\n"
fi

if [ -z "$QMAKE_BIN" ]; then
    echo "If your Qt is in a nonstandard location, try:"
    echo "QTDIR=<path to your Qt directory> ./$relconf"
    exit 1
fi

# now check for qt version
QT_MAJOR_VERSION_NEEDED=5
QT_MINOR_VERSION_NEEDED=0

QT_VERSION=$($QMAKE_BIN -query QT_VERSION)

QT_MAJOR_VERSION=0
QT_MINOR_VERSION=0
QT_PATCH_VERSION=0
if [ -n "$QT_VERSION" ]; then
   MAJOR=`echo $QT_VERSION | sed 's,^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*,\1,'`
   if [ -n "$MAJOR" ]; then
     MINOR=`echo $QT_VERSION | sed 's,^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*,\2,'`
      PATCH=`echo $QT_VERSION | sed 's,^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*,\3,'`
      QT_MAJOR_VERSION="$MAJOR"
      [ -z "$MINOR" ] || QT_MINOR_VERSION="$MINOR"
      [ -z "$PATCH" ] || QT_PATCH_VERSION="$PATCH"
   fi
fi

if [ -z "$QT_MAJOR_VERSION" ]; then
   echo "Cannot process version from qglobal.h: $QT_VERSION"
   echo "Cannot proceed."
   exit 1
fi

if [ "$QT_MAJOR_VERSION" -lt "$QT_MAJOR_VERSION_NEEDED" ] \
    && [ "$QT_MINOR_VERSION" -lt "$QT_MINOR_VERSION_NEEDED" ]; then
    echo "Your Qt version is too old: $QT_VERSION"
    echo "You need at least version $QT_MAJOR_VERSION_NEEDED.$QT_MINOR_VERSION_NEEDED.0"
    echo "Cannot proceed."
    exit 1
fi

# set special options for mac build
if [ "$UNAME_SYSTEM" = "Darwin" ]; then
    QMAKE_SWITCHES="$QMAKE_SWITCHES -spec macx-g++"
fi


#Run qmake
cd "$outpath"
$QMAKE_BIN "$relpath/projects.pro" $QMAKE_CONFIG $QMAKE_SWITCHES

#-------------------------------------------------------------------------------
# give feedback on configuration
#-------------------------------------------------------------------------------

echo ""
echo ""
echo "Optional build dependencies found:"

echo "ICU ............................. $HAVE_ICU"

echo "MeeGo Touch UI framework build configuration:"
echo "Version ............. $M_VERSION"
echo "Build ............... $CFG_BUILD_PARTS"
echo "Extra features ...... $MEEGOTOUCHCONFIG_FEATURES"
echo "Release ............. $CFG_RELEASE"
echo "Debug ............... $CFG_DEBUG"

echo
echo "Benchmarks .......... $CFG_BUILD_BENCHMARKS"
echo "Tests ............... $CFG_BUILD_TESTS"

echo ""
echo libmlocale is now configured for building. Just run \'make\'.
if [ "$relpath" = "$M_INSTALL_PREFIX" ]; then
    echo Once everything is built, libmlocale is installed.
    echo You should not run \'make install\'.
else
    echo Once everything is built, you can run \'make install\'.
    echo libmlocale will be installed into $M_INSTALL_PREFIX
fi

echo ""
echo ""
