#!/bin/bash
# Script generated by dpconfigure on Oct  6 2020
#
#   Configure script for Linux, Mac OS X and Unix
#
# copyright (C) 2002-2019 Marc Wathelet. All rights reserved.
#
#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#

# Return absolute path
absolutePath()
{
  PATH=$1
  if [ "${PATH:0:1}" = "/" ]; then
    echo $PATH
  else
    ABS_PATH=$PWD
    while [ "${PATH:0:3}" = "../" ]; do
      ABS_PATH=${ABS_PATH%/[^/]*}
      PATH=${PATH:3}
    done
    echo $ABS_PATH/$PATH
  fi
}

# $1 is the path in the mingw form: /c/usr/... or "/c/usr/..."
function winPath()
{
  if [ "$PLATFORM" = "MINGW" ] &&
     [ "${1:0:1}" = "/" ] && [ "${1:2:1}" = "/" ]; then
    echo $1 | sed -r "s/^\"(.*)\"$/\1/" | sed -r "s/^\/([a-zA-Z])/\1:/" | sed "s/\//\\\\/g"
  else
    echo $1
  fi
}

#function cWinPath()
#{
#  winPath $1 | sed "s/\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/g"
#}

# Arguments: destination directory, file
# New file must be generated in TEMP_DIR
function replaceFile()
{
  if [ -e $1/$2 ] && diff $1/$2 $TEMP_DIR/$2 > /dev/null; then
    rm $TEMP_DIR/$2
  else
    mv $TEMP_DIR/$2 $1
  fi
}

