#!/bin/bash

appname=$(pwd | sed "s/.*\/\(.*\)/\1/")                                  #"

usage="
Usage: `basename $0` [OPTION] \"...\"
Compile a Qt program for different platforms.
If no options are given the script will detect the platform and
compile the program with the latest Qt version installed.

Additional flags to be passed at the qmake -project command can
be given in quotes.
The default flags to be passed are:
For all the systems
   \"QT += core gui\" \"greaterThan(QT_MAJOR_VERSION, 4): QT += widgets\"
Additionaly for Windows and linux systems with qt5
   \"DEPENDPATH += . include src\" \"INCLUDEPATH += . include\"

After compilation the directory will be cleaned.

  -h, --help                    display this help and exit
  -win, -win64                  cross-compile code for windows 64bit OS
                                the mingw cross-compilers have to be already
                                installed
  -win32                        cross-compile code for windows 32bit OS
                                the mingw cross-compilers have to be already
                                installed
  -qt4                          for opensuse OS that has both Qt4 and Qt5
                                installed, it will compile using the Qt4
                                libraries
  -d, --debug                   it will keep the project file (.pro) and the
                                Makefiles
"

for arg in "$@" ; do
   case "$1" in
      -h|--help)
         echo "$usage"
         exit 0
      ;;
      -win|-win64)
         windows="1"
         windows64="1"
         shift
      ;;
      -win32)
         windows="1"
         windows32="1"
         shift
      ;;
      -qt4)
         qt4="1"
         shift
      ;;
      *)
         qt_flags+=("$1")
         shift
      ;;
      -d|--debug)
         debug="1"
         shift
      ;;
      *)
         echo "Unknown option."
         echo "Type `basename $0` -h or --help for usage"
         exit 1
      ;;
   esac
done

if uname -a | grep -iq linux 
   then
   OS="linux"
elif uname -a | grep -iq mac
   then
   OS="mac"
else
   echo "You are running on a non supported system"
   exit 1
fi

if [[ "$OS" == "mac" ]]
   then
   if hash qmake 2>/dev/null
      then
      program=qmake
      qt_flags=("QT += core gui" "greaterThan(QT_MAJOR_VERSION, 4): QT += widgets" "APP_Data_FILES.files = data/masses.dat" "APP_Data_FILES.path = Contents/MacOS/data" "STP_Data_FILES.files = data/SCOEF.95A" "STP_Data_FILES.path = Contents/MacOS/data" "QMAKE_BUNDLE_DATA += APP_Data_FILES STP_Data_FILES")
   else
      echo "qmake is not installed"
      exit 2 
   fi
elif [[ "$OS" == "linux" ]]
   then
   if [[ "$windows" == "1" ]]
      then
      if [[ "$windows64" == "1" ]]
         then
         if hash mingw64-qmake-qt5 2>/dev/null
            then
            program=mingw64-qmake-qt5
            dllfolder="/usr/x86_64-w64-mingw32/sys-root/mingw/bin"
            winfolder=Win64_$appname
            qt_flags+=("DEPENDPATH += . include src" "INCLUDEPATH += . include")
         else
            echo "mingw64-qmake-qt5 is not installed"
            exit 2 
         fi
      elif [[ "$windows32" == "1" ]]
         then
         if hash mingw32-qmake-qt5 2>/dev/null
            then
            program=mingw32-qmake-qt5
            dllfolder="/usr/i686-w64-mingw32/sys-root/mingw/bin/"
            winfolder=Win32_$appname
            qt_flags+=("DEPENDPATH += . include src" "INCLUDEPATH += . include")
         else
            echo "mingw32-qmake-qt5 is not installed"
            exit 2 
         fi
      fi
   else
      if [[ "$qt4" == "1" ]]
         then
         if hash qmake 2>/dev/null
            then
            program=qmake
         else
            echo "qmake is not installed"
            exit 2
         fi
      elif hash qmake-qt5 2>/dev/null
         then program=qmake-qt5
         qt_flags+=("DEPENDPATH += . include src" "INCLUDEPATH += . include")
      elif hash qmake 2>/dev/null
         then
         program=qmake
      else
         echo "qmake is not installed"
         exit 2
      fi
   fi
   qt_flags+=("QT += core gui" "greaterThan(QT_MAJOR_VERSION, 4): QT += widgets")
fi

qt_version=$($program -v | grep "Qt\ version" | sed "s/.\+Qt\ version\ \([0-9.]\+\).\+/\1/")                                                  #"
if [ -f $appname\_Qt_$qt_version ] ; then rm -rf $appname\_Qt_$qt_version ; fi
if [ -d $winfolder ] ; then rm -rf $winfolder ; fi
if [ -d debug ] ; then rm -rf debug ; fi
if [ -d release ] ; then rm -rf release ; fi
$program -project "${qt_flags[@]}"
$program
make
makeresult="$?"
if [[ "$makeresult" != "0" ]] ; then echo "" ; echo "!!!!!!!!!!!!!!!!!!!!!" ; echo "An error occured while compiling." ; echo "Exiting without removing any files." ; exit 2 ; fi
make clean
if [[ "$debug" != "1" ]]
   then
   rm -rf $appname.pro
   rm -rf Makefile*
fi

if [ -f $appname ]
   then
   mv $appname $appname\_Qt_$qt_version
fi

if [[ "$windows" == "1" ]]
   then
   mkdir $winfolder
   mv release/*.exe ./$winfolder
   rm -rf debug release
   if hash wine 2>/dev/null
      then
      cp -r $dllfolder/../lib/qt5/plugins/platforms $winfolder
      until [ $PID ]
         do
         wine $winfolder/$appname.exe 2>kk &
         sleep 5s
         PID=$(pgrep $appname)
         if [ -n "$PID" ]
            then
            kill -9 $PID 2>kk &
            sleep 5s
            rm -f kk* object_script.*
#            make_msi
#            if [ -f $appname.msi ] ; then if [[ "$windows32" == "1" ]] ; then mv $appname.msi $appname\_32bit.msi ; else mv $appname.msi $appname\_64bit.msi ; fi ;fi
            exit 0
         else
            grep ".dll" kk | cut -d" " -f3 > kk1
            sort -u kk1 > kk
            IFS=$'\n' read -a dlls <<< $(cat kk)
            for dll in ${dlls[@]}
               do
               if [ ! -f $winfolder/$dll ] ; then cp $dllfolder/$dll $winfolder/ ; fi
            done
         fi
         rm -f kk*
      done
   else
      echo "wine is not installed"
      exit 2
   fi
fi

if [[ "$OS" == "mac" ]]
   then
   macdeployqt $appname.app -dmg
   rm -rf .qmake.stash $appname.app
fi

exit 0
