#!/bin/bash
# This code is part of CMPL
#
# Copyright (C) 2007 - 2016 Thomas Schleiff - Halle(Saale), 
# Germany and Mike Steglich - Technical University of Applied Sciences
# Wildau, Germany 
#
# Coliop3 and CMPL are projects of the Technical University of 
# Applied Sciences Wildau and the Institute for Operations Research 
# and Business Management at the Martin Luther University 
# Halle-Wittenberg.
# Please visit the project homepage <www.coliop.org>
# 
# CMPL 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 3 of the License, or 
# (at your option) any later version.
# 
# CMPL is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
# License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# @author  Mike Steglich - March 2016


# usage cmplMake
function usage () 
{
	if [ $oSystem == "win" ]; then 
		echo "Usage: make-cmpl [ test | clean | package ]"
	else
		echo "Usage: make-cmpl [ test | clean | package | install ]"
	fi
    echo " "
    echo "test                          - test routine "
    echo "clean                         - removes the obj and moc files in folder obj/"
    echo "package                       - creates a distribution package in folder Package/[linux/osx/win]"
    if [ $oSystem != "win" ]; then 
    	echo "getExternals                  - downloads and builds OS,GLPK and Coliop"
    fi
    exit 1
}

# copy the CMPL binary out of the cmpl.app
function copyCMPLOsx () {
	if  ( test -d bin/cmpl.app )  ; then 
		cp bin/cmpl.app/Contents/MacOS/cmpl bin/cmpl
	fi 
}

# CMPL test routine
function testCMPL () 
{
    cd test
    ./test-all
    cd ..
    exit 0
}


# test of the OS
oSystem="";
if [[ $OSTYPE == *darwin* ]] ; then 
	oSystem="osx"
elif  [[ $OSTYPE == *linux* ]] ; then
	if [[ $MACHTYPE =~ .*arm.* ]] ; then
		oSystem="raspbian"
	else 
		oSystem="linux"
	fi
elif  [[ $OSTYPE == *msys* ]] ; then
	oSystem="win"
fi

if [ $oSystem == "" ] ; then
	echo "cmplMake error: Can not recognize your OS"
	exit 1
fi

if [ $# -gt 1 ] ; then
    usage
    exit 1
fi

if [ $# == 0 ] ; then 
    if [ $oSystem == "win" ] ; then
        qmake cmpl.pro -r -spec win32-g++ CONFIG+=release
        mingw32-make -w
        cd bin
        windeployqt  --no-translations --no-system-d3d-compiler --no-plugins --release  cmpl.exe
        cd ..
    elif [ $oSystem == "osx" ] ; then
        qmake cmpl.pro -r -spec macx-clang CONFIG+=x86_64 CONFIG+=release
        make -w
        copyCMPLOsx
    elif [ $oSystem == "linux" ] ; then
        qmake cmpl.pro -r -spec linux-g++  CONFIG+=release
		make -w
    elif [ $oSystem == "raspbian" ] ; then
        qmake cmpl.pro -r -spec linux-g++  CONFIG+=release
		make -w
    else
        usage   
    fi
    cp data/gurobiCmpl/gurobiCmpl.py bin/
    
    cp "data/${oSystem}/cmpl.opt" bin/
	if [ $oSystem == "win" ] ; then
		cp "data/${oSystem}/gurobiPython.bat" bin/
	else 
		cp "data/${oSystem}/gurobiPython" bin/
	fi
fi


if [ $# == 1 ] ; then
	if [ $1 == "test" ]; then
		testCMPL
	elif [ $1 == "clean" ]; then
		if [ $oSystem == "win" ]; then 
			mingw32-make clean
		else
          	        make clean
                fi
	elif [ $1 == "package" ]; then
                data/makePackage
    elif [ $1 == "getExternals" ]; then
    	        if [ $oSystem != "win" ]; then 
			cd data
			./build-CBC-for-cmpl
			./build-GLPK-for-cmpl
			cd ..
	        fi
    else
    	        usage	
	fi
fi

exit 0