cleanexit()
{
  rm -rf $TEMP_DIR
  if [ $# = 1 ]; then
    if [ $1 -ne 0 ]; then
      rm -f $BUILD_DIR/gppoisson.pro
      rm -f $BUILD_DIR/Makefile*
      echo "Error while configuring gppoisson. Check above messages."
      echo "If you want to contact support, send a copy of the whole output."
    fi
    exit $1
  else
    echo
    echo "Aborting..." 1>&2
    exit 2
  fi
}

function beginModificationSafe() {
  rm -rf $TEMP_DIR/*
  if [ -e $1 ]; then
    mkdir $TEMP_DIR/old
    mv $* $TEMP_DIR/old
  fi
}

function endModificationSafe() {
  BASE_DIR=$1
  shift
  if [ -d $TEMP_DIR/old ]; then
    mkdir $TEMP_DIR/new
    mv $* $TEMP_DIR/new
    if diff -r $TEMP_DIR/old $TEMP_DIR/new > /dev/null; then
      mv $TEMP_DIR/old/* $BASE_DIR
    else
      mv $TEMP_DIR/new/* $BASE_DIR
    fi
    rm -r $TEMP_DIR/old $TEMP_DIR/new
  fi
}

trap cleanexit INT QUIT
TEMP_DIR=$(mktemp -d /tmp/gppoisson_configure_XXXXXXXXX)

###############################################################
# Guess platform
###############################################################
#Platform options
case "$(uname)" in
  Linux) PLATFORM=LINUX ;;
  SunOS) PLATFORM=SUNOS ;;
  Darwin) PLATFORM=MAC ;;
  MINGW*) PLATFORM=MINGW ;;
  *)
    echo "Unknown platform $(uname)" > /dev/stderr
    cleanexit 2
  ;;
esac

###############################################################
# Default options
###############################################################
# make sure that we are in the correct directory
cd $(dirname $0)

PREFIX="/usr/local/Geopsy.org"
BINDIR="$PREFIX/bin"
LIBDIR="$PREFIX/lib"
INCDIR="$PREFIX/include"
SHAREDIR="$PREFIX/share"
LIBTYPE=shared
EXTINCDIR=""
EXTLIBDIR=""
RPATHDIR="$LIBDIR"
DEFINES=""
LICENSE_ACCEPTED="no"
COMMANDLINE=""
SILENT_MARK=""
SKIP_QT_CHECK="no"
SKIP_QT_TRANSLATIONS="no"
SKIP_QMAKE="no"
PROFILING="no"
PCH="yes"
QT_BIN_PATH=""
NO_SCRIPT="no"
NO_SVG="no"
CFLAGS=""
DEBUG="no"
# BASE_SOURCE_DIR is the directory containing eventually several projects
# Note that explicit -sourcedir option is mandatory if BASE_SOURCE_DIR and SOURCE_DIR are different.
BASE_SOURCE_DIR=$(pwd)
# SOURCE_DIR is the directory of this project.
SOURCE_DIR=$BASE_SOURCE_DIR
# Same remarks for BASE_BUILD_DIR and BUILD_DIR
BASE_BUILD_DIR=$BASE_SOURCE_DIR
BUILD_DIR=$SOURCE_DIR
DEST_DIR=$SOURCE_DIR
###############################################################
# Collect user options
###############################################################
while [ "$1" ]; do
  case "$1" in
    "-prefix" | "--prefix")
      shift
      PREFIX="$(absolutePath $1)"
      BINDIR="$PREFIX/bin"
      LIBDIR="$PREFIX/lib"
      RPATHDIR="\"$LIBDIR\""
      INCDIR="$PREFIX/include"
      SHAREDIR="$PREFIX/share/Geopsy.org"
      COMMANDLINE="$COMMANDLINE -prefix \"$1\""
    ;;
    "-bindir" | "--bindir")
      shift
      BINDIR="$(absolutePath $1)"
      COMMANDLINE="$COMMANDLINE -bindir \"$1\""
    ;;
    "-libdir" | "--libdir")
      shift
      LIBDIR="$(absolutePath $1)"
      RPATHDIR="$RPATHDIR \"$LIBDIR\""
      COMMANDLINE="$COMMANDLINE -libdir \"$1\""
    ;;
    "-incdir" | "--incdir")
      shift
      INCDIR="$(absolutePath $1)"
      COMMANDLINE="$COMMANDLINE -incdir \"$1\""
    ;;
    "-sharedir" | "--sharedir")
      shift
      SHAREDIR="$(absolutePath $1)"
      COMMANDLINE="$COMMANDLINE -sharedir \"$1\""
    ;;
    "-sourcedir" | "--sourcedir")
      shift
      BASE_SOURCE_DIR="$(absolutePath $1)"
      COMMANDLINE="$COMMANDLINE -sourcedir \"$1\""
    ;;
    "-builddir" | "--builddir")
      shift
      BASE_BUILD_DIR="$(absolutePath $1)"
      if [ "$BUILD_DIR" = "$SOURCE_DIR" ]; then
        BUILD_DIR=$BASE_BUILD_DIR
      fi
      COMMANDLINE="$COMMANDLINE -builddir \"$1\""
    ;;
    "-buildsubdir" | "--buildsubdir")
      shift
      REGEXP_BUILD_SUBDIR=".*/$1$"
      if ! [[ "$BUILD_DIR" =~ $REGEXP_BUILD_SUBDIR ]]; then
        BUILD_DIR="$BASE_BUILD_DIR/$1"
      fi
      COMMANDLINE="$COMMANDLINE -buildsubdir \"$1\""
    ;;
    "-dest-prefix" | "--dest-prefix")
      shift
      DEST_DIR="$(absolutePath $1)"
      COMMANDLINE="$COMMANDLINE -dest-prefix \"$1\""
    ;;
    "-shared" | "--shared")
      LIBTYPE="shared"
      COMMANDLINE="$COMMANDLINE -shared"
    ;;
    "-static" | "--static")
      LIBTYPE="static"
      COMMANDLINE="$COMMANDLINE -static"
    ;;
    "-accept-license" | "--accept-license")
      LICENSE_ACCEPTED="yes"
      COMMANDLINE="$COMMANDLINE -accept-license"
    ;;
    -I*)
      if [ "$1" != "-I" ]; then
        ARG=${1:2}
      else
        shift
        ARG="$1"
      fi
      EXTINCDIR="$EXTINCDIR \"$(winPath $ARG)\""
      COMMANDLINE="$COMMANDLINE -I \"$ARG\""
    ;;
    -L*)
      if [ "$1" != "-L" ]; then
        ARG=${1:2}
      else
        shift
        ARG="$1"
      fi
      EXTLIBDIR="$EXTLIBDIR \"$(winPath $ARG)\""
      COMMANDLINE="$COMMANDLINE -L \"$ARG\""
    ;;
    "-rpath" | "--rpath")
      if [ "${2:0:1}" = "-" ]; then
        RPATHDIR=""
        COMMANDLINE="$COMMANDLINE -rpath"
      else
        shift
        RPATHDIR="$RPATHDIR \"$(absolutePath $1)\""
        COMMANDLINE="$COMMANDLINE -rpath \"$1\""
      fi
    ;;
    "-D")
      shift
      DEFINES="$DEFINES $1"
      COMMANDLINE="$COMMANDLINE -D \"$1\""
    ;;
    "-skip-qt-check" | "--skip-qt-check")
      SKIP_QT_CHECK="yes"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
    "-skip-qt-translations" | "--skip-qt-translations")
      SKIP_QT_TRANSLATIONS="yes"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
     "-skip-qmake" | "--skip-qmake")
      SKIP_QMAKE="yes"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
   "-profile" | "--profile")
      PROFILING="yes"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
    "-silent" | "--silent")
      SILENT_MARK="@"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
    "-no-pch" | "--no-pch")
      PCH="no"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
    "-debug" | "--debug")
      DEBUG="yes"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
    "-win32" | "--win32" | "-win64" | "--win64")
      PLATFORM="MINGW"
      COMMANDLINE="$COMMANDLINE $1"
    ;;
    "-qt-bin-path" | "--qt-bin-path")
      shift
      QT_BIN_PATH="$1/"
      COMMANDLINE="$COMMANDLINE -qt-bin-path \"$1\""
    ;;
    "-no-svg" | "--no-svg")
      NO_SVG=yes
      DEFINES="$DEFINES NO_SVG"
      COMMANDLINE="$COMMANDLINE -no-svg"
    ;;
    "-cflags" | "--cflags")
      shift
      CFLAGS="$CFLAGS $1"
      COMMANDLINE="$COMMANDLINE -cflags \"$1\""
    ;;
    "-sse3" | "--sse3")
      CFLAGS="$CFLAGS -msse3"
      COMMANDLINE="$COMMANDLINE -msse3"
    ;;
    "-march" | "--march")
      shift
      CFLAGS="$CFLAGS -march=$1"
      COMMANDLINE="$COMMANDLINE -march \"$1\""
    ;;
    "-h" | "-help" | "--help")
      echo
      echo "Usage: configure [options]"
      echo
      echo "Options:"
      echo
      echo " -qt-bin-path <DIR>     Use Qt in DIR instead of qt in PATH"         
      echo " -accept-license        Skip interactive license acceptance"
      echo " -silent                Minimal output during \"make install\""
      echo " -no-pch                Do not use precompiled headers (slower compilation)"
      echo " -debug                 Include debugging symbols (less optimized)"
      echo
      echo "Paths:"
      echo
      echo " -prefix <DIR>          Base directory for installation. It must be before any -*dir"
      echo "                          (default /usr/local/Geopsy.org)"
      echo " -bindir <DIR>          Executables will be installed to DIR"
      echo "                          (default $BINDIR)"
      echo " -libdir <DIR>          Libraries will be installed to DIR"
      echo "                          (default $LIBDIR)"
      echo " -incdir <DIR>          Headers will be installed to DIR"
      echo "                          (default $INCDIR)"
      echo " -sharedir <DIR>        Shared files and translations will be installed"
      echo "                        to DIR (default $SHAREDIR)"
      echo " -sourcedir <DIR>       Source are in DIR (default $SOURCE_DIR)"
      echo " -builddir <DIR>        Building in DIR (default $BASE_BUILD_DIR)"
      echo " -buildsubdir <DIR>     Add subdir DIR to build directory"
      echo
      echo "Configuration:"
      echo
      echo " -shared                Create and use shared libraries"
      echo " -static                Create and use static libraries"
      echo
      echo " -D <MACRO>             Defines MACRO"
      echo
      echo " -dest-prefix <DIR>     Destination prefix for all binaries"
      echo "                          (default $DEST_DIR)"
      echo " -rpath <DIR>           Add DIR to library rutime search path"
      echo "                          (default $RPATHDIR, reset to null without DIR)"
      echo " -skip-qt-check         Do not check current Qt version"
      echo " -skip-qt-TRANSLATIONS  Do not use Qt translations"
      echo " -profile               Turn profiling on, add gcc option '-pg'"
      echo "                          (for development under Linux only)"
      echo " -win32                 Turn on cross compilation with i686-mingw32"
      echo "                          (under Linux only)"
      echo " -win64                 Turn on cross compilation with x86_64-mingw32"
      echo "                          (under Linux only)"
      echo " -no-svg                Do not compile support for Qt svg"
      echo " -cflags <FLAG>         Add FLAG to the list of compilation flags"
      echo " -sse3                  Compile with SSE3 support"
      echo " -march <ARCH>          Specify the name of the target architecture"
      echo "                        Use ARCH=native for auto detection of the CPU's architecture"
      echo "                        Useful link: https://wiki.gentoo.org/wiki/Safe_CFLAGS"
      echo "                        Your architecture is:"
      grep -m1 -A3 "vendor_id" /proc/cpuinfo | awk '{printf("                       ");print}'
      echo
      echo "Third party libraries:"
      echo
      echo " -I dir                 Add dir to the list of directories to be"
      echo "                        searched for headers"
      echo " -L dir                 Add dir to the list of directories to be"
      echo "                        searched for libraries"
      echo
      cleanexit 0
    ;;
    *)
      echo "Bad option $1, see './configure -h' for help" > /dev/stderr
      cleanexit 2
    ;;
  esac
  shift
