#!/bin/sh
# Don't set `-e'!

# Basic environment variables
test -n "$MAKE"      || MAKE="make"
test -n "$CXX"       || CXX="c++"
test -n "$QTCHOOSER" || QTCHOOSER="$(command -v qtchooser 2> /dev/null)"
test -n "$TMPDIR"    || TMPDIR="$PWD"

# Use qtchooser if we have it, otherwise use Posix command -v
if test -n "$QTCHOOSER"; then
    test -n "$QMAKE"    || QMAKE="$QTCHOOSER -run-tool=qmake -qt=5"
    test -n "$LRELEASE" || LRELEASE="$QTCHOOSER -run-tool=lrelease -qt=5"
else
    test -n "$QMAKE"    || QMAKE="$(command -v qmake-qt5 2>/dev/null || command -v qmake 2>/dev/null)"
    test -n "$LRELEASE" || LRELEASE="$(command -v lrelease-qt5 2>/dev/null || command -v lrelease 2>/dev/null)"
fi


errorExit() {
    echo $1
    test x"$2" != x"1" || fail_on_missing="yes"
    test $fail_on_missing != "yes" || exit 1
}

help() {
cat << EOF

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g. CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

  -h, --help              display this help and exit

  --prefix PREFIX,
  --prefix=PREFIX         install files in PREFIX [/usr/local]

  -d, --debug             build with debugging symbols intact

  --fail-on-missing       return exit 1 if non-optional components are missing

  --lrelease COMMAND,
  --lrelease=COMMAND      specify lrelease command [$LRELEASE]

  --qmake COMMAND,
  --qmake=COMMAND         specify qmake command [$QMAKE]
  --qmake-args ARGS       arguments to pass directly to qmake

Some influential environment variables:
  MAKE        make command
  CXX         C++ compiler
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CXXFLAGS    C++ compiler flags

EOF
}

# check "toolname/description" "command" errorExit
# set errorExit to 1 for an error exit, otherwise leave it empty.
# examples:
# check "a Git client tool" git
# check c++ g++ 1
check() {
    printf "checking for $1... "
    t=$(basename $2)
    command -v $t 2>/dev/null 1>/dev/null
    test $(echo $?) -eq 0 && echo "$(command -v $t)" || errorExit "not found!" $3
}


fail_on_missing="no"
while [ "$#" -ge 1 ]; do
    key="$1"
    shift

    case $key in
        -h|--help)
        help
        exit 0
        ;;
        -d|--debug)
        BUILDMODE="debug"
        echo "WARNING: Building in debug mode!"
        ;;
        --prefix=*) # --prefix=<something>
        PREFIX="${key#*=}"
        ;;
        --prefix) # --prefix <something>
        PREFIX="$1"
        shift
        ;;
        --fail-on-missing)
        fail_on_missing="yes"
        ;;
        --lrelease=*)
        LRELEASE="${key#*=}"
        ;;
        --lrelease)
        LRELEASE="$1"
        shift
        ;;
        --qmake=*)
        QMAKE="${key#*=}"
        ;;
        --qmake)
        QMAKE="$1"
        shift
        ;;
        --qmake-args)
        # Leave qmake args in $@
        break
        ;;
        *)
        echo WARNING: Unknown option "$key"
        ;;
    esac
done


# check for QT5 qmake
printf "checking for QT5 qmake... "
$QMAKE -help 2>/dev/null 1>/dev/null
test $? -eq 0 && echo "$QMAKE" || \
    errorExit "not found!\n  Try to run configure with \`--qmake /path/to/qmake-executable'." 1

# check for lrelease
printf "checking for lrelease... "
$LRELEASE -help 2>/dev/null 1>/dev/null
test $? -eq 0 && echo "$LRELEASE" || \
    errorExit "not found!\n  Try to run configure with \`--lrelease /path/to/lrelease-executable'." 1

check c++ "$CXX" 1

# check C++ compiler functionality
rm -f test test.cpp
cat << EOF > test.cpp
#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
EOF
printf "checking whether c++ compiler builds test program... "
$CXX -o "$TMPDIR/test" test.cpp && echo "ok" || errorExit "error!" 1
printf "checking whether c++ compiler supports -std=c++0x... "
rm -f test
$CXX -std=c++0x -o "$TMPDIR/test" test.cpp && echo "ok" || errorExit "error!" 1
printf "checking whether compiled test program works... "
"$TMPDIR/test" 2>/dev/null 1>/dev/null && echo "ok" || errorExit "error!" 1
rm -f "$TMPDIR/test" test.cpp

check make "$MAKE" 1
check pkg-config pkg-config 1

# check for libraries via pkg-config
libraries="Qt5Core Qt5Gui Qt5Network Qt5WebEngine Qt5Widgets Qt5WebEngineWidgets Qt5PrintSupport Qt5Svg Qt5WebSockets Qt5WebChannel uchardet"
for l in $libraries ; do
    printf "checking for ${l} library... "
    pkg-config --libs $l 2>/dev/null 1>/dev/null
    test $(echo $?) -eq 0 && pkg-config --libs $l || errorExit "not found!"
done

if [ -f "Makefile" ]; then
    make distclean 2>/dev/null 1>/dev/null || true
fi


printf "generate Makefile... "

$QMAKE PREFIX="$PREFIX" \
QMAKE_CXX="$CXX" \
QMAKE_CXXFLAGS="$CXXFLAGS $CPPFLAGS" \
QMAKE_LFLAGS="$LDFLAGS" \
LRELEASE="$LRELEASE" \
CONFIG+="$BUILDMODE" \
"$@" notepadqq.pro && echo "done" || errorExit "error!" 1
