#!/bin/sh

if ! test $PWD; then
  echo Your shell does not exist. Go away.
  exit 1
fi

if ! test -e ../build/build_qt; then
  echo
  echo ERROR: could not find ../build/build_qt
  echo
  echo Please cd to the build directory where this script is and try again.
  echo
  exit 1
fi

if ! test -x ../qtbase/configure; then
  echo
  echo ERROR: could not find ../qtbase/configure
  echo
  if test -e ../qtbase/configure; then
    echo You have non-unix Qt source. Please unpack the .tar.gz file and try again.
  else
    echo Please make sure you have copied the Qt source code to the parent directory.
  fi
  echo
  exit 1
fi

cd ..
PREFIX=$PWD/qt

UNAME=`uname 2> /dev/null`
UNAMEM=`uname -m 2> /dev/null`
if test "x$UNAME" = "xDarwin"; then
  QTV="5.3.2"
  GTK="-no-gtkstyle -no-glib"
else
  QTV="5.6.3"
  GTK="-gtkstyle -glib"
fi

PATCH=1
XCB=-qt-xcb
SQL="-no-sql-sqlite -no-sql-odbc -no-sql-psql -no-sql-tds -no-sql-db2 -no-sql-ibase -no-sql-mysql -no-sql-oci -no-sql-sqlite2"
PCRE=
RELEASE=-release
CPUS=0
while test $# -gt 0; do
  case "$1" in
    -no-xcb) XCB=-system-xcb;;
    -no-gtk) GTK="-no-gtkstyle -no-glib";;
    -no-patch) PATCH=0;;
    -install-exec) CPUS=1; SQL="-no-opengl -no-egl -system-freetype";;
    -install-make) PCRE=-qt-pcre;;
    -debug) RELEASE=-debug;;
    -qt) QTV=$2; shift;;
    -cores) CPUS=$2; shift;;
    *) break;;
  esac
  shift
done

if test $PATCH -gt 0; then
  echo Patching Qt $QTV for $UNAME...
  PATCH1=patches/501_qt_bugfixes.diff
  if test "$QTV" = "5.3.2"; then
    PATCH2=patches/530_qt_static.diff
    PATCH0=patches/531_qt_bugfix.diff
    PATCH3=patches/532_qt_alert.diff
    for include in QtCore QtDBus QtGui QtNetwork QtOpenGL QtPlatformSupport QtPrintSupport QtSql QtTest QtWidgets QtXml; do
      ln -sf 5.3.2 qtbase/include/$include/5.3.2-sim
    done
  else
    PATCH2=patches/560_qt_static.diff
    PATCH0=patches/561_qt_bugfix.diff
    PATCH3=patches/562_qt_alert.diff
  fi

  CHECK=--dry-run
  gpatch -f -p0 -R --dry-run --version < /dev/null > /dev/null 2> /dev/null
  RET=$?
  if test $RET -ne 0; then
    patch -f -p0 -R --dry-run --version < /dev/null > /dev/null 2> /dev/null
    RET=$?
    if test $RET -ne 0; then
      if test $RET -lt 127; then
        CHECK=-C
        patch -f -p0 -R -C --version < /dev/null > /dev/null 2> /dev/null
        RET=$?
      fi
    fi
    PATCH=patch
  else
    PATCH=gpatch
  fi
  PATCHED1=$PATCH1
  PATCHED2=$PATCH2
  PATCHED0=$PATCH0
  PATCHED3=$PATCH3
  if test $RET -ne 0; then
    echo
    if test $RET -ge 127; then
      echo Please make sure you have the 'patch' program installed and try again.
      echo
      exit 100
    fi
    echo OOPS: Your 'patch' program does not seem to support the -C and -R options.
    echo
    echo Skipping check for already applied patches and applying them now...
  else
    $PATCH -f -p0 -R $CHECK < $PATCH1 > /dev/null
    if test $? -eq 0; then
      PATCHED1=
    fi
    $PATCH -f -p0 -R $CHECK < $PATCH2 > /dev/null
    if test $? -eq 0; then
      PATCHED2=
    fi
    $PATCH -f -p0 -R $CHECK < $PATCH0 > /dev/null
    if test $? -eq 0; then
      PATCHED0=
    fi
    $PATCH -f -p0 -R $CHECK < $PATCH3 > /dev/null
    if test $? -eq 0; then
      PATCHED3=
    fi
  fi

  for patch in $PATCHED1 $PATCHED2 $PATCHED0 $PATCHED3; do
    $PATCH -f -p0 < $patch
    RET=$?
    if test $RET -ne 0; then
      echo
      echo ERROR $RET: could not patch the Qt source code.
      echo
      echo The sources under ../qtbase are not of the correct version of Qt: $QTV
      echo You may proceed to re-run with -no-patch at your own risk.
      echo
      exit 2
    fi
  done