done
# Fix DEST_DIR in case of separate BUILD_DIR
if [ "$BUILD_DIR" != "$SOURCE_DIR" ] && [ "$DEST_DIR" = "$SOURCE_DIR" ]; then
  DEST_DIR=$BASE_BUILD_DIR
fi
EXTLIBDIR="\"$(winPath $DEST_DIR/lib)\" $EXTLIBDIR"

###############################################################
# Show license if .pro file does not exist
###############################################################
echo "* License..."
if [ ! -e $BUILD_DIR/configure.cache ] && [ "$LICENSE_ACCEPTED" = "no" ]; then
  echo
  echo "gppoisson package is licensed to you under the terms"
  echo "of the GNU General Public License (GPL) version 2."
  LICENSE_ACCEPT=""
  while [ "$LICENSE_ACCEPT" != "no" ] && [ "$LICENSE_ACCEPT" != "yes" ]; do
    echo
    echo "Type 'view' to view the GNU General Public License."
    echo "Type 'yes' to accept this license offer."
    echo "Type 'no' to decline this license offer."
    echo
    echo -n "Do you accept this license? "
    read LICENSE_ACCEPT
    if [ "$LICENSE_ACCEPT" = "view" ]; then
      echo "#######################################################"
      echo "Type SPACE BAR or RETURN to move down"
      echo "#######################################################"
      sleep 2
      more LICENSE.GPL
      echo "#######################################################"
    fi
  done
  if [ "$LICENSE_ACCEPT" = "no" ]; then
    echo "You are not allowed to use this package." > /dev/stderr
    cleanexit 2
  fi
fi

###############################################################
# Store configure options
###############################################################
# If configure called from ./configure.cache, do not overwrite
if [ "$SAVE_CONFIGURE_OPTIONS_IN_CACHE" != "no" ]; then
  (
  cat << END
export SAVE_CONFIGURE_OPTIONS_IN_CACHE="no"
cd $SOURCE_DIR
echo "$SHELL $0 ${COMMANDLINE//\"/\\\"}"
$SHELL $0 $COMMANDLINE
END
  ) > $BUILD_DIR/configure.cache
  chmod a+x $BUILD_DIR/configure.cache
fi

###############################################################
# Check that Qt is correctly installed
###############################################################
if [ "$SKIP_QT_CHECK" = "yes" ]; then
  QT_VERSION=$(${QT_BIN_PATH}qmake -v | awk '{if (NR==2) print $4}')
  QT_VERSION_NUM=$(echo $QT_VERSION | awk 'BEGIN{FS="."}{print $1*1000000+$2*1000+$3}')
else
  echo "* Check Qt..."

  if ! ${QT_BIN_PATH}qmake -v > /dev/null 2>&1; then
    (
    echo "*** ERROR *** : qmake command is not available."
    echo "                Either Qt is not correctly installed"
    echo "                or PATH does not point to Qt binaries."
    echo "                Check '-qt-bin-path' option."
    ) > /dev/stderr
    cleanexit 2
  fi
 
  QT_VERSION=$(${QT_BIN_PATH}qmake -v | awk '{if (NR==2) print $4}')
  QT_VERSION_NUM=$(echo $QT_VERSION | awk 'BEGIN{FS="."}{print $1*1000000+$2*1000+$3}')

  if [ $QT_VERSION_NUM -lt 5014000 ]; then
    (
    echo "*** ERROR *** : Your Qt version is $QT_VERSION."
    echo "                At least version 5.14.2 is recommended."
    echo "                If you want to skip this version check,"
    echo "                add option -skip-qt-check."
    ) > /dev/stderr
    cleanexit 2
  fi

  if [ "$SKIP_QT_TRANSLATIONS" = "no" ] && ! ${QT_BIN_PATH}lrelease -version > /dev/null 2>&1; then
    (
    echo "*** ERROR *** : lrelease command is not available."
    echo "                Either Qt is not correctly installed"
    echo "                or PATH does not point to Qt binaries."
    echo "                Check '-qt-bin-path' option."
    echo "                You can also use '-skip-qt-translations'."
    ) > /dev/stderr
    cleanexit 2
  fi