fi

if test "x$UNAME" = "xDarwin"; then
  if test "$QTV" = "5.3.2"; then
    PLATFORM=macx-g++
  else
    PLATFORM=macx-clang
  fi
  XCB=-no-xcb
  PLATFORM="-platform $PLATFORM -system-zlib -no-framework -opengl desktop -no-openvg -no-fontconfig -no-audio-backend -no-harfbuzz -no-xcb -no-feature-TEXTCODEC -no-freetype -no-feature-FREETYPE -no-dbus -no-feature-DBUS"
  SDK=`xcodebuild -showsdks 2>/dev/null | grep macosx | tail -1 | cut -f 3`
  RET=$?
  if test $RET -ne 0; then
    echo
    echo ERROR $RET: could not find macosx SDK version.
    echo Please make sure that xcodebuild is installed.
    echo
    exit 3
  fi
  if test -z "$SDK"; then
    echo
    echo ERROR: could not find macosx SDK version. Proceed at your own risk.
    echo
    exit 3
  fi
else
  PLATFORM=-qt-harfbuzz
  SDK=
  if test "x$UNAME" = "xNetBSD"; then
    PLATFORM=-no-harfbuzz
  fi
  if test "x$UNAME" = "xOpenBSD"; then
    if test -x /usr/bin/ld.bfd; then
      PLATFORM="$PLATFORM -platform openbsd-eg++"
    fi
  fi
  if test "x$UNAME" = "xFreeBSD"; then
    LIBDIR=`gcc -### 2>&1 | grep libdir | tr ' ' '\n' | grep libdir | cut -d = -f 2`
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR
    export LD_LIBRARY_PATH
    PLATFORM="$PLATFORM -platform freebsd-g++"
  elif test "x$UNAME" = "xDragonFly"; then
    PLATFORM="$PLATFORM -platform freebsd-g++"
  fi
  PLATFORM="-qt-zlib $PLATFORM $XCB -no-pch -no-accessibility -no-feature-DRAGANDDROP -no-feature-TABLETEVENT -no-feature-SETTINGS -no-feature-ACCESSIBILITY -no-feature-ANIMATION"
  if test "x$UNAMEM" = "xi386" -o  "x$UNAMEM" = "xi486" -o "x$UNAMEM" = "xi586" -o "x$UNAMEM" = "xi686"; then
    PLATFORM="-no-sse2 $PLATFORM"
  fi
  if test "$QTV" != "5.3.2"; then
    if test "x$UNAME" = "xOpenBSD"; then
      PLATFORM="$PLATFORM -no-dbus"
    else
      PLATFORM="$PLATFORM -dbus-runtime"
    fi
  fi
fi
if test "$QTV" = "5.3.2"; then
  OPTIONS="-no-iwmmxt -no-nis -no-feature-REGULAREXPRESSION"
else
  OPTIONS="-no-libinput -no-libproxy -no-gstreamer -qt-xkbcommon-x11 -no-securetransport -no-syslog -no-ltcg -no-gbm -no-mirclient"
fi

echo
echo '********** configure' $SDK $GTK $PLATFORM $OPTIONS $SQL $PCRE $RELEASE -prefix $PREFIX $*
cd qtbase
if test $? -ne 0; then
  exit 1