###############################################################
# Check that g++ is available
###############################################################

  echo "* Check compiler..."

  if ! g++ --version > /dev/null 2>&1; then
    (
    echo "*** ERROR *** : g++ is not available."
    ) > /dev/stderr
    cleanexit 2
  fi

###############################################################
# Check if Qt is compiled statically
###############################################################

  QT_SHARED="yes"
  rm -rf $TEMP_DIR/Qt_static
  mkdir $TEMP_DIR/Qt_static
  ORIG_DIR=$(pwd)
  cd $TEMP_DIR/Qt_static
  if [ "$PLATFORM" = "MINGW" ]; then
    (
    cat << END
#include <qglobal.h>
int main(int argc, int argv)
{
#ifdef QT_DLL
  return 2;
#else
  return 0;
#endif
}
END
    ) > main.cpp
  else
    (
    cat << END
#include <qglobal.h>
int main(int argc, int argv)
{
#ifdef QT_SHARED
  return 2;
#else
  return 0;
#endif
}
END
    ) > main.cpp
  fi
  if ${QT_BIN_PATH}qmake -project -o test.pro > /dev/null 2>&1 &&     ${QT_BIN_PATH}qmake "CONFIG-=app_bundle" "CONFIG-=debug" "CONFIG+=release" > /dev/null 2>&1 &&     make > /dev/null 2>&1 &&     test; then
    QT_SHARED="no"
    echo "Qt build statically"
  fi
  cd $ORIG_DIR
  rm -rf $TEMP_DIR/Qt_static
fi

###############################################################
# Dependency lib names
###############################################################
echo "* Dependency lib names [platform dependent]..."
case "$PLATFORM" in
  "LINUX")
    LIBS="-lQGpCoreTools "
  ;;
  "SUNOS")
    LIBS="-lQGpCoreTools "
  ;;
  "MINGW")
    LIBS="-lQGpCoreTools2 "
  ;;
  "MAC")
    LIBS="-framework QGpCoreTools "
  ;;
esac

###############################################################
# Dependency lib includes
###############################################################
echo "* Dependency lib include paths..."
cd $BASE_SOURCE_DIR
for LLIB in -lQGpCoreTools; do
  LIB_BASENAME=$(echo $LLIB | sed "s/^-l//g")
  LIB_PATH_FOUND="no"
  for LIB_PATH in $(find . -name "${LIB_BASENAME}" | sed "s/^\.\///"); do
    if [ -e ${LIB_PATH}/src/${LIB_BASENAME}.h ] && [ -e ${LIB_PATH}/configure ]; then
      if [ "$LIB_PATH_FOUND" = "yes" ]; then
        echo "    Found more than one include path for lib '${LIB_BASENAME}'"
        cleanexit 2
      fi
      cd $LIB_PATH
      echo "    $LIB_PATH"
      EXTINCDIR="$EXTINCDIR $(winPath $PWD/src)"
      if [ "$BUILD_DIR" != "$SOURCE_DIR" ]; then
        EXTINCDIR="$EXTINCDIR $(winPath $BASE_BUILD_DIR/$LIB_PATH/src)"
      fi
      if ls src/*.ui > /dev/null 2>&1; then
        if [ "$BUILD_DIR" = "$SOURCE_DIR" ]; then
          EXTINCDIR="$EXTINCDIR $(winPath $PWD/ui)"
        else
          EXTINCDIR="$EXTINCDIR $(winPath $BASE_BUILD_DIR/$LIB_PATH/ui)"
        fi
      fi
      LIB_PATH_FOUND="yes"
      cd $BASE_SOURCE_DIR
    fi
  done
done
cd $SOURCE_DIR


###############################################################
# Generate .pro file
###############################################################
echo "* Qt project..."
test -d ${BUILD_DIR} || mkdir -p ${BUILD_DIR}
(
echo "CONFIG -= debug_and_release"
if [ "$DEBUG" = "yes" ]; then
  echo "CONFIG += debug separate_debug_info"
else
  echo "CONFIG += release"
fi
echo "CONFIG +=no_lflags_merge"
echo "TARGET = gppoisson"
echo "TEMPLATE = app"
echo "VERSION = 2.0.0"
  echo "QT-=gui"
  echo "INCLUDEPATH+=$(winPath $SOURCE_DIR/src) src"
  echo "DEPENDPATH+=$(winPath $SOURCE_DIR/src) src"
if [ "$QT_SHARED" = "no" ]; then
  echo "QTPLUGIN += qjpeg qgif qmng"
fi
if [ "$PLATFORM" = "MAC" ] && [ -e $SOURCE_DIR/images/gppoisson.icns ]; then
  echo "ICON= $SOURCE_DIR/images/gppoisson.icns"
elif [ "$PLATFORM" = "MINGW" ] && [ -e $SOURCE_DIR/images/gppoisson.rc ]; then
  echo "RC_FILE= $(winPath $SOURCE_DIR/images/gppoisson.rc)"
fi 
if [ "$PLATFORM" = "MAC" ]; then
  echo "QMAKE_SONAME_PREFIX = @rpath"
  echo "QMAKE_MACOSX_DEPLOYMENT_TARGET=10.7"
  echo "CONFIG-= app_bundle"
fi
if [ -n "$CFLAGS" ]; then
  echo "QMAKE_CXXFLAGS_DEBUG += $CFLAGS"
  echo "QMAKE_CXXFLAGS_RELEASE += $CFLAGS"
fi
if [ "$LIBTYPE" = "static" ]; then
  echo "CONFIG += staticlib"
  echo "QMAKE_CXXFLAGS_DEBUG += -fPIC"
  echo "QMAKE_CXXFLAGS_RELEASE += -fPIC"
  echo "QMAKE_CFLAGS_DEBUG += -fPIC"
  echo "QMAKE_CFLAGS_RELEASE += -fPIC"
fi
if [ "$PROFILING" = "yes" ]; then
  echo "CONFIG(debug) {"
  echo "  QMAKE_CXXFLAGS_DEBUG += -pg"
  echo "  QMAKE_CFLAGS_DEBUG += -pg"
  echo "  QMAKE_LFLAGS_DEBUG += -pg"
  echo "} else {"
  echo "  QMAKE_CXXFLAGS_RELEASE += -pg"
  echo "  QMAKE_CFLAGS_RELEASE += -pg"
  echo "  QMAKE_LFLAGS_RELEASE += -pg"
  echo "}"
fi
echo "QMAKE_LFLAGS_DEBUG += -g"
if [ "$EXTINCDIR" ]; then
  echo "INCLUDEPATH += $EXTINCDIR"
  echo "DEPENDPATH += $EXTINCDIR"
fi
if [ "$EXTLIBDIR" ]; then
  for p in $(echo $EXTLIBDIR); do
    echo "LIBS += -L$p"
    if [ "$PLATFORM" = "MAC" ]; then
      echo "LIBS += -F$p"
    fi
  done
  for p in $(echo $RPATHDIR); do
    if [ "$PLATFORM" = "LINUX" ] || [ "$PLATFORM" = "MAC" ]; then
      echo "LIBS += -Wl,-rpath,$p"
    elif [ "$PLATFORM" = "SUNOS" ]; then
      echo "LIBS += -Wl,-R,$p"
    fi
  done
fi
if [ "$LIBS" ]; then
   echo "LIBS += $LIBS"
fi
if [ "$PLATFORM" = "MAC" ]; then
  echo "LIBS += -Wl,-undefined,dynamic_lookup"
  echo "QMAKE_SONAME_PREFIX = @rpath"
fi
if [ "$PLATFORM" = "MINGW" ]; then
  echo "QMAKE_LFLAGS_RELEASE = -Wl,-S"
fi
if [ "$DEFINES" ] || [ "" ]; then
  echo "DEFINES += $DEFINES "
fi
echo "CONFIG += console"
echo "!macx:!win32:QMAKE_LFLAGS=-rdynamic"
echo "DESTDIR = $(winPath ${DEST_DIR}/bin)"
cat << END
SOURCES+= \\
           $(winPath $SOURCE_DIR/src/main.cpp)
!mac {
SOURCES+=
}
HEADERS+=
FORMS+=
TRANSLATIONS+= \\
           $(winPath $SOURCE_DIR/lang/gppoisson.fr.ts) \\
           $(winPath $SOURCE_DIR/lang/gppoisson.de.ts) \\
           $(winPath $SOURCE_DIR/lang/gppoisson.es.ts) \\
           $(winPath $SOURCE_DIR/lang/gppoisson.it.ts)
END
echo "target.path = $(winPath $BINDIR)"
echo "INSTALLS += target"
if [ "$SKIP_QT_TRANSLATIONS" = "no" ]; then
  cat << END
lang.path = $(winPath $SHAREDIR/lang)
lang.files = lang/gppoisson.fr.qm \\
             lang/gppoisson.de.qm \\
             lang/gppoisson.es.qm \\
             lang/gppoisson.it.qm
INSTALLS += lang
END
fi
) > $TEMP_DIR/gppoisson.pro
replaceFile $BUILD_DIR gppoisson.pro

###############################################################
# Generate src/gppoissonInstallPath.h file
###############################################################
echo "* src/gppoissonInstallPath.h..."
test -d $BUILD_DIR/src || mkdir -p $BUILD_DIR/src
(
cat << END
#ifndef GPPOISSON_INSTALL_PATHS
#define GPPOISSON_INSTALL_PATHS
#define GPPOISSON_BINDIR "$BINDIR"
#define GPPOISSON_LIBDIR "$LIBDIR"
#define GPPOISSON_INCDIR "$INCDIR"
#define GPPOISSON_SHAREDIR "$SHAREDIR"
#endif // GPPOISSON_INSTALL_PATHS
END
) > $TEMP_DIR/gppoissonInstallPath.h
replaceFile $BUILD_DIR/src gppoissonInstallPath.h

###############################################################
# Generate src/gppoissonStatic.h file
###############################################################
###############################################################
# Generate .qrc file
###############################################################
###############################################################
# Generate binary lang files
###############################################################
cd ${BUILD_DIR}
if [ "$SKIP_QT_TRANSLATIONS" = "no" ]; then
  echo "* Qt translations..."
  test -d lang || mkdir -p lang
  beginModificationSafe lang/*.qm
  for f in $(ls $SOURCE_DIR/lang/*.ts); do
    QM_FILE=lang/$(basename $f ts)qm
    rm -f $QM_FILE
    ${QT_BIN_PATH}lrelease -silent $f -qm $QM_FILE
  done
  endModificationSafe lang lang/*.qm
fi
###############################################################
# Generate Makefile
###############################################################
if [ "$SKIP_QMAKE" = "no" ]; then
  echo "* Makefile..."
  if [ "$PLATFORM" = "MINGW" ]; then
    QMAKE_OPTIONS="-spec win32-g++"
  else
    QMAKE_OPTIONS=""
  fi
  beginModificationSafe Makefile*
  ${QT_BIN_PATH}qmake $QMAKE_OPTIONS gppoisson.pro
  endModificationSafe . Makefile*
  cd ${SOURCE_DIR}
  ###############################################################
  # Final user message
  ###############################################################
  echo
  if [ "$PLATFORM" = "MINGW" ]; then
    CONFIGURE_COMMAND="sh configure"
  else
    CONFIGURE_COMMAND="./configure"
  fi
  echo "  gppoisson is now configured for building."
  echo
  echo "  To proceed, run (if you have 8 cores)"
  echo
  if [ "${BUILD_DIR}" != "${SOURCE_DIR}" ]; then
    echo "    $ make -j 8 -C ${BUILD_DIR}"
  else
    echo "    $ make -j 8"
  fi
  echo
  echo "  Once everything is built, you must run either"
  echo
  echo "    $ su -c \"make install\", if root privileges are needed;"
  echo "  or"
  echo "    $ make install, if user permissions are sufficient."
  echo
  echo "  The current configuration options are stored in 'configure.cache'."
  echo "  To change the configuration, run '$CONFIGURE_COMMAND [options]'."
  echo
else
  echo "  To finish configuration, run qmake"
  echo
fi
cleanexit 0