fi
./configure -prefix $PREFIX -extprefix $PREFIX $SDK -opensource -confirm-license $GTK $PLATFORM $OPTIONS $SQL $PCRE $RELEASE -no-icu -no-sse3 -no-ssse3 -no-sse4.1 -no-sse4.2 -no-avx -no-avx2 -no-libudev -no-libjpeg -no-gif -qt-libpng -no-xcb-xlib -no-tslib -no-mtdev -no-journald -no-xinput2 -no-pulseaudio -no-evdev -no-compile-examples -no-shared -static -no-qml-debug -no-openssl -no-cups -no-eglfs -no-directfb -no-linuxfb -no-kms -no-system-proxies -feature-systemtrayicon -nomake examples -nomake tests -nomake tools -no-iconv -no-alsa -no-feature-TEXTODFWRITER -no-feature-CONCURRENT -no-feature-EFFECTS -no-feature-SHAREDMEMORY -no-feature-SYSTEMSEMAPHORE -no-feature-IM -no-feature-PROCESS -no-feature-DOM -no-feature-TREEWIDGET -no-feature-LISTWIDGET -no-feature-DATETIMEEDIT -no-feature-TEXTBROWSER -no-feature-SPLASHSCREEN -no-feature-LCDNUMBER -no-feature-FONTCOMBOBOX -no-feature-TOOLBAR -no-feature-TOOLBOX -no-feature-DOCKWIDGET -no-feature-MDIAREA -no-feature-DIAL -no-feature-GRAPHICSVIEW -no-feature-GRAPHICSEFFECT -no-feature-SYNTAXHIGHLIGHTER -no-feature-STATUSTIP -no-feature-WHATSTHIS -no-feature-CALENDARWIDGET -no-feature-PRINTPREVIEWWIDGET -no-feature-KEYSEQUENCEEDIT -no-feature-COLORDIALOG -no-feature-FONTDIALOG -no-feature-PRINTDIALOG -no-feature-PRINTPREVIEWDIALOG -no-feature-PROGRESSDIALOG -no-feature-ERRORMESSAGE -no-feature-WIZARD -no-feature-SORTFILTERPROXYMODEL -no-feature-IDENTITYPROXYMODEL -no-feature-STRINGLISTMODEL -no-feature-DATAWIDGETMAPPER -no-feature-COLUMNVIEW -no-feature-STYLE_WINDOWSXP -no-feature-STYLE_WINDOWSVISTA -no-feature-STYLE_WINDOWSCE -no-feature-STYLE_WINDOWSMOBILE -no-feature-MOVIE -no-feature-IMAGEFORMAT_BMP -no-feature-IMAGEFORMAT_JPEG -no-feature-IMAGEFORMAT_PPM -no-feature-IMAGEFORMAT_XBM -no-feature-IMAGE_HEURISTIC_MASK -no-feature-IMAGE_TEXT -no-feature-PICTURE -no-feature-PDF -no-feature-PRINTER -no-feature-CUPS -no-feature-PAINT_DEBUG -no-feature-CODECS -no-feature-BIG_CODECS -no-feature-ICONV -no-feature-FTP -no-feature-HTTP -no-feature-UDPSOCKET -no-feature-NETWORKPROXY -no-feature-SOCKS5 -no-feature-NETWORKINTERFACE -no-feature-NETWORKDISKCACHE -no-feature-BEARERMANAGEMENT -no-feature-COMPLETER -no-feature-FSCOMPLETER -no-feature-DESKTOPSERVICES -no-feature-UNDOCOMMAND -no-feature-UNDOSTACK -no-feature-UNDOGROUP -no-feature-UNDOVIEW -no-feature-STATEMACHINE -no-feature-GESTURES -no-feature-XMLSCHEMA -no-feature-MIMETYPE $*
RET=$?
if test $RET -ne 0; then
  echo
  echo ERROR $RET: could not configure Qt.
  echo
  exit 101
fi

if test "x$UNAME" != "xDarwin"; then
  GREP=`grep FontConfig config.summary`
  RET=$?
  if test $RET -lt 127; then
    if test $RET -ne 0; then
      echo
      echo ERROR $RET: could not configure Qt - config.summary not created.
      echo
      exit 4
    fi
    if test "x${GREP##* }" != "xyes"; then
      echo
      echo $GREP
      echo
      echo ERROR: could not configure Qt - libfontconfig not found.
      echo
      exit 102
    fi
  fi
fi

if test $CPUS -eq 0; then
  CPUS=`python -c "import multiprocessing;print(multiprocessing.cpu_count())"` 2> /dev/null
  if test -z "$CPUS"; then
    CPUS=1
  fi
fi
echo '********** make -j' $CPUS
echo
make -j $CPUS
RET=$?
if test $RET -ne 0; then
  echo
  echo ERROR $RET: could not compile Qt.
  echo
  if test $RET -ge 127; then
    echo Please make sure you have the 'make' program installed and try again.
    echo
  fi
  exit 103
fi
echo
echo '********** make install'
echo

make install
RET=$?
if test $RET -ne 0; then
  echo
  echo ERROR $RET: could not install Qt.
  echo
  echo The Qt sources seem to have been compiled, but the Qt libraries
  echo could not be copied to the target directory ../qt. Sorry.
  echo
  exit 4
fi
echo
echo Qt compiled SUCCESSFULLY.
echo
if test "x$XCB" = "x-qt-xcb"; then
  cp -f -p -R src/3rdparty/xcb/include/xcb ../qt/include
fi
exit 0
